63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import { MetadataRoute } from "next"
|
|
import { listProducts } from "@lib/data/products"
|
|
import { listCollections } from "@lib/data/collections"
|
|
import { listCategories } from "@lib/data/categories"
|
|
import { getBaseURL } from "@lib/util/env"
|
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
const baseUrl = getBaseURL()
|
|
const defaultCountry = process.env.NEXT_PUBLIC_DEFAULT_REGION || "us"
|
|
|
|
const routes = [
|
|
"",
|
|
"/store",
|
|
"/cart",
|
|
"/account",
|
|
"/wishlist",
|
|
].map((route) => ({
|
|
url: `${baseUrl}/${defaultCountry}${route}`,
|
|
lastModified: new Date(),
|
|
changeFrequency: "daily" as const,
|
|
priority: route === "" ? 1.0 : 0.8,
|
|
}))
|
|
|
|
try {
|
|
// Fetch products
|
|
const { response } = await listProducts({
|
|
countryCode: defaultCountry,
|
|
queryParams: { limit: 100, fields: "handle,updated_at" }
|
|
})
|
|
const productRoutes = (response.products || []).map((product) => ({
|
|
url: `${baseUrl}/${defaultCountry}/products/${product.handle}`,
|
|
lastModified: product.updated_at ? new Date(product.updated_at) : new Date(),
|
|
changeFrequency: "weekly" as const,
|
|
priority: 0.7,
|
|
}))
|
|
|
|
// Fetch collections
|
|
const { collections } = await listCollections({
|
|
fields: "handle,updated_at"
|
|
})
|
|
const collectionRoutes = (collections || []).map((collection) => ({
|
|
url: `${baseUrl}/${defaultCountry}/collections/${collection.handle}`,
|
|
lastModified: collection.updated_at ? new Date(collection.updated_at) : new Date(),
|
|
changeFrequency: "weekly" as const,
|
|
priority: 0.6,
|
|
}))
|
|
|
|
// Fetch categories
|
|
const categories = await listCategories()
|
|
const categoryRoutes = (categories || []).map((category: any) => ({
|
|
url: `${baseUrl}/${defaultCountry}/categories/${category.handle}`,
|
|
lastModified: category.updated_at ? new Date(category.updated_at) : new Date(),
|
|
changeFrequency: "weekly" as const,
|
|
priority: 0.6,
|
|
}))
|
|
|
|
return [...routes, ...productRoutes, ...collectionRoutes, ...categoryRoutes]
|
|
} catch (error) {
|
|
console.error("Failed to generate sitemap dynamically:", error)
|
|
return routes
|
|
}
|
|
}
|