InitialData Cache - Next.js SSR #447
Unanswered
tomevans18
asked this question in
General
Replies: 1 comment 1 reply
-
Currently yes, const data = useSWR(...).data || initialData And we have an improved solution to make it a real inital value, not just a fallback. The new API is planned and we are gonna work on it soon. I shared it on #388 (comment), in the API Design part. Ideally it should be something like: <SWRConfig value={{ initialData: { [key]: data } }}>
<Component/>
</SWRConfig> |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Following reviewing a PR around
initialData
cache (#388) I now understand thatinitialData
is actually a fallback and will be set to the key within the cache if its undefined. This means if there is an existing cache against said key, theninitialData
is effectively ignored.The sadly this creates an issue of stale data. Within our situation we have:
When the second page loads, if there is an existing cache, the server will call the API and obtain the latest data, which is then passed to
initialData
but as there is a cached version,initialData
is ignored and now the data loaded is out of date.Has anyone found a way around this?
We have tried adding
mutate([secondPageSWRKey])
but that doesn't seem to work. We assume this is because you need to pass the data in the second arg if theswr
setup/method is on another page but this would mean making another API call to an endpoint thats is not truly related to the component, which seems a little wrong to do.Beta Was this translation helpful? Give feedback.
All reactions