Skip to content

Commit

Permalink
Adds better sitemap error handling (vercel#1134)
Browse files Browse the repository at this point in the history
* Adds better sitemap error handling

* Removes extra `flat`

---------

Co-authored-by: Lee Robinson <[email protected]>
  • Loading branch information
manovotny and leerob authored Aug 2, 2023
1 parent 71c9cb9 commit 1d5242e
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { getCollections, getPages, getProducts } from 'lib/shopify';
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<Promise<Promise<MetadataRoute.Sitemap>>> {
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const routesMap = [''].map((route) => ({
url: `${baseUrl}${route}`,
lastModified: new Date().toISOString()
Expand All @@ -32,9 +37,13 @@ export default async function sitemap(): Promise<Promise<Promise<MetadataRoute.S
}))
);

const fetchedRoutes = (
await Promise.all([collectionsPromise, productsPromise, pagesPromise])
).flat();
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 1d5242e

Please sign in to comment.