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

put initialData in cache #237

Closed
shrugs opened this issue Jan 19, 2020 · 5 comments · Fixed by #231
Closed

put initialData in cache #237

shrugs opened this issue Jan 19, 2020 · 5 comments · Fixed by #231
Labels
feature request New feature or request

Comments

@shrugs
Copy link

shrugs commented Jan 19, 2020

description

when populating a useSWR hook with initialData, that data is per-hook and not stored in the cache to be available to other hook invocations, resulting in duplicate fetches for useSWR invocations that don't have the SSR initialData passed to them.

for example:

  1. in a next.js project a page's getInitialProps function is generating initialData as in the example.
  2. the page component does useSWR(someKey, { initialData }) and correctly SSR renders the initialData.
  3. a child component also invokes useSWR(someKey). the child component's useSWR sees an empty cache and executes fetcher, resulting in an unnecessary fetch.

workarounds

  1. prop-drill or context-pass initialData to all the components that need it
  2. only use useSWR in the parent and prop-drill / context-pass the return value of useSWR to components that need it

expectation

when initialData is provided, it will be stored in the cache under the provided key. when other useSWR hooks are invoked, they'll see the cached value and avoid an extra call to fetcher

@shrugs
Copy link
Author

shrugs commented Jan 19, 2020

somewhat related to the goals in #158

@mirshko
Copy link

mirshko commented Jan 20, 2020

This explains my issue here: #11 (comment)

@mirshko mirshko mentioned this issue Feb 3, 2020
@shuding shuding added the feature request New feature or request label Feb 24, 2020
@rick-nu
Copy link

rick-nu commented Nov 13, 2020

Workaround to cache the server-side initial data on the client side (as long as SWR doens't support it natively): https://www.npmjs.com/package/use-fetch-cache

@yaoyu2021
Copy link

just use revalidateOnMount to trigger cache in backgrounnd

@shuding
Copy link
Member

shuding commented Oct 20, 2021

Update for whoever comes to this issue: https://swr.vercel.app/docs/with-nextjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants