import { getCategories, getProducts } from 'lib/medusa'; 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 default async function sitemap(): Promise { const routesMap = [''].map((route) => ({ url: `${baseUrl}${route}`, lastModified: new Date().toISOString() })); const collections = await getCategories(); const collectionsPromise = collections.map((collection) => ({ url: `${baseUrl}${collection.path}`, lastModified: collection.updatedAt.toString() })); const productsPromise = getProducts({}).then((products) => products.map((product) => ({ url: `${baseUrl}/product/${product.handle}`, lastModified: product.updatedAt.toString() })) ); let fetchedRoutes: Route[] = []; try { fetchedRoutes = (await Promise.all([collectionsPromise, productsPromise])).flat(); } catch (error) { throw JSON.stringify(error, null, 2); } return [...routesMap, ...fetchedRoutes]; }