seo: always serve sitemap.xml even if DB unavailable
Co-authored-by: dennis <dennis@konkol.net>
This commit is contained in:
@@ -12,8 +12,8 @@ export async function GET() {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error generating sitemap.xml:", error);
|
console.error("Error generating sitemap.xml:", error);
|
||||||
|
// Always return a valid sitemap with 200 so crawlers don't treat it as broken.
|
||||||
return new NextResponse(generateSitemapXml([]), {
|
return new NextResponse(generateSitemapXml([]), {
|
||||||
status: 500,
|
|
||||||
headers: { "Content-Type": "application/xml" },
|
headers: { "Content-Type": "application/xml" },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,11 +58,15 @@ export async function getSitemapEntries(): Promise<SitemapEntry[]> {
|
|||||||
orderBy: { updatedAt: "desc" },
|
orderBy: { updatedAt: "desc" },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If DB isn't ready/migrated yet, still serve a valid sitemap for static pages.
|
// If DB isn't ready/migrated/reachable yet, still serve a valid sitemap for static pages.
|
||||||
|
if (process.env.NODE_ENV === "development") {
|
||||||
|
console.warn("Sitemap: failed to load projects; serving static entries only.", error);
|
||||||
|
}
|
||||||
if (error instanceof PrismaClientKnownRequestError && (error.code === "P2021" || error.code === "P2022")) {
|
if (error instanceof PrismaClientKnownRequestError && (error.code === "P2021" || error.code === "P2022")) {
|
||||||
return staticEntries;
|
return staticEntries;
|
||||||
}
|
}
|
||||||
throw error;
|
// Also fail soft on connection/init errors in dev/staging (keeps sitemap valid for crawlers)
|
||||||
|
return staticEntries;
|
||||||
}
|
}
|
||||||
|
|
||||||
const projectEntries: SitemapEntry[] = projects.flatMap((p) => {
|
const projectEntries: SitemapEntry[] = projects.flatMap((p) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user