Skip to content

Commit

Permalink
Update sitemap.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Uwinfly committed Apr 2, 2024
1 parent 610b0e8 commit d10037a
Showing 1 changed file with 0 additions and 51 deletions.
51 changes: 0 additions & 51 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1 @@
import { getCollections, getPages, getProducts } from 'lib/shopify';
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 default async function sitemap(): Promise<MetadataRoute.Sitemap> {
validateEnvironmentVariables();

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
}))
);

let fetchedRoutes: Route[] = [];

try {
fetchedRoutes = (await Promise.all([collectionsPromise, productsPromise, pagesPromise])).flat();
} catch (error) {
throw JSON.stringify(error, null, 2);
}

return [...routesMap, ...fetchedRoutes];
}

0 comments on commit d10037a

Please sign in to comment.