generateSitemaps
您可以使用 generateSitemaps
函式為您的應用程式產生多個 Sitemap。
回傳值
generateSitemaps
會回傳一個物件陣列,每個物件都具有 id
屬性。
網址
在正式環境中,您產生的 Sitemap 位於 /.../sitemap/[id].xml
。例如,/product/sitemap/1.xml
。
在開發環境中,您可以在 /.../sitemap.xml/[id]
查看產生的 Sitemap。例如,/product/sitemap.xml/1
。這個差異是暫時的,未來將會遵循正式環境的格式。
範例
例如,要使用 generateSitemaps
拆分 Sitemap,請回傳一個包含 Sitemap id
的物件陣列。然後,使用 id
產生唯一的 Sitemap。
app/product/sitemap.ts
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,
}))
}
這有幫助嗎?