mirror of
https://github.com/vercel/commerce.git
synced 2025-05-08 18:57:51 +00:00
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { getCollectionProducts, getCollections } from 'lib/fourthwall';
|
|
import { validateEnvironmentVariables } from 'lib/utils';
|
|
import { MetadataRoute } from 'next';
|
|
|
|
type Route = {
|
|
url: string;
|
|
lastModified: string;
|
|
};
|
|
|
|
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
|
|
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
|
: 'http://localhost:3000';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
validateEnvironmentVariables();
|
|
|
|
const routesMap = [''].map((route) => ({
|
|
url: `${baseUrl}${route}`,
|
|
lastModified: new Date().toISOString()
|
|
}));
|
|
|
|
const collections = await getCollections();
|
|
|
|
const productsPromises = collections.map((collection) => {
|
|
return getCollectionProducts({ collection: collection.handle, currency: 'USD' }).then((products) =>
|
|
products.map((product) => ({
|
|
url: `${baseUrl}/product/${product.handle}`,
|
|
lastModified: product.updatedAt
|
|
}))
|
|
);
|
|
});
|
|
|
|
let fetchedRoutes: Route[] = [];
|
|
|
|
try {
|
|
fetchedRoutes = (await Promise.all([...productsPromises])).flat();
|
|
} catch (error) {
|
|
throw JSON.stringify(error, null, 2);
|
|
}
|
|
|
|
return [...routesMap, ...fetchedRoutes];
|
|
}
|