Skip to content

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

Merged
merged 33 commits into from
Jul 1, 2021
Merged

Sub-packages and immutable hook #992

merged 33 commits into from
Jul 1, 2021

Conversation

shuding
Copy link
Member

@shuding shuding commented Feb 21, 2021

revalidateWhenStale

This PR adds the revalidateWhenStale option to useSWR and it's true by default. If a resource has revalidateWhenStale: false, SWR will check if the cache, or initialData exists. If one of them is not undefined, 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 for useSWR 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:

import useSWRImmutable from 'swr/immutable'
import useSWRInfinite from 'swr/infinite'

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:

function useSWRImmutable(key, fetcher, config) {
  return useSWR(key, fetcher, {
    ...config,
    revalidateWhenStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  })
}

A middleware is included in the exports too so you can combine it with useSWRInfinite.

Known issues

To-do

  • Middleware #1160
  • Normally, this option needs to be set together with revalidateOnFocus and revalidateOnReconnect. We need to provide a useSWRImmutable hook to pre-config these options for you.
  • Deprecate revalidateOnMount in favor of this option.
  • More test cases.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 21, 2021

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:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

@shuding shuding changed the title Immutable option revalidateWhenStale option May 5, 2021
@shuding shuding changed the title revalidateWhenStale option Sub-packages and immutable hook Jun 28, 2021
Copy link
Member

@huozhi huozhi left a 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": ... }
}

@shuding
Copy link
Member Author

shuding commented Jun 29, 2021

@huozhi It seems there're still things don't support exports yet, for example csb doesn't seem to work...

CleanShot 2021-06-29 at 11 32 37@2x

@shuding shuding marked this pull request as ready for review June 30, 2021 16:12
@shuding shuding requested a review from pacocoursey as a code owner June 30, 2021 16:12
Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀

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.

2 participants