question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reloading `initialData` after mutation

See original GitHub issue

I’m having some trouble understanding the proper pattern when trying to update/revalidate data from one view/page that’s being mutated somewhere else. I’m using swr in a Next.js project.

The scenario I’m facing is a fairly basic one:

  • /users: A main page with a list of users. Fetches data using a /api/users key.
  • /users/[username]: A details page where you can edit attributes of a single user. Fetches data using a /api/users/[username] key.

If you edit a user, changes to its attributes should be reflected back on the list when going back - which it did, until I introduced SSR via initialData.

/users page has a getServerSideProps fetching all users on the list. Those are plugged into useSWR as { initialData }. This effectively avoids having to fetch users client side on first render, but makes it so it doesn’t refresh the list after editing a user entry: it always returns the original initialData value.

Things I’ve tried:

  1. Adding revalidateOnMount: true to the useSWR call. This solves the problem, but entirely defeats the purpose of passing initialData in as the exact same set of users is unnecessarily fetched twice: once SS from getServerSideProps, once CS from the hook.

  2. Calling mutate('/api/users') from the edit page after successfully submitting the edit form. This would look like the natural solution to my issue. According to the docs, it should “broadcast a revalidation message globally to all SWRs with the same key” - but in my case, I found it doesn’t seem to do anything at all.

  3. Same as above, but calling mutate('/api/users', async users => ...). However, the users array from the callback is always undefined for me. I would have expected the last set of fetched users from the list to be passed. Does this work when navigating between SSR’d pages?

  4. Same as above, but explicitly passing a new list of users with mutate('/api/users', fetch('/api/users')). This works fine, but suffers from the same double fetching quirk as 1. Not to mention the incursion of a clear violation of SoC patterns.

So what’s the expected way to go about this? initialData changes after a mutation and the server returns the correct value. However, it seems as though, swr ignores new data after being set the first around and doesn’t “reload” or “reinitialize” it. While this might be intentional (mimic’ing things like useState), how do I refresh my stale data without fetching the same more than once?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ssorallencommented, Mar 20, 2021

@nfantone RE: 2. in your list, the issue seems to be documented in https://github.com/vercel/swr/issues/751

If you call mutate on a key when there are no mounted components with a useSWR for that key, then mutate is effectively a no-op. I ran into this and ended up manually clearing the cache for that specific key as well. There is no fetcher to go do the re-validation when a mutate is called in that case, but maybe it should be an option to do this cache clearing if there are no fetchers.

1reaction
huozhicommented, Jun 11, 2020

@nfantone it’s not fully documented on readme yet, but you can take a look on the file https://github.com/vercel/swr/blob/master/src/cache.ts hope this is helpful for you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pattern for waiting for cache data to reload after a mutation
I'm trying to figure out the right way of waiting for cache data to reload after a mutations is invoked. I would like...
Read more >
Initial Query Data | TanStack Query Docs
There are many ways to supply initial data for a query to the cache before you need it: Declaratively: Provide initialData to a...
Read more >
Hooks - Apollo GraphQL Docs
A function used to update the Apollo Client cache after the mutation completes. For more information, see Updating the cache after a mutation....
Read more >
Mastering Mutations in React Query | TkDodo's blog
Mutations only take one argument for variables. Since the last argument to mutate is the options object, useMutation can currently only take one ......
Read more >
Fetch initial data on page load in React-Redux application
As soon as I receive the data, I dispatch an action from my root component so that the data get stored in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found