-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a default fetcher using Fetch API #367
Conversation
I agree on this one, but would love to hear more discussions! |
Love it. Would request a note about best practices for error responses; that particular default leads to a lot of vague SyntaxErrors |
I think when you want to handle error errors is when you will start to use a custom build fetcher, this fetcher should allow you to start using SWR without any config or boilerplate until you need a better fetcher |
But yeah we may need to better document how to handle errors, specially because Fetch does not throw in all the HTTP errors |
I really like this idea, since it makes it easier to hit the ground running with SWR. |
This is great, I always need to make that simple fetcher on small demos. Also, I think this would help new people using SWR easily |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the feedback we should take it! 👍
So, I tried @pacocoursey 's idea for SWR as a local cache but this PR fills the console with localhost:3000/whatever 404 errors. 😱 |
@brandonpittman you will need to define a custom fetcher then, maybe you can try passing undefined as your fetcher 🤔 |
@sergiodxa Yeah, passing |
This allows again for lazy execution since passing `undefined` broke with the introduction of a default fetcher in vercel#367
* Allow passing null as the fetcher function This allows again for lazy execution since passing `undefined` broke with the introduction of a default fetcher in #367 * Fix syntax for null fetcher Co-authored-by: Shu Ding <[email protected]>
Not sure if you want this, but I found most of the time a simple fetcher like
url => fetch(url).then(res => res.json())
is more than enough to use useSWR until you have reasons to build your own fetcher (like handling errors or parsing the response in a specific way).I believe adding a default one could make using SWR a little bit faster since now you will only need to install and use.
@shuding if you prefer to don't provide a default one feel free to close this PR.