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

useSWR('key', fetcher) calls fetcher non-deterministically even when key is in the fallback #2931

Open
AndrewRayCode opened this issue Apr 6, 2024 · 0 comments

Comments

@AndrewRayCode
Copy link

Bug report

Description / Observed Behavior

<SWRConfig value={{ fallback: { a: 'b'} }}>

Combined with an inner component of

const { data } = `useSWR('a', () => { console.log('I should never be called'); });

Causes I should never be called to be logged. Note this is non-deterministic, the first few times this is called, it returns the fallback data. A requestAnimationFrame kicks off later which checks for the fallback data, and fails to find it, calling the fetcher.

Expected Behavior

Fallback data should never be re-fetched unless mutated.

Repro Steps / Code Example

This is a debugger screenshot from my app.

Screen Shot 2024-04-05 at 5 13 00 PM

useSWR is doing something incredibly tricky with destructuring, at a glance I don't understand the logic. What I want to point out in the screenshot is that PRELOAD is destructured to an empty object, while you can clearly see in the console that SWRGlobalState has the fallback data there ({ key: {'shader' => {...}, 'assets' => {...} }). This is the data in my app provided to <SWRConfig value={{ fallback: ... }}> in server side props. In this screenshot, key is 'assets', which is defined in the fallback data.

So useSWR() has the data properly in its cache, but it appears to discard it. It then calls the fetcher function.

Additional Context

"swr": "^2.2.5",

I'm doing SSR with Next.js to improve page load speeds. useSWR triggering a client fetch is defeating the purpose of this.

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

No branches or pull requests

1 participant