Rebuilded sitemap generator
This commit is contained in:
+14
-16
@@ -1,33 +1,31 @@
|
||||
import type { SitemapEntry } from './buildSitemapEntries.js';
|
||||
/**
|
||||
* Serializes sitemap entries to an XML STRING with an XSL stylesheet reference,
|
||||
* so /sitemap.xml renders as a readable table in the browser (not raw XML) while
|
||||
* staying a valid sitemap for crawlers.
|
||||
* Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers
|
||||
* and readable in the browser as the native XML tree (indented, collapsible,
|
||||
* syntax-highlighted by the browser itself).
|
||||
*
|
||||
* Why this exists alongside the Next MetadataRoute sitemap: Next's app/sitemap.ts
|
||||
* (returning SitemapEntry[]) does NOT let you inject <?xml-stylesheet?>. To get
|
||||
* the styled table, serve a custom route that returns this string instead:
|
||||
* NO XSLT stylesheet: browsers (Chrome et al.) are REMOVING XSLT support, so a
|
||||
* <?xml-stylesheet?> approach is a dead end — it shows a deprecation warning now
|
||||
* and will break soon. Instead we serve plain XML with the correct Content-Type;
|
||||
* the browser renders its built-in formatted XML view (the "code tree" look) with
|
||||
* no transformation needed.
|
||||
*
|
||||
* // app/sitemap.xml/route.ts
|
||||
* import { buildSitemapXml } from '@intecion/ipal-kit'
|
||||
* import { sitemap } from '@/lib/content' // your entries source
|
||||
* import { sitemap } from '@/lib/content'
|
||||
* export const dynamic = 'force-dynamic'
|
||||
* export async function GET() {
|
||||
* const entries = await sitemap()
|
||||
* const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' })
|
||||
* const xml = buildSitemapXml(entries)
|
||||
* return new Response(xml, {
|
||||
* headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* Put sitemap.xsl in the project's /public (copy from the plugin's assets, or
|
||||
* serve it from a route). The <?xml-stylesheet?> points browsers at it; crawlers
|
||||
* ignore it and read the XML. Include hreflang alternates as <xhtml:link>.
|
||||
* The indentation makes the raw XML pleasant to read; the browser's default XML
|
||||
* viewer adds the tree/fold UI. Crawlers read the XML as usual.
|
||||
*
|
||||
* NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one
|
||||
* (the styled route OR the Next MetadataRoute). Two sitemaps at different paths
|
||||
* confuse crawlers.
|
||||
* (this route OR the Next MetadataRoute). Two sitemaps confuse crawlers.
|
||||
*/
|
||||
export declare function buildSitemapXml(entries: SitemapEntry[], opts?: {
|
||||
stylesheetUrl?: string;
|
||||
}): string;
|
||||
export declare function buildSitemapXml(entries: SitemapEntry[]): string;
|
||||
|
||||
Vendored
+21
-20
@@ -1,33 +1,32 @@
|
||||
/**
|
||||
* Serializes sitemap entries to an XML STRING with an XSL stylesheet reference,
|
||||
* so /sitemap.xml renders as a readable table in the browser (not raw XML) while
|
||||
* staying a valid sitemap for crawlers.
|
||||
* Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers
|
||||
* and readable in the browser as the native XML tree (indented, collapsible,
|
||||
* syntax-highlighted by the browser itself).
|
||||
*
|
||||
* Why this exists alongside the Next MetadataRoute sitemap: Next's app/sitemap.ts
|
||||
* (returning SitemapEntry[]) does NOT let you inject <?xml-stylesheet?>. To get
|
||||
* the styled table, serve a custom route that returns this string instead:
|
||||
* NO XSLT stylesheet: browsers (Chrome et al.) are REMOVING XSLT support, so a
|
||||
* <?xml-stylesheet?> approach is a dead end — it shows a deprecation warning now
|
||||
* and will break soon. Instead we serve plain XML with the correct Content-Type;
|
||||
* the browser renders its built-in formatted XML view (the "code tree" look) with
|
||||
* no transformation needed.
|
||||
*
|
||||
* // app/sitemap.xml/route.ts
|
||||
* import { buildSitemapXml } from '@intecion/ipal-kit'
|
||||
* import { sitemap } from '@/lib/content' // your entries source
|
||||
* import { sitemap } from '@/lib/content'
|
||||
* export const dynamic = 'force-dynamic'
|
||||
* export async function GET() {
|
||||
* const entries = await sitemap()
|
||||
* const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' })
|
||||
* const xml = buildSitemapXml(entries)
|
||||
* return new Response(xml, {
|
||||
* headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
||||
* })
|
||||
* }
|
||||
*
|
||||
* Put sitemap.xsl in the project's /public (copy from the plugin's assets, or
|
||||
* serve it from a route). The <?xml-stylesheet?> points browsers at it; crawlers
|
||||
* ignore it and read the XML. Include hreflang alternates as <xhtml:link>.
|
||||
* The indentation makes the raw XML pleasant to read; the browser's default XML
|
||||
* viewer adds the tree/fold UI. Crawlers read the XML as usual.
|
||||
*
|
||||
* NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one
|
||||
* (the styled route OR the Next MetadataRoute). Two sitemaps at different paths
|
||||
* confuse crawlers.
|
||||
*/ export function buildSitemapXml(entries, opts = {}) {
|
||||
const { stylesheetUrl } = opts;
|
||||
* (this route OR the Next MetadataRoute). Two sitemaps confuse crawlers.
|
||||
*/ export function buildSitemapXml(entries) {
|
||||
const esc = (s)=>s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
||||
const urls = entries.map((e)=>{
|
||||
const parts = [
|
||||
@@ -37,9 +36,12 @@
|
||||
const iso = e.lastModified instanceof Date ? e.lastModified.toISOString() : String(e.lastModified);
|
||||
parts.push(` <lastmod>${esc(iso)}</lastmod>`);
|
||||
}
|
||||
if (e.changeFrequency) parts.push(` <changefreq>${e.changeFrequency}</changefreq>`);
|
||||
if (typeof e.priority === 'number') parts.push(` <priority>${e.priority}</priority>`);
|
||||
// hreflang alternates
|
||||
if (e.changeFrequency) {
|
||||
parts.push(` <changefreq>${e.changeFrequency}</changefreq>`);
|
||||
}
|
||||
if (typeof e.priority === 'number') {
|
||||
parts.push(` <priority>${e.priority}</priority>`);
|
||||
}
|
||||
const alternates = e.alternates?.languages;
|
||||
if (alternates) {
|
||||
for (const [lang, href] of Object.entries(alternates)){
|
||||
@@ -50,8 +52,7 @@
|
||||
}
|
||||
return ` <url>\n${parts.join('\n')}\n </url>`;
|
||||
}).join('\n');
|
||||
const stylesheet = stylesheetUrl ? `<?xml-stylesheet type="text/xsl" href="${esc(stylesheetUrl)}"?>\n` : '';
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>\n` + stylesheet + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ` + `xmlns:xhtml="http://www.w3.org/1999/xhtml">\n` + urls + `\n</urlset>`;
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>\n` + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ` + `xmlns:xhtml="http://www.w3.org/1999/xhtml">\n` + urls + `\n</urlset>`;
|
||||
}
|
||||
|
||||
//# sourceMappingURL=buildSitemapXml.js.map
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"sources":["../../../src/modules/seo/buildSitemapXml.ts"],"sourcesContent":["import type { SitemapEntry } from './buildSitemapEntries.js'\n\n/**\n * Serializes sitemap entries to an XML STRING with an XSL stylesheet reference,\n * so /sitemap.xml renders as a readable table in the browser (not raw XML) while\n * staying a valid sitemap for crawlers.\n *\n * Why this exists alongside the Next MetadataRoute sitemap: Next's app/sitemap.ts\n * (returning SitemapEntry[]) does NOT let you inject <?xml-stylesheet?>. To get\n * the styled table, serve a custom route that returns this string instead:\n *\n * // app/sitemap.xml/route.ts\n * import { buildSitemapXml } from '@intecion/ipal-kit'\n * import { sitemap } from '@/lib/content' // your entries source\n * export const dynamic = 'force-dynamic'\n * export async function GET() {\n * const entries = await sitemap()\n * const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' })\n * return new Response(xml, {\n * headers: { 'Content-Type': 'application/xml; charset=utf-8' },\n * })\n * }\n *\n * Put sitemap.xsl in the project's /public (copy from the plugin's assets, or\n * serve it from a route). The <?xml-stylesheet?> points browsers at it; crawlers\n * ignore it and read the XML. Include hreflang alternates as <xhtml:link>.\n *\n * NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one\n * (the styled route OR the Next MetadataRoute). Two sitemaps at different paths\n * confuse crawlers.\n */\nexport function buildSitemapXml(\n entries: SitemapEntry[],\n opts: { stylesheetUrl?: string } = {},\n): string {\n const { stylesheetUrl } = opts\n\n const esc = (s: string): string =>\n s\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''')\n\n const urls = entries\n .map((e) => {\n const parts = [` <loc>${esc(e.url)}</loc>`]\n if (e.lastModified) {\n const iso =\n e.lastModified instanceof Date ? e.lastModified.toISOString() : String(e.lastModified)\n parts.push(` <lastmod>${esc(iso)}</lastmod>`)\n }\n if (e.changeFrequency) parts.push(` <changefreq>${e.changeFrequency}</changefreq>`)\n if (typeof e.priority === 'number') parts.push(` <priority>${e.priority}</priority>`)\n // hreflang alternates\n const alternates = e.alternates?.languages\n if (alternates) {\n for (const [lang, href] of Object.entries(alternates)) {\n if (typeof href === 'string') {\n parts.push(\n ` <xhtml:link rel=\"alternate\" hreflang=\"${esc(lang)}\" href=\"${esc(href)}\"/>`,\n )\n }\n }\n }\n return ` <url>\\n${parts.join('\\n')}\\n </url>`\n })\n .join('\\n')\n\n const stylesheet = stylesheetUrl\n ? `<?xml-stylesheet type=\"text/xsl\" href=\"${esc(stylesheetUrl)}\"?>\\n`\n : ''\n\n return (\n `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n` +\n stylesheet +\n `<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ` +\n `xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\\n` +\n urls +\n `\\n</urlset>`\n )\n}\n"],"names":["buildSitemapXml","entries","opts","stylesheetUrl","esc","s","replace","urls","map","e","parts","url","lastModified","iso","Date","toISOString","String","push","changeFrequency","priority","alternates","languages","lang","href","Object","join","stylesheet"],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4BC,GACD,OAAO,SAASA,gBACdC,OAAuB,EACvBC,OAAmC,CAAC,CAAC;IAErC,MAAM,EAAEC,aAAa,EAAE,GAAGD;IAE1B,MAAME,MAAM,CAACC,IACXA,EACGC,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6C,CAAC,GAC/CnB,OACA,CAAC,WAAW,CAAC;AAEjB"}
|
||||
{"version":3,"sources":["../../../src/modules/seo/buildSitemapXml.ts"],"sourcesContent":["import type { SitemapEntry } from './buildSitemapEntries.js'\n\n/**\n * Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers\n * and readable in the browser as the native XML tree (indented, collapsible,\n * syntax-highlighted by the browser itself).\n *\n * NO XSLT stylesheet: browsers (Chrome et al.) are REMOVING XSLT support, so a\n * <?xml-stylesheet?> approach is a dead end — it shows a deprecation warning now\n * and will break soon. Instead we serve plain XML with the correct Content-Type;\n * the browser renders its built-in formatted XML view (the \"code tree\" look) with\n * no transformation needed.\n *\n * // app/sitemap.xml/route.ts\n * import { buildSitemapXml } from '@intecion/ipal-kit'\n * import { sitemap } from '@/lib/content'\n * export const dynamic = 'force-dynamic'\n * export async function GET() {\n * const entries = await sitemap()\n * const xml = buildSitemapXml(entries)\n * return new Response(xml, {\n * headers: { 'Content-Type': 'application/xml; charset=utf-8' },\n * })\n * }\n *\n * The indentation makes the raw XML pleasant to read; the browser's default XML\n * viewer adds the tree/fold UI. Crawlers read the XML as usual.\n *\n * NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one\n * (this route OR the Next MetadataRoute). Two sitemaps confuse crawlers.\n */\nexport function buildSitemapXml(entries: SitemapEntry[]): string {\n const esc = (s: string): string =>\n s\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''')\n\n const urls = entries\n .map((e) => {\n const parts = [` <loc>${esc(e.url)}</loc>`]\n if (e.lastModified) {\n const iso =\n e.lastModified instanceof Date ? e.lastModified.toISOString() : String(e.lastModified)\n parts.push(` <lastmod>${esc(iso)}</lastmod>`)\n }\n if (e.changeFrequency) {parts.push(` <changefreq>${e.changeFrequency}</changefreq>`)}\n if (typeof e.priority === 'number') {parts.push(` <priority>${e.priority}</priority>`)}\n const alternates = e.alternates?.languages\n if (alternates) {\n for (const [lang, href] of Object.entries(alternates)) {\n if (typeof href === 'string') {\n parts.push(\n ` <xhtml:link rel=\"alternate\" hreflang=\"${esc(lang)}\" href=\"${esc(href)}\"/>`,\n )\n }\n }\n }\n return ` <url>\\n${parts.join('\\n')}\\n </url>`\n })\n .join('\\n')\n\n return (\n `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n` +\n `<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ` +\n `xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\\n` +\n urls +\n `\\n</urlset>`\n )\n}\n"],"names":["buildSitemapXml","entries","esc","s","replace","urls","map","e","parts","url","lastModified","iso","Date","toISOString","String","push","changeFrequency","priority","alternates","languages","lang","href","Object","join"],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4BC,GACD,OAAO,SAASA,gBAAgBC,OAAuB;IACrD,MAAMC,MAAM,CAACC,IACXA,EACGC,OAAO,CAAC,MAAM,SACdA,OAAO,CAAC,MAAM,QACdA,OAAO,CAAC,MAAM,QACdA,OAAO,CAAC,MAAM,UACdA,OAAO,CAAC,MAAM;IAEnB,MAAMC,OAAOJ,QACVK,GAAG,CAAC,CAACC;QACJ,MAAMC,QAAQ;YAAC,CAAC,SAAS,EAAEN,IAAIK,EAAEE,GAAG,EAAE,MAAM,CAAC;SAAC;QAC9C,IAAIF,EAAEG,YAAY,EAAE;YAClB,MAAMC,MACJJ,EAAEG,YAAY,YAAYE,OAAOL,EAAEG,YAAY,CAACG,WAAW,KAAKC,OAAOP,EAAEG,YAAY;YACvFF,MAAMO,IAAI,CAAC,CAAC,aAAa,EAAEb,IAAIS,KAAK,UAAU,CAAC;QACjD;QACA,IAAIJ,EAAES,eAAe,EAAE;YAACR,MAAMO,IAAI,CAAC,CAAC,gBAAgB,EAAER,EAAES,eAAe,CAAC,aAAa,CAAC;QAAC;QACvF,IAAI,OAAOT,EAAEU,QAAQ,KAAK,UAAU;YAACT,MAAMO,IAAI,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