mirror of
https://github.com/vercel/commerce.git
synced 2025-03-14 22:42:33 +00:00
Co-authored-by: Andrew Jones <andrewj@corra.com> Co-authored-by: Lee Robinson <lrobinson2011@gmail.com>
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { getCollections, getPages, getProducts } from 'lib/shopify';
|
|
import { MetadataRoute } from 'next';
|
|
|
|
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
|
|
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
|
: 'http://localhost:3000';
|
|
|
|
export default async function sitemap(): Promise<Promise<Promise<MetadataRoute.Sitemap>>> {
|
|
const routesMap = [''].map((route) => ({
|
|
url: `${baseUrl}${route}`,
|
|
lastModified: new Date().toISOString()
|
|
}));
|
|
|
|
const collectionsPromise = getCollections().then((collections) =>
|
|
collections.map((collection) => ({
|
|
url: `${baseUrl}${collection.path}`,
|
|
lastModified: collection.updatedAt
|
|
}))
|
|
);
|
|
|
|
const productsPromise = getProducts({}).then((products) =>
|
|
products.map((product) => ({
|
|
url: `${baseUrl}/product/${product.handle}`,
|
|
lastModified: product.updatedAt
|
|
}))
|
|
);
|
|
|
|
const pagesPromise = getPages().then((pages) =>
|
|
pages.map((page) => ({
|
|
url: `${baseUrl}/${page.handle}`,
|
|
lastModified: page.updatedAt
|
|
}))
|
|
);
|
|
|
|
const fetchedRoutes = (
|
|
await Promise.all([collectionsPromise, productsPromise, pagesPromise])
|
|
).flat();
|
|
|
|
return [...routesMap, ...fetchedRoutes];
|
|
}
|