forked from crowetic/commerce
Adds better sitemap error handling (#1134)
* Adds better sitemap error handling * Removes extra `flat` --------- Co-authored-by: Lee Robinson <lrobinson2011@gmail.com>
This commit is contained in:
parent
71c9cb96fa
commit
1d5242eef3
@ -1,11 +1,16 @@
|
|||||||
import { getCollections, getPages, getProducts } from 'lib/shopify';
|
import { getCollections, getPages, getProducts } from 'lib/shopify';
|
||||||
import { MetadataRoute } from 'next';
|
import { MetadataRoute } from 'next';
|
||||||
|
|
||||||
|
type Route = {
|
||||||
|
url: string;
|
||||||
|
lastModified: string;
|
||||||
|
};
|
||||||
|
|
||||||
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
|
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
|
||||||
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
|
||||||
: 'http://localhost:3000';
|
: '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) => ({
|
const routesMap = [''].map((route) => ({
|
||||||
url: `${baseUrl}${route}`,
|
url: `${baseUrl}${route}`,
|
||||||
lastModified: new Date().toISOString()
|
lastModified: new Date().toISOString()
|
||||||
@ -32,9 +37,13 @@ export default async function sitemap(): Promise<Promise<Promise<MetadataRoute.S
|
|||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
|
|
||||||
const fetchedRoutes = (
|
let fetchedRoutes: Route[] = [];
|
||||||
await Promise.all([collectionsPromise, productsPromise, pagesPromise])
|
|
||||||
).flat();
|
try {
|
||||||
|
fetchedRoutes = (await Promise.all([collectionsPromise, productsPromise, pagesPromise])).flat();
|
||||||
|
} catch (error) {
|
||||||
|
throw JSON.stringify(error, null, 2);
|
||||||
|
}
|
||||||
|
|
||||||
return [...routesMap, ...fetchedRoutes];
|
return [...routesMap, ...fetchedRoutes];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user