fix: isHydration will cause unnecessary rerender #4212
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Pull request overview
This PR fixes an unnecessary re-render issue in useSWR during hydration. When using SWR in server-side rendered applications (like Next.js), components were rendering twice even when not calling the fetcher, due to the hydration detection mechanism triggering a state change.
Changes:
- Modified the hydration detection logic to use a ref-based approach that avoids triggering re-renders
- Added E2E tests to verify the fix prevents extra renders
- Added E2E tests for the server prefetch warning functionality
- Updated comment to correctly indicate the warning occurs during hydration, not SSR
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/index/use-swr.ts | Changed hydration detection from returning different values to returning the same ref object with mutated content, preventing unnecessary re-renders |
| e2e/test/server-prefetch-warning.test.ts | Added test to verify server prefetch warnings only appear during hydration when expected |
| e2e/test/initial-render.test.ts | Added test to verify SWR doesn't cause extra re-renders |
| e2e/site/app/server-prefetch-warning/page.tsx | Added test page to verify strictServerPrefetchWarning behavior |
| e2e/site/app/render-count/page.tsx | Added test page to count renders and verify no extra re-renders occur |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This hack is crazy (and you have snapshotted tweets?!) |
|
Not sure what's happening, but in our client-side Next.js app we're seeing frequent failures to fetch data since this change. |
close #4210
The current solution is inspired by seb's previous tweet (The original tweet was deleted)
