Skip to content
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

Merged
merged 2 commits into from
Sep 17, 2020

Conversation

sergiodxa
Copy link
Contributor

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.

@shuding
Copy link
Member

shuding commented May 3, 2020

I agree on this one, but would love to hear more discussions!

@allpwrfulroot
Copy link

Love it. Would request a note about best practices for error responses; that particular default leads to a lot of vague SyntaxErrors

@sergiodxa
Copy link
Contributor Author

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

@sergiodxa
Copy link
Contributor Author

But yeah we may need to better document how to handle errors, specially because Fetch does not throw in all the HTTP errors

@leerob
Copy link
Member

leerob commented May 25, 2020

I really like this idea, since it makes it easier to hit the ground running with SWR.

@cristianbgp
Copy link
Contributor

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

Copy link
Member

@shuding shuding left a 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! 👍

@shuding shuding merged commit 7091de9 into vercel:master Sep 17, 2020
@sergiodxa sergiodxa deleted the default-fetcher branch September 17, 2020 18:15
@brandonpittman
Copy link

brandonpittman commented Sep 21, 2020

So, I tried @pacocoursey 's idea for SWR as a local cache but this PR fills the console with localhost:3000/whatever 404 errors. 😱

@sergiodxa
Copy link
Contributor Author

@brandonpittman you will need to define a custom fetcher then, maybe you can try passing undefined as your fetcher 🤔

@brandonpittman
Copy link

@sergiodxa Yeah, passing null in the fetcher spot fixed it. Thanks!

maggo added a commit to maggo/swr that referenced this pull request Oct 29, 2020
This allows again for lazy execution since passing `undefined` broke with the introduction of a default fetcher in vercel#367
shuding added a commit that referenced this pull request Oct 29, 2020
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants