Unverified Commit d6fe60a2 authored by Joseph Chamochumbi's avatar Joseph Chamochumbi Committed by GitHub

docs: use navigation() to defer a cookie-based price from prefetch={true} in ISR guide

parent e32cb8e4
...@@ -178,8 +178,6 @@ export async function getPopularProducts(category: string) { ...@@ -178,8 +178,6 @@ export async function getPopularProducts(category: string) {
If your components access runtime APIs like `cookies` or `headers`, wrap them in `<Suspense>`. Their fallback UI is included in the static shell instead. If your components access runtime APIs like `cookies` or `headers`, wrap them in `<Suspense>`. Their fallback UI is included in the static shell instead.
{/* TODO: show how unstable_navigation() can help keep content out of the prefetch={true} */}
### At build time ### At build time
When you run `next build`, Next.js prerenders the layout for each known category (`tops`, `shorts`), plus one render where `await params` suspends, producing the App Shell for `[category]`. When you run `next build`, Next.js prerenders the layout for each known category (`tops`, `shorts`), plus one render where `await params` suspends, producing the App Shell for `[category]`.
...@@ -216,6 +214,64 @@ After the first visit, Next.js renders the page in the background with the known ...@@ -216,6 +214,64 @@ After the first visit, Next.js renders the page in the background with the known
- If all params are resolved but the render still hits uncached data or runtime APIs (`cookies`, `headers`) wrapped in `<Suspense>` boundaries, the upgrade produces a **cached page with those fallbacks**. The uncached or runtime parts stream in at request time. - If all params are resolved but the render still hits uncached data or runtime APIs (`cookies`, `headers`) wrapped in `<Suspense>` boundaries, the upgrade produces a **cached page with those fallbacks**. The uncached or runtime parts stream in at request time.
- Params are resolved in route order. A param value not returned by `generateStaticParams` stays unresolved and prevents any deeper params from upgrading. - Params are resolved in route order. A param value not returned by `generateStaticParams` stays unresolved and prevents any deeper params from upgrading.
### Deferring expensive content
Because links to these pages use `prefetch={true}`, each prefetch also prepares content that depends on `cookies` or `headers`. Consider showing each price in the visitor's currency. The currency comes from a cookie, and converting the price calls an exchange-rate service, so every prefetch of a product link would run that conversion. Use [`await navigation()`](/docs/app/api-reference/functions/navigation) before it. The prefetch leaves the converted price out, and it renders when the user navigates.
```tsx filename="app/[category]/[product]/page.tsx" highlight={1-2,4,6-24,32-34}
import { cookies } from 'next/headers'
import { navigation } from 'next/cache'
import { Suspense } from 'react'
import { getProduct, getExchangeRate } from '../../lib/data'
async function LocalPrice({
params,
}: Pick<PageProps<'/[category]/[product]'>, 'params'>) {
await navigation()
const { category, product } = await params
const currency = (await cookies()).get('currency')?.value ?? 'USD'
const [data, rate] = await Promise.all([
getProduct(category, product),
getExchangeRate(currency),
])
if (!data) return null
return (
<p>
{(data.price * rate).toFixed(2)} {currency}
</p>
)
}
export default function ProductPage(props: PageProps<'/[category]/[product]'>) {
return (
<div>
<Suspense fallback={<div>Loading product...</div>}>
<ProductDetails {...props} />
</Suspense>
<Suspense fallback={<p>Loading price...</p>}>
<LocalPrice params={props.params} />
</Suspense>
</div>
)
}
```
The exchange rate comes from a new function in `app/lib/data.ts`, cached per currency by the file's `'use cache'` directive:
```ts filename="app/lib/data.ts"
'use cache'
// ...other exported functions
export async function getExchangeRate(currency: string) {
const res = await fetch(`https://rates.example.com/latest?to=${currency}`)
const { rate } = await res.json()
return rate
}
```
## Choosing what to prerender ## Choosing what to prerender
Not every route needs to be prerendered. Every page you prerender increases build work and produces output that has to be stored and deployed. Many routes may never be visited before your next deployment, making that work unnecessary. Not every route needs to be prerendered. Every page you prerender increases build work and produces output that has to be stored and deployed. Many routes may never be visited before your next deployment, making that work unnecessary.
......
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