Unverified Commit 3b1cc0b0 authored by Joseph's avatar Joseph Committed by GitHub

docs: Clarify when useSearchParams boundaries render on the server with Cache Components (#100276)

## Summary

The `useSearchParams` docs say a prerendered route client-renders the
tree up to the closest `<Suspense>` boundary. With Cache Components,
that only holds when the rest of the route is prerendered. If any
component on the route reads request data, such as `cookies()`, the
postponed boundary is rendered on the server at request time with the
real search params. Code inside it then runs on the server, and
browser-only reads in render cause hydration mismatches.

- `docs/01-app/03-api-reference/04-functions/use-search-params.mdx`: In
Prerendering, state when the boundary is server-rendered under Cache
Components, and point to `use(browser())` for browser-only components.
- `errors/blocking-prerender-client-hook.mdx`: The Trade-off section
said the fallback always swaps after hydration. Qualify it to routes
where the rest is prerendered, and point to `use(browser())` for the
request-time case.
- `docs/01-app/02-guides/instant-navigation.mdx`: "suspends during
server rendering" to "suspends during prerendering".

Left unchanged: `errors/missing-suspense-with-csr-bailout.mdx`, which
describes the non-Cache Components model and is accurate there.

Finding: https://next-maintainer-agent.vercel.tools/agent-feedback/2121

## Verification

Production build on `next@16.4.0-canary.37` with `cacheComponents:
true`, `curl "/<route>?q=hello"`:

| Route | `useSearchParams` boundary in server HTML |
| --- | --- |
| Rest of the route prerendered | No, fallback only |
| `cookies()` in a sibling `<Suspense>` | Yes, with `hello` |
| `cookies()` awaited above the component | Yes, with `hello` |
| Sibling `cookies()`, component calls `use(browser())` | No, sibling
still streams |

Matches `app-render.tsx`: a complete server result resumes and aborts
postponed HTML boundaries into client render mode, a partial one saves
postponed state for the request-time resume. Only `useSearchParams` was
tested; the error page Trade-off covers the other URL hooks on the same
code path.

<!-- NEXT_JS_LLM -->
parent ef0bc5f3
......@@ -32,7 +32,7 @@ On a page load, the entire page renders from the document root. Every component
On a client navigation between `/store/shoes` and `/store/hats`, only the components below the `/store` layout re-render. A `<Suspense>` boundary in the root layout covers everything on a page load, but on this navigation, it sits above the re-render scope and does not trigger.
This is also why client-side hooks behave differently. `useSearchParams()` suspends during server rendering because search params are not available at build time. But on a client navigation, the router already has the params from the URL and the hook resolves synchronously. The same component can render immediately on a client navigation but sit behind a fallback on a page load.
This is also why client-side hooks behave differently. `useSearchParams()` suspends during prerendering because search params are not available at build time. But on a client navigation, the router already has the params from the URL and the hook resolves synchronously. The same component can render immediately on a client navigation but sit behind a fallback on a page load.
</details>
......
......@@ -83,6 +83,8 @@ If a route is [prerendered](/docs/app/glossary#prerendering), calling `useSearch
This allows a part of the route to be prerendered while the dynamic part that uses `useSearchParams` is client-side rendered.
With [Cache Components](/docs/app/api-reference/config/next-config-js/cacheComponents), the boundary is client-side rendered only when the rest of the route is prerendered. If any component on the route reads request data, such as [`cookies()`](/docs/app/api-reference/functions/cookies), Next.js renders the boundary on the server at request time with the real search params. To keep a component that reads browser-only APIs, such as `sessionStorage`, out of the server render, use [`use(browser())`](/docs/app/guides/single-page-applications#rendering-components-only-in-the-browser) instead of relying on the `useSearchParams` boundary.
We recommend wrapping the Client Component that uses `useSearchParams` in a `<Suspense/>` boundary. This will allow any Client Components above it to be prerendered and sent as part of initial HTML.
See [Next.js encountered URL data in a Client Component outside of Suspense](/docs/messages/blocking-prerender-client-hook) for full fix options and trade-offs.
......
......@@ -192,7 +192,7 @@ The sibling renders nothing visible, so an empty fallback is correct. There is n
### Trade-off
The user sees the fallback on the first paint of the suspended region, then it swaps to the real value once the hook resolves after hydration. Choose a fallback shape that matches the final layout so the swap doesn't cause a layout shift. See [minimizing layout shift](/docs/app/guides/streaming#cls-cumulative-layout-shift).
The user sees the fallback on the first paint of the suspended region. When the rest of the route is prerendered, the fallback swaps to the real value once the hook resolves after hydration. If any component on the route reads request data, such as [`cookies()`](/docs/app/api-reference/functions/cookies), Next.js renders the boundary on the server at request time instead, so put browser-only reads, such as `localStorage`, behind [`use(browser())`](/docs/app/guides/single-page-applications#rendering-components-only-in-the-browser). Choose a fallback shape that matches the final layout so the swap doesn't cause a layout shift. See [minimizing layout shift](/docs/app/guides/streaming#cls-cumulative-layout-shift).
### Gotchas
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment