-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Sub-packages and immutable hook #992
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
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 3593fd1:
|
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.
I wonder if we can delete the sub folders, package.json and yarn.lock. since we can rely them on exports
in package.json. How about trying to put them all inside src folder.
- src/
| - infinite/
| - immutable/
package.json
{
"exports": { "./immutable": ..., "infinite": ... }
}
@huozhi It seems there're still things don't support |
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.
🚀🚀🚀
revalidateWhenStale
This PR adds the
revalidateWhenStale
option touseSWR
and it'strue
by default. If a resource hasrevalidateWhenStale: false
, SWR will check if the cache, orinitialData
exists. If one of them is notundefined
, SWR won't trigger the validation on mount.Idea behind this option
There're some types of resources we are sure that they will never change (during the application lifecycle), or we want to manually control their revalidation, instead of always revalidate on mount. This helps reduce the number of requests for some specific data.
We are explicitly calling it
revalidateWhenStale
because it follows the existing naming and, it has a smaller granularity that only controls one thing. Ideally we want all options foruseSWR
to be orthogonal, and we can build another hook layer with pre-configurations on top of them to have more scenarios covered.Sub-packages
We're adding sub-packages to SWR with this PR. You can now import these 2 built-in hooks via:
The old
import { useSWRInfinite } from 'swr'
was removed with this PR too. This change will enable better code-splitting so if those hooks are not used in your project, they won't be bundled.useSWRImmutable
It's a pre-configured
useSWR
hook, basically the same as:A middleware is included in the exports too so you can combine it with
useSWRInfinite
.Known issues
exports
To-do
revalidateOnFocus
andrevalidateOnReconnect
. We need to provide auseSWRImmutable
hook to pre-config these options for you.revalidateOnMount
in favor of this option.