Do preloaded resources not get cached in v2.0.0? #2304
-
|
I am trying to use the docs to prefetch data and show. When I run the preload function inside my Component's I have the SWR devtools installed and they show nothing was cached, or any history of an api request being sent. I am calling this, preload<Response>('my-endpoint', myFetcher)Could you provide some guidance as to why this would be happening? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
We don't store the preload result in the cache store that you can see on the devtool. But the preload result is cached in another cache store and is used as the response for the same key of fetcher requests. |
Beta Was this translation helpful? Give feedback.
-
|
I'm also using react-router and SWR's preload. I was surprised that this doesn't behave as I expected: // In route definition...
loader: () => preload("/users", fetcher),
// In component...
const {data: users} = useSWR("/users", fetcher);The component data is undefined at first, I guess, because the useSWR hook didn't grab the preloaded data or something. FWIW, this approach works: // In component...
const {data: users} = useSWR("/users", fetcher, {fallbackData: useLoaderData()}); |
Beta Was this translation helpful? Give feedback.
I'm also using react-router and SWR's preload. I was surprised that this doesn't behave as I expected:
The component data is undefined at first, I guess, because the useSWR hook didn't grab the preloaded data or something.
FWIW, this approach works: