2023-04-23 13:55:25 -05:00
|
|
|
import { getCollections, getPages, getProducts } from 'lib/shopify';
|
2023-10-02 10:18:56 -05:00
|
|
|
import { validateEnvironmentVariables } from 'lib/utils';
|
2023-04-23 13:55:25 -05:00
|
|
|
import { MetadataRoute } from 'next';
|
|
|
|
|
2023-08-01 20:34:45 -05:00
|
|
|
type Route = {
|
|
|
|
url: string;
|
|
|
|
lastModified: string;
|
|
|
|
};
|
|
|
|
|
2023-04-23 13:55:25 -05:00
|
|
|
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
|
|
|
|
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
|
|
|
: 'http://localhost:3000';
|
|
|
|
|
2024-04-17 21:54:09 -05:00
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
|
2023-08-01 20:34:45 -05:00
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
2023-10-02 10:18:56 -05:00
|
|
|
validateEnvironmentVariables();
|
|
|
|
|
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
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
|
2023-08-01 20:34:45 -05:00
|
|
|
let fetchedRoutes: Route[] = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
fetchedRoutes = (await Promise.all([collectionsPromise, productsPromise, pagesPromise])).flat();
|
|
|
|
} catch (error) {
|
|
|
|
throw JSON.stringify(error, null, 2);
|
|
|
|
}
|
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
|
|
|
}
|