feat: experimental per-key isolation for the Next.js adapters - #1485
feat: experimental per-key isolation for the Next.js adapters#1485franky47 wants to merge 4 commits into
Conversation
A fresh context value on every provider render invalidates all consumers under React 19's lazy context propagation, even through a nested provider of the same context that should shadow them. Memoizing also removes redundant re-render invalidations on unrelated provider re-renders for every adapter.
Next.js' useSearchParams (app) and useRouter (pages) re-render every call site on any search param change. Behind experimental_keyIsolation, a single Bridge per adapter instance consumes those contexts and publishes committed search params into a per-key store that hooks subscribe to, so writing a key only re-renders its watchers. Publishing at commit time only keeps the pathname-gate semantics (#1293/#1273), and the pending-updates overlay covers both the optimistic path and shallow: false round-trips without useOptimistic. On the pages router, isolation additionally requires a render-stable flagged subtree, as Next re-renders the page tree top-down on every route state change.
Previously N/A on Next.js (the context re-renders made the spec unpassable); the flagged adapters make it applicable. Nested flagged providers keep every existing route on the unflagged path, preserving the pinned render counts.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
…ge-next-pr-1485 # Conflicts: # packages/nuqs/src/adapters/lib/key-isolation.test.ts # packages/nuqs/src/adapters/next/impl.pages.ts # packages/nuqs/tests/snapshots/adapters/next/app.snapshot.d.ts
Bundle size
Brotli-compressed. Limits are set in the |
Next.js'
useSearchParams(app) anduseRouter(pages) re-render every call site on any search param change, so a page with many keys pays a full re-render for a single-key write. This has kept the shared key-isolation e2e spec marked N/A on Next.js.Behind an
experimental_keyIsolationflag, each adapter instance runs a single Bridge that consumes those contexts and republishes committed search params into a per-key store hooks subscribe to, so writing one key only re-renders its watchers. Publishing at commit time preserves the existing pathname-gate semantics, and the pending-updates overlay covers both the optimistic path andshallow: falseround-trips withoutuseOptimistic. Unflagged routes stay on the current path untouched, so pinned render counts hold.Opt-in and additive: nothing changes for existing consumers unless they set the flag.
Tasks
Blocked by #1482 (merge first then rebase).