108 lines
4.5 KiB
JavaScript
108 lines
4.5 KiB
JavaScript
import { buildLlmsTxt } from './buildLlmsTxt.js';
|
|
import { buildSitemapXml } from './buildSitemapXml.js';
|
|
import { buildSitemapCss, SITEMAP_STYLE_PARAM } from './sitemapStyle.js';
|
|
/**
|
|
* Ready-made Next route handler for /llms.txt. Replaces the ~60-line try/catch
|
|
* every project was copying. buildLlmsTxt already handles collections without
|
|
* drafts, so no workaround is needed here.
|
|
*
|
|
* // app/llms.txt/route.ts
|
|
* import { createLlmsTxtRoute } from '@intecion/ipal-kit'
|
|
* import { getCachedPayload } from '@/lib/content'
|
|
* import { i18nConfig } from '@/i18n.config'
|
|
* export const dynamic = 'force-dynamic'
|
|
* export const GET = createLlmsTxtRoute({ getCachedPayload, config: i18nConfig,
|
|
* baseUrl: process.env.NEXT_PUBLIC_SERVER_URL! })
|
|
*
|
|
* For the full-content variant, add a second route with `full: true`:
|
|
*
|
|
* // app/llms-full.txt/route.ts
|
|
* export const GET = createLlmsTxtRoute({ getCachedPayload, config: i18nConfig,
|
|
* baseUrl: process.env.NEXT_PUBLIC_SERVER_URL!, full: true })
|
|
*/ export function createLlmsTxtRoute(opts) {
|
|
return async ()=>{
|
|
try {
|
|
const payload = await opts.getCachedPayload();
|
|
const body = await buildLlmsTxt({
|
|
baseUrl: opts.baseUrl,
|
|
config: opts.config,
|
|
full: opts.full,
|
|
pagesSlug: opts.pagesSlug,
|
|
payload,
|
|
settingsSlug: opts.settingsSlug,
|
|
summaryField: opts.summaryField
|
|
});
|
|
return new Response(body, {
|
|
headers: {
|
|
'Content-Type': 'text/plain; charset=utf-8'
|
|
}
|
|
});
|
|
} catch (err) {
|
|
// DB unreachable at build (container) — return a minimal valid file so the
|
|
// route doesn't 500; it fills in at runtime. No per-project try/catch.
|
|
console.warn('[ipal] createLlmsTxtRoute: could not build llms.txt:', err);
|
|
return new Response('# llms.txt\n', {
|
|
headers: {
|
|
'Content-Type': 'text/plain; charset=utf-8'
|
|
}
|
|
});
|
|
}
|
|
};
|
|
}
|
|
/**
|
|
* Ready-made Next route handler for /sitemap.xml, styled for humans out of the box.
|
|
* Usually you don't call this — `createContentHelpers` returns `sitemapRoute`:
|
|
*
|
|
* // app/sitemap.xml/route.ts — the whole file
|
|
* export { sitemapRoute as GET } from '@/lib/content'
|
|
* export const dynamic = 'force-dynamic'
|
|
*
|
|
* `GET /sitemap.xml` → XML (crawlers), `GET /sitemap.xml?style` → its CSS.
|
|
* Don't keep app/sitemap.ts next to it — two sitemaps confuse crawlers.
|
|
*/ export function createSitemapRouteHandler(sitemap, opts = {}) {
|
|
const style = opts.cssUrl ? false : opts.style === false ? false : opts.style ?? {};
|
|
return async (req)=>{
|
|
const url = req ? new URL(req.url) : null;
|
|
if (style && url?.searchParams.has(SITEMAP_STYLE_PARAM)) {
|
|
let title;
|
|
try {
|
|
title = typeof style.title === 'function' ? await style.title() : style.title;
|
|
} catch {
|
|
title = undefined; // DB down → generic heading, never a broken stylesheet
|
|
}
|
|
return new Response(buildSitemapCss({
|
|
...style,
|
|
title
|
|
}), {
|
|
headers: {
|
|
'Cache-Control': 'public, max-age=3600',
|
|
'Content-Type': 'text/css; charset=utf-8',
|
|
'X-Robots-Tag': 'noindex'
|
|
}
|
|
});
|
|
}
|
|
const cssUrl = opts.cssUrl ?? (style ? `${url?.pathname ?? '/sitemap.xml'}?${SITEMAP_STYLE_PARAM}` : undefined);
|
|
try {
|
|
const entries = await sitemap();
|
|
const xml = buildSitemapXml(entries, {
|
|
cssUrl
|
|
});
|
|
return new Response(xml, {
|
|
headers: {
|
|
'Content-Type': 'application/xml; charset=utf-8'
|
|
}
|
|
});
|
|
} catch (err) {
|
|
console.warn('[ipal] createSitemapRouteHandler: could not build sitemap:', err);
|
|
// Valid empty urlset so the route doesn't 500 during a DB-less build.
|
|
const empty = '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>';
|
|
return new Response(empty, {
|
|
headers: {
|
|
'Content-Type': 'application/xml; charset=utf-8'
|
|
}
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=routeHandlers.js.map
|