2023-04-23 13:55:25 -05:00
|
|
|
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>>> {
|
2023-05-09 22:18:01 -04:00
|
|
|
const routesMap = [''].map((route) => ({
|
2023-04-23 13:55:25 -05:00
|
|
|
url: `${baseUrl}${route}`,
|
|
|
|
lastModified: new Date().toISOString()
|
|
|
|
}));
|
|
|
|
|
2023-05-09 22:18:01 -04:00
|
|
|
const collectionsPromise = getCollections().then((collections) =>
|
|
|
|
collections.map((collection) => ({
|
|
|
|
url: `${baseUrl}${collection.path}`,
|
|
|
|
lastModified: collection.updatedAt
|
|
|
|
}))
|
|
|
|
);
|
2023-04-23 13:55:25 -05:00
|
|
|
|
2023-05-09 22:18:01 -04:00
|
|
|
const productsPromise = getProducts({}).then((products) =>
|
|
|
|
products.map((product) => ({
|
|
|
|
url: `${baseUrl}/product/${product.handle}`,
|
|
|
|
lastModified: product.updatedAt
|
|
|
|
}))
|
|
|
|
);
|
2023-04-23 13:55:25 -05:00
|
|
|
|
2023-05-09 22:18:01 -04:00
|
|
|
const pagesPromise = getPages().then((pages) =>
|
|
|
|
pages.map((page) => ({
|
|
|
|
url: `${baseUrl}/${page.handle}`,
|
|
|
|
lastModified: page.updatedAt
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
|
|
|
|
const fetchedRoutes = (
|
|
|
|
await Promise.all([collectionsPromise, productsPromise, pagesPromise])
|
|
|
|
).flat();
|
2023-04-23 13:55:25 -05:00
|
|
|
|
2023-05-09 22:18:01 -04:00
|
|
|
return [...routesMap, ...fetchedRoutes];
|
2023-04-23 13:55:25 -05:00
|
|
|
}
|