Unverified Commit 9238666d authored by Joseph Chamochumbi's avatar Joseph Chamochumbi Committed by GitHub

docs: move the local price into ProductDetails in ISR guide

parent d6fe60a2
...@@ -218,46 +218,49 @@ After the first visit, Next.js renders the page in the background with the known ...@@ -218,46 +218,49 @@ After the first visit, Next.js renders the page in the background with the known
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. 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} ```tsx filename="app/[category]/[product]/page.tsx" highlight={3-5,7-16,32-34}
import { Suspense } from 'react'
import Link from 'next/link'
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { navigation } from 'next/cache' import { navigation } from 'next/cache'
import { Suspense } from 'react'
import { getProduct, getExchangeRate } from '../../lib/data' import { getProduct, getExchangeRate } from '../../lib/data'
async function LocalPrice({ async function LocalPrice({ price }: { price: number }) {
params,
}: Pick<PageProps<'/[category]/[product]'>, 'params'>) {
await navigation() await navigation()
const { category, product } = await params
const currency = (await cookies()).get('currency')?.value ?? 'USD' const currency = (await cookies()).get('currency')?.value ?? 'USD'
const [data, rate] = await Promise.all([ const rate = await getExchangeRate(currency)
getProduct(category, product),
getExchangeRate(currency),
])
if (!data) return null
return ( return (
<p> <p>
{(data.price * rate).toFixed(2)} {currency} {(price * rate).toFixed(2)} {currency}
</p> </p>
) )
} }
export default function ProductPage(props: PageProps<'/[category]/[product]'>) { async function ProductDetails(props: PageProps<'/[category]/[product]'>) {
const { category, product } = await props.params
const data = await getProduct(category, product)
if (!data) {
return <p>Product not found.</p>
}
return ( return (
<div> <>
<Suspense fallback={<div>Loading product...</div>}> <Link href={`/${category}`} prefetch={true}>
<ProductDetails {...props} /> &larr; Back to products
</Suspense> </Link>
<h2>{data.name}</h2>
<Suspense fallback={<p>Loading price...</p>}> <Suspense fallback={<p>Loading price...</p>}>
<LocalPrice params={props.params} /> <LocalPrice price={data.price} />
</Suspense> </Suspense>
</div> <p>{data.description}</p>
</>
) )
} }
``` ```
The prefetch still includes the product name and description. The converted price renders when the user navigates.
The exchange rate comes from a new function in `app/lib/data.ts`, cached per currency by the file's `'use cache'` directive: 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" ```ts filename="app/lib/data.ts"
......
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