Unverified Commit 8bfa443b authored by Joseph's avatar Joseph Committed by GitHub

docs: generateSitemap passes id as promise (#85767)

parent 49933c2e
...@@ -24,6 +24,7 @@ Your generated sitemaps will be available at `/.../sitemap/[id].xml`. For exampl ...@@ -24,6 +24,7 @@ Your generated sitemaps will be available at `/.../sitemap/[id].xml`. For exampl
For example, to split a sitemap using `generateSitemaps`, return an array of objects with the sitemap `id`. Then, use the `id` to generate the unique sitemaps. For example, to split a sitemap using `generateSitemaps`, return an array of objects with the sitemap `id`. Then, use the `id` to generate the unique sitemaps.
```ts filename="app/product/sitemap.ts" switcher ```ts filename="app/product/sitemap.ts" switcher
import type { MetadataRoute } from 'next'
import { BASE_URL } from '@/app/lib/constants' import { BASE_URL } from '@/app/lib/constants'
export async function generateSitemaps() { export async function generateSitemaps() {
...@@ -31,11 +32,10 @@ export async function generateSitemaps() { ...@@ -31,11 +32,10 @@ export async function generateSitemaps() {
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }] return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
} }
export default async function sitemap({ export default async function sitemap(props: {
id, id: Promise<number>
}: {
id: number
}): Promise<MetadataRoute.Sitemap> { }): Promise<MetadataRoute.Sitemap> {
const id = await props.id
// Google's limit is 50,000 URLs per sitemap // Google's limit is 50,000 URLs per sitemap
const start = id * 50000 const start = id * 50000
const end = start + 50000 const end = start + 50000
...@@ -57,7 +57,8 @@ export async function generateSitemaps() { ...@@ -57,7 +57,8 @@ export async function generateSitemaps() {
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }] return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
} }
export default async function sitemap({ id }) { export default async function sitemap(props) {
const id = await props.id
// Google's limit is 50,000 URLs per sitemap // Google's limit is 50,000 URLs per sitemap
const start = id * 50000 const start = id * 50000
const end = start + 50000 const end = start + 50000
...@@ -65,7 +66,7 @@ export default async function sitemap({ id }) { ...@@ -65,7 +66,7 @@ export default async function sitemap({ id }) {
`SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}` `SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
) )
return products.map((product) => ({ return products.map((product) => ({
url: `${BASE_URL}/product/${id}`, url: `${BASE_URL}/product/${product.id}`,
lastModified: product.date, lastModified: product.date,
})) }))
} }
...@@ -75,5 +76,6 @@ export default async function sitemap({ id }) { ...@@ -75,5 +76,6 @@ export default async function sitemap({ id }) {
| Version | Changes | | Version | Changes |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `v16.0.0` | The `id` values returned from `generateSitemaps` are now passed as a promise to the sitemap function. |
| `v15.0.0` | `generateSitemaps` now generates consistent URLs between development and production | | `v15.0.0` | `generateSitemaps` now generates consistent URLs between development and production |
| `v13.3.2` | `generateSitemaps` introduced. In development, you can view the generated sitemap on `/.../sitemap.xml/[id]`. For example, `/product/sitemap.xml/1`. | | `v13.3.2` | `generateSitemaps` introduced. In development, you can view the generated sitemap on `/.../sitemap.xml/[id]`. For example, `/product/sitemap.xml/1`. |
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