跳到主要内容

generateSitemaps

你可以使用 generateSitemaps 函数为你的应用生成多个站点地图。

¥You can use the generateSitemaps function to generate multiple sitemaps for your application.

返回

¥Returns

generateSitemaps 返回具有 id 属性的对象数组。

¥The generateSitemaps returns an array of objects with an id property.

URLs

你生成的站点地图将在 /.../sitemap/[id].xml 上可用。例如,/product/sitemap/1.xml

¥Your generated sitemaps will be available at /.../sitemap/[id].xml. For example, /product/sitemap/1.xml.

示例

¥Example

例如,要使用 generateSitemaps 分割站点地图,请返回包含站点地图 id 的对象数组。然后,使用 id 生成唯一的站点地图。

¥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.

import { BASE_URL } from '@/app/lib/constants'

export async function generateSitemaps() {
// Fetch the total number of products and calculate the number of sitemaps needed
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
}

export default async function sitemap({
id,
}: {
id: number
}): Promise<MetadataRoute.Sitemap> {
// Google's limit is 50,000 URLs per sitemap
const start = id * 50000
const end = start + 50000
const products = await getProducts(
`SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
)
return products.map((product) => ({
url: `${BASE_URL}/product/${product.id}`,
lastModified: product.date,
}))
}
import { BASE_URL } from '@/app/lib/constants'

export async function generateSitemaps() {
// Fetch the total number of products and calculate the number of sitemaps needed
return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
}

export default async function sitemap({ id }) {
// Google's limit is 50,000 URLs per sitemap
const start = id * 50000
const end = start + 50000
const products = await getProducts(
`SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
)
return products.map((product) => ({
url: `${BASE_URL}/product/${id}`,
lastModified: product.date,
}))
}

版本历史

¥Version History

版本变化
v15.0.0generateSitemaps 现在在开发和生产之间生成一致的 URL
v13.3.2generateSitemaps 推出。开发中,你可以在 /.../sitemap.xml/[id] 上查看生成的站点地图。例如,/product/sitemap.xml/1