Compare commits

..
4 Commits
Author SHA1 Message Date
radoslaw.smolinski 4a46651839 1.4.2 2026-09-28 14:47:19 +02:00
radoslaw.smolinski e7f06548fb Added styling for sitemap generator 2026-09-28 14:47:13 +02:00
radoslaw.smolinski e2a703fc72 1.4.1 2026-09-28 14:29:50 +02:00
radoslaw.smolinski de75d8374d Rebuilded sitemap generator 2026-09-28 14:29:42 +02:00
7 changed files with 268 additions and 280 deletions
+29 -17
View File
@@ -1,33 +1,45 @@
import type { SitemapEntry } from './buildSitemapEntries.js'; import type { SitemapEntry } from './buildSitemapEntries.js';
type BuildSitemapXmlOptions = {
/**
* URL of a CSS stylesheet to make the sitemap readable in the browser, e.g.
* '/sitemap.css'. Uses `type="text/css"` — the W3C "Associating Style Sheets
* with XML" mechanism, which is NOT deprecated (unlike XSLT / type="text/xsl",
* which Chrome/WebKit are removing). CSS on XML shows no warning, styles the
* raw tags directly (e.g. `url { display: block }` turns the wall of text into
* cards), and crawlers ignore the directive entirely.
*/
cssUrl?: string;
};
/** /**
* Serializes sitemap entries to an XML STRING with an XSL stylesheet reference, * Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers
* so /sitemap.xml renders as a readable table in the browser (not raw XML) while * and (with `cssUrl`) styled in the browser via plain CSS.
* staying a valid sitemap for crawlers.
* *
* Why this exists alongside the Next MetadataRoute sitemap: Next's app/sitemap.ts * Two viewing modes:
* (returning SitemapEntry[]) does NOT let you inject <?xml-stylesheet?>. To get * - No cssUrl → the browser's native formatted XML tree (indented, collapsible).
* the styled table, serve a custom route that returns this string instead: * - With cssUrl → a `<?xml-stylesheet type="text/css">` directive; the project's
* CSS styles the XML tags (cards, labels via ::before). NOT XSLT — that's being
* removed from browsers and shows a deprecation warning. CSS is safe and
* W3C-standard.
* *
* // app/sitemap.xml/route.ts * // app/sitemap.xml/route.ts
* import { buildSitemapXml } from '@intecion/ipal-kit' * 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 const dynamic = 'force-dynamic'
* export async function GET() { * export async function GET() {
* const entries = await sitemap() * const entries = await sitemap()
* const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' }) * const xml = buildSitemapXml(entries, { cssUrl: '/sitemap.css' })
* return new Response(xml, { * return new Response(xml, {
* headers: { 'Content-Type': 'application/xml; charset=utf-8' }, * headers: { 'Content-Type': 'application/xml; charset=utf-8' },
* }) * })
* } * }
* *
* Put sitemap.xsl in the project's /public (copy from the plugin's assets, or * Put sitemap.css in the project's /public and style the tags (see docs/seo.md
* serve it from a route). The <?xml-stylesheet?> points browsers at it; crawlers * for a starter). Note: <loc> is an XML tag, not <a href> — CSS can't make it a
* ignore it and read the XML. Include hreflang alternates as <xhtml:link>. * clickable link (some browsers auto-detect URLs); the win is readability, not
* clickability.
* *
* NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one * 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 * Two sitemaps confuse crawlers.
* confuse crawlers.
*/ */
export declare function buildSitemapXml(entries: SitemapEntry[], opts?: { export declare function buildSitemapXml(entries: SitemapEntry[], opts?: BuildSitemapXmlOptions): string;
stylesheetUrl?: string; export {};
}): string;
+18 -17
View File
@@ -1,33 +1,35 @@
/** /**
* Serializes sitemap entries to an XML STRING with an XSL stylesheet reference, * Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers
* so /sitemap.xml renders as a readable table in the browser (not raw XML) while * and (with `cssUrl`) styled in the browser via plain CSS.
* staying a valid sitemap for crawlers.
* *
* Why this exists alongside the Next MetadataRoute sitemap: Next's app/sitemap.ts * Two viewing modes:
* (returning SitemapEntry[]) does NOT let you inject <?xml-stylesheet?>. To get * - No cssUrl → the browser's native formatted XML tree (indented, collapsible).
* the styled table, serve a custom route that returns this string instead: * - With cssUrl → a `<?xml-stylesheet type="text/css">` directive; the project's
* CSS styles the XML tags (cards, labels via ::before). NOT XSLT — that's being
* removed from browsers and shows a deprecation warning. CSS is safe and
* W3C-standard.
* *
* // app/sitemap.xml/route.ts * // app/sitemap.xml/route.ts
* import { buildSitemapXml } from '@intecion/ipal-kit' * 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 const dynamic = 'force-dynamic'
* export async function GET() { * export async function GET() {
* const entries = await sitemap() * const entries = await sitemap()
* const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' }) * const xml = buildSitemapXml(entries, { cssUrl: '/sitemap.css' })
* return new Response(xml, { * return new Response(xml, {
* headers: { 'Content-Type': 'application/xml; charset=utf-8' }, * headers: { 'Content-Type': 'application/xml; charset=utf-8' },
* }) * })
* } * }
* *
* Put sitemap.xsl in the project's /public (copy from the plugin's assets, or * Put sitemap.css in the project's /public and style the tags (see docs/seo.md
* serve it from a route). The <?xml-stylesheet?> points browsers at it; crawlers * for a starter). Note: <loc> is an XML tag, not <a href> — CSS can't make it a
* ignore it and read the XML. Include hreflang alternates as <xhtml:link>. * clickable link (some browsers auto-detect URLs); the win is readability, not
* clickability.
* *
* NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one * 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 * Two sitemaps confuse crawlers.
* confuse crawlers.
*/ export function buildSitemapXml(entries, opts = {}) { */ export function buildSitemapXml(entries, opts = {}) {
const { stylesheetUrl } = opts; const { cssUrl } = opts;
const esc = (s)=>s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;'); const esc = (s)=>s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;');
const urls = entries.map((e)=>{ const urls = entries.map((e)=>{
const parts = [ const parts = [
@@ -39,7 +41,6 @@
} }
if (e.changeFrequency) parts.push(` <changefreq>${e.changeFrequency}</changefreq>`); if (e.changeFrequency) parts.push(` <changefreq>${e.changeFrequency}</changefreq>`);
if (typeof e.priority === 'number') parts.push(` <priority>${e.priority}</priority>`); if (typeof e.priority === 'number') parts.push(` <priority>${e.priority}</priority>`);
// hreflang alternates
const alternates = e.alternates?.languages; const alternates = e.alternates?.languages;
if (alternates) { if (alternates) {
for (const [lang, href] of Object.entries(alternates)){ for (const [lang, href] of Object.entries(alternates)){
@@ -50,7 +51,7 @@
} }
return ` <url>\n${parts.join('\n')}\n </url>`; return ` <url>\n${parts.join('\n')}\n </url>`;
}).join('\n'); }).join('\n');
const stylesheet = stylesheetUrl ? `<?xml-stylesheet type="text/xsl" href="${esc(stylesheetUrl)}"?>\n` : ''; const stylesheet = cssUrl ? `<?xml-stylesheet type="text/css" href="${esc(cssUrl)}"?>\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` + stylesheet + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ` + `xmlns:xhtml="http://www.w3.org/1999/xhtml">\n` + urls + `\n</urlset>`;
} }
File diff suppressed because one or more lines are too long
+188 -139
View File
@@ -19,14 +19,14 @@ admina i bez importMap się nie wyrenderują.
```ts ```ts
ipalKit({ ipalKit({
seo: { seo: {
collections: ['pages', 'posts'], // które kolekcje dostają meta collections: ['pages', 'posts'], // które kolekcje dostają meta
// generateTitle: ({ doc }) => `${doc.title}`, // opcjonalne // generateTitle: ({ doc }) => `${doc.title}`, // opcjonalne
// generateDescription: ({ doc }) => doc.excerpt ?? '', // generateDescription: ({ doc }) => doc.excerpt ?? '',
// fields: [...], // extra pola w grupie SEO // fields: [...], // extra pola w grupie SEO
// autoFill: { title: 'title', description: 'excerpt' }, // mapowanie auto-fill // autoFill: { title: 'title', description: 'excerpt' }, // mapowanie auto-fill
// autoFill: false, // wyłącz auto-fill // autoFill: false, // wyłącz auto-fill
}, },
}) })
``` ```
@@ -89,16 +89,16 @@ import { createPageMetadata } from '@intecion/ipal-kit'
import { i18nConfig } from '@/i18n.config' import { i18nConfig } from '@/i18n.config'
const pageMetadata = createPageMetadata({ const pageMetadata = createPageMetadata({
config: i18nConfig, config: i18nConfig,
baseUrl: process.env.NEXT_PUBLIC_SERVER_URL, baseUrl: process.env.NEXT_PUBLIC_SERVER_URL,
// collection: 'pages', // domyślne // collection: 'pages', // domyślne
// settingsSlug: 'site-settings', // domyślne // settingsSlug: 'site-settings', // domyślne
// siteNameField: 'siteName', // domyślne // siteNameField: 'siteName', // domyślne
}) })
export async function generateMetadata({ params }): Promise<Metadata> { export async function generateMetadata({ params }): Promise<Metadata> {
const { locale, slug } = await params const { locale, slug } = await params
return pageMetadata({ payload: await getPayload({ config }), locale, slug }) return pageMetadata({ payload: await getPayload({ config }), locale, slug })
} }
``` ```
@@ -121,43 +121,43 @@ global. Klient dostarcza resolvery.
import { createMetadataGenerator } from '@intecion/ipal-kit' import { createMetadataGenerator } from '@intecion/ipal-kit'
const generate = createMetadataGenerator({ const generate = createMetadataGenerator({
config: i18nConfig, config: i18nConfig,
baseUrl: process.env.NEXT_PUBLIC_SERVER_URL, baseUrl: process.env.NEXT_PUBLIC_SERVER_URL,
homeSlug: 'homepage', homeSlug: 'homepage',
resolveDocument: async ({ payload, params, locale }) => { resolveDocument: async ({ payload, params, locale }) => {
const slug = (params.slug as string[])?.join('/') const slug = (params.slug as string[])?.join('/')
// meta MUSI przyjść w konkretnym locale (stringi), a slug jako mapa // meta MUSI przyjść w konkretnym locale (stringi), a slug jako mapa
// locale→wartość (hreflang) — to dwa różne odczyty. // locale→wartość (hreflang) — to dwa różne odczyty.
const found = await payload.find({ const found = await payload.find({
collection: 'posts', collection: 'posts',
where: { slug: { equals: slug } }, where: { slug: { equals: slug } },
locale, locale,
depth: 1, depth: 1,
limit: 1, limit: 1,
}) })
const doc = found.docs[0] const doc = found.docs[0]
if (!doc) return null if (!doc) return null
const allLocales = await payload.findByID({ const allLocales = await payload.findByID({
collection: 'posts', collection: 'posts',
id: doc.id, id: doc.id,
locale: 'all', locale: 'all',
depth: 0, depth: 0,
}) })
return { ...doc, slug: allLocales.slug } return { ...doc, slug: allLocales.slug }
}, },
resolveSiteName: async ({ payload, locale }) => resolveSiteName: async ({ payload, locale }) =>
(await getSiteSettings(payload, { locale })).siteName ?? null, (await getSiteSettings(payload, { locale })).siteName ?? null,
resolveImageUrl: async ({ doc }) => doc.meta?.image?.url ?? null, resolveImageUrl: async ({ doc }) => doc.meta?.image?.url ?? null,
}) })
export async function generateMetadata({ params }) { export async function generateMetadata({ params }) {
const { locale, slug } = await params const { locale, slug } = await params
return generate({ payload: await getPayload({ config }), params: { slug }, locale }) return generate({ payload: await getPayload({ config }), params: { slug }, locale })
} }
``` ```
@@ -176,16 +176,16 @@ Gdy chcesz pełną kontrolę:
import { buildMetadata, getLocalizedSlugs } from '@intecion/ipal-kit' import { buildMetadata, getLocalizedSlugs } from '@intecion/ipal-kit'
return buildMetadata({ return buildMetadata({
meta: doc.meta, // z plugin-seo meta: doc.meta, // z plugin-seo
siteName: settings.siteName, siteName: settings.siteName,
imageUrl: '/og.png', imageUrl: '/og.png',
locale: 'pl', locale: 'pl',
slugs: getLocalizedSlugs({ slugField: docAllLocales.slug, config }), slugs: getLocalizedSlugs({ slugField: docAllLocales.slug, config }),
config, config,
baseUrl: 'https://example.com', baseUrl: 'https://example.com',
separator: ' – ', // opcjonalne separator: ' – ', // opcjonalne
order: 'site-first', // opcjonalne order: 'site-first', // opcjonalne
homeSlug: 'homepage', homeSlug: 'homepage',
}) })
// → { title, description, openGraph, alternates: { canonical, languages } } // → { title, description, openGraph, alternates: { canonical, languages } }
``` ```
@@ -232,10 +232,10 @@ nie rozjedzie się z tym, co strony serwują. Handlery są gotowe w
```ts ```ts
// src/lib/content.ts // src/lib/content.ts
export const { /* ... */, sitemap, robots } = createContentHelpers({ export const { /* ... */, sitemap, robots } = createContentHelpers({
config, config,
content: contentConfig, content: contentConfig,
i18n: i18nConfig, // wymagane dla sitemap (hreflang) i18n: i18nConfig, // wymagane dla sitemap (hreflang)
baseUrl: process.env.NEXT_PUBLIC_SERVER_URL, baseUrl: process.env.NEXT_PUBLIC_SERVER_URL,
}) })
``` ```
@@ -278,7 +278,7 @@ Niskopoziomowo (własna trasa zamiast handlera z fabryki):
import { buildSitemapEntries, buildRobots } from '@intecion/ipal-kit' import { buildSitemapEntries, buildRobots } from '@intecion/ipal-kit'
const entries = await buildSitemapEntries({ const entries = await buildSitemapEntries({
payload, config: i18nConfig, baseUrl, content: contentConfig, payload, config: i18nConfig, baseUrl, content: contentConfig,
}) })
``` ```
@@ -321,9 +321,9 @@ import { buildIconsMetadata } from '@intecion/ipal-kit'
import { getSettings } from '@/lib/payload' import { getSettings } from '@/lib/payload'
export async function generateMetadata({ params }): Promise<Metadata> { export async function generateMetadata({ params }): Promise<Metadata> {
const { locale } = await params const { locale } = await params
const settings = await getSettings(locale) const settings = await getSettings(locale)
return buildIconsMetadata(settings.favicon) // z pola favicon (panel) return buildIconsMetadata(settings.favicon) // z pola favicon (panel)
} }
``` ```
@@ -346,16 +346,16 @@ wynikach, logo w knowledge panel.
import { buildOrganizationJsonLd } from '@intecion/ipal-kit' import { buildOrganizationJsonLd } from '@intecion/ipal-kit'
const jsonLd = buildOrganizationJsonLd({ const jsonLd = buildOrganizationJsonLd({
name: settings.siteName, name: settings.siteName,
url: process.env.NEXT_PUBLIC_SERVER_URL!, url: process.env.NEXT_PUBLIC_SERVER_URL!,
logo: settings.logo, logo: settings.logo,
sameAs: settings.socialLinks, // opcjonalne: profile społecznościowe sameAs: settings.socialLinks, // opcjonalne: profile społecznościowe
}) })
// w JSX layoutu: // w JSX layoutu:
<script <script
type="application/ld+json" type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/> />
``` ```
@@ -403,42 +403,42 @@ import { i18nConfig } from '@/i18n.config'
import type { SiteSetting } from '@/payload-types' import type { SiteSetting } from '@/payload-types'
export default async function manifest(): Promise<MetadataRoute.Manifest> { export default async function manifest(): Promise<MetadataRoute.Manifest> {
const payload = await getCachedPayload() const payload = await getCachedPayload()
const settings = await getSiteSettings<SiteSetting>(payload, { const settings = await getSiteSettings<SiteSetting>(payload, {
locale: i18nConfig.defaultLocale as never, locale: i18nConfig.defaultLocale as never,
}) })
const siteName = settings?.siteName?.trim() const siteName = settings?.siteName?.trim()
// Ikona z panelu (favicon → logo). Dla PNG podaj KONKRETNY rozmiar z media // Ikona z panelu (favicon → logo). Dla PNG podaj KONKRETNY rozmiar z media
// (nie 'any' — 'any' jest tylko dla SVG). Bez ikony → pomiń pole icons. // (nie 'any' — 'any' jest tylko dla SVG). Bez ikony → pomiń pole icons.
const icon = settings?.favicon ?? settings?.logo const icon = settings?.favicon ?? settings?.logo
const iconEntry = const iconEntry =
typeof icon === 'object' && icon?.url typeof icon === 'object' && icon?.url
? (() => { ? (() => {
const isSvg = icon.mimeType === 'image/svg+xml' || icon.url.endsWith('.svg') const isSvg = icon.mimeType === 'image/svg+xml' || icon.url.endsWith('.svg')
const size = const size =
typeof icon.width === 'number' && typeof icon.height === 'number' typeof icon.width === 'number' && typeof icon.height === 'number'
? `${Math.min(icon.width, icon.height)}x${Math.min(icon.width, icon.height)}` ? `${Math.min(icon.width, icon.height)}x${Math.min(icon.width, icon.height)}`
: '512x512' : '512x512'
return { return {
src: icon.url, src: icon.url,
type: icon.mimeType ?? 'image/png', type: icon.mimeType ?? 'image/png',
sizes: isSvg ? 'any' : size, // 'any' tylko dla SVG sizes: isSvg ? 'any' : size, // 'any' tylko dla SVG
} }
})() })()
: undefined : undefined
// Buduj TYLKO z tego, co jest. Brak pola → nie ma go w manifeście (zamiast // Buduj TYLKO z tego, co jest. Brak pola → nie ma go w manifeście (zamiast
// zaszytego fallbacku). start_url z configu, nie zaszyte '/pl'. // zaszytego fallbacku). start_url z configu, nie zaszyte '/pl'.
return { return {
...(siteName ? { name: siteName, short_name: siteName } : {}), ...(siteName ? { name: siteName, short_name: siteName } : {}),
start_url: `/${i18nConfig.defaultLocale}`, start_url: `/${i18nConfig.defaultLocale}`,
display: 'standalone', display: 'standalone',
...(iconEntry ? { icons: [iconEntry] } : {}), ...(iconEntry ? { icons: [iconEntry] } : {}),
// theme_color / background_color / description — TYLKO jeśli dodasz pola w // theme_color / background_color / description — TYLKO jeśli dodasz pola w
// panelu i je odczytasz. NIE zaszywaj '#0e1e24' ani opisu klienta. // panelu i je odczytasz. NIE zaszywaj '#0e1e24' ani opisu klienta.
} }
} }
``` ```
@@ -522,16 +522,16 @@ zanim strona wygeneruje metadane. To pcha metadata do body. NIE deklaruj `<head>
```tsx ```tsx
// ŹLE — jawny <head> zamyka head za wcześnie // ŹLE — jawny <head> zamyka head za wcześnie
<html lang={locale}> <html lang={locale}>
<head><MediaPreconnect /></head> <head><MediaPreconnect /></head>
<body>{children}</body> <body>{children}</body>
</html> </html>
// DOBRZE — MediaPreconnect w body, React 19 hoistuje link do head // DOBRZE — MediaPreconnect w body, React 19 hoistuje link do head
<html lang={locale}> <html lang={locale}>
<body> <body>
<MediaPreconnect /> <MediaPreconnect />
{children} {children}
</body> </body>
</html> </html>
``` ```
@@ -546,8 +546,8 @@ z jakiegoś powodu nie może być ISR):
```ts ```ts
// next.config.ts // next.config.ts
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
htmlLimitedBots: htmlLimitedBots:
/Googlebot|Google-InspectionTool|Storebot-Google|Bingbot|Yandex|DuckDuckBot|Baiduspider|Screaming Frog|AhrefsBot|SemrushBot/i, /Googlebot|Google-InspectionTool|Storebot-Google|Bingbot|Yandex|DuckDuckBot|Baiduspider|Screaming Frog|AhrefsBot|SemrushBot/i,
} }
``` ```
@@ -652,10 +652,10 @@ searchbox (pole wyszukiwania pod wynikiem marki). RAZ w root layout:
```tsx ```tsx
import { buildWebSiteJsonLd } from '@intecion/ipal-kit' import { buildWebSiteJsonLd } from '@intecion/ipal-kit'
const jsonLd = buildWebSiteJsonLd({ const jsonLd = buildWebSiteJsonLd({
name: settings.siteName, name: settings.siteName,
url: baseUrl, url: baseUrl,
// TYLKO jeśli masz działającą stronę wyszukiwania: // TYLKO jeśli masz działającą stronę wyszukiwania:
search: { target: `${baseUrl}/szukaj?q={search_term_string}` }, search: { target: `${baseUrl}/szukaj?q={search_term_string}` },
}) })
``` ```
Pomiń `search`, jeśli nie ma realnej wyszukiwarki — SearchAction wskazujący na Pomiń `search`, jeśli nie ma realnej wyszukiwarki — SearchAction wskazujący na
@@ -666,9 +666,9 @@ Detailing) + Google rozumie hierarchię. PER STRONA, z pozycji strony:
```tsx ```tsx
import { buildBreadcrumbJsonLd } from '@intecion/ipal-kit' import { buildBreadcrumbJsonLd } from '@intecion/ipal-kit'
const jsonLd = buildBreadcrumbJsonLd([ const jsonLd = buildBreadcrumbJsonLd([
{ name: 'Strona główna', url: `${base}/pl` }, { name: 'Strona główna', url: `${base}/pl` },
{ name: 'Usługi', url: `${base}/pl/uslugi` }, { name: 'Usługi', url: `${base}/pl/uslugi` },
{ name: 'Detailing', url: `${base}/pl/uslugi/detailing` }, { name: 'Detailing', url: `${base}/pl/uslugi/detailing` },
]) ])
``` ```
Okruszki buduj z RZECZYWISTEJ pozycji strony (resolveRoute / ścieżka URL), NIE z Okruszki buduj z RZECZYWISTEJ pozycji strony (resolveRoute / ścieżka URL), NIE z
@@ -679,7 +679,7 @@ samych pozycji co menu w headerze:
```tsx ```tsx
import { buildSiteNavigationJsonLd } from '@intecion/ipal-kit' import { buildSiteNavigationJsonLd } from '@intecion/ipal-kit'
const jsonLd = buildSiteNavigationJsonLd( const jsonLd = buildSiteNavigationJsonLd(
navItems.map(i => ({ name: i.label, url: `${base}${i.href}` })) navItems.map(i => ({ name: i.label, url: `${base}${i.href}` }))
) )
``` ```
Dane z tego samego źródła co widoczne menu — nie osobna zaszyta lista. Dane z tego samego źródła co widoczne menu — nie osobna zaszyta lista.
@@ -886,17 +886,17 @@ Generative Engine Optimization) i audytach „Agentic Browsing”.
Wymaga wypełnionego siteDescription i sensownych meta.description stron Wymaga wypełnionego siteDescription i sensownych meta.description stron
(inaczej llms.txt będzie ubogi). (inaczej llms.txt będzie ubogi).
## Sitemap jako czytelna tabela (XSL stylesheet) ## Sitemap czytelny w przeglądarce (czysty XML)
Domyślny `/sitemap.xml` to surowy XML — Google go czyta, ale człowiek widzi Domyślny `/sitemap.xml` (Next MetadataRoute) działa dla Google, ale w przeglądarce
„ścianę tagów". Można ostylować przez XSL (przeglądarka renderuje tabelę), bywa nieczytelny. Możesz serwować go jako **czysty, sformatowany XML** przez
zachowując poprawność dla crawlerów. `buildSitemapXml` — przeglądarka pokaże wbudowane drzewo XML (wcięcia, zwijanie,
kolorowanie składni), bez żadnej transformacji.
### Ograniczenie Next > **NIE używaj XSLT.** Przeglądarki (Chrome i in.) WYCOFUJĄ XSLT — arkusz
> `<?xml-stylesheet?>` pokazuje ostrzeżenie i wkrótce przestanie działać.
Standardowy `app/sitemap.ts` (Next MetadataRoute) **nie pozwala** wstrzyknąć > Rozwiązanie: serwuj czysty XML z poprawnym Content-Type; przeglądarka
`<?xml-stylesheet?>`. Żeby mieć styl, serwuj sitemap własnym route przez > renderuje swój natywny widok drzewa XML sama.
`buildSitemapXml` (string XML z odwołaniem do XSL):
```ts ```ts
// app/sitemap.xml/route.ts (zamiast app/sitemap.ts) // app/sitemap.xml/route.ts (zamiast app/sitemap.ts)
@@ -907,24 +907,73 @@ export const dynamic = 'force-dynamic'
export async function GET() { export async function GET() {
const entries = await sitemap() const entries = await sitemap()
const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' }) const xml = buildSitemapXml(entries)
return new Response(xml, { return new Response(xml, {
headers: { 'Content-Type': 'application/xml; charset=utf-8' }, headers: { 'Content-Type': 'application/xml; charset=utf-8' },
}) })
} }
``` ```
### Plik XSL w /public `buildSitemapXml` zwraca wcięty XML. Dwa tryby wyświetlania:
Skopiuj `sitemap.xsl` (z pluginu: `node_modules/@intecion/ipal-kit/dist/modules/seo/assets/sitemap.xsl`) **Bez `cssUrl`** → przeglądarka pokazuje natywny widok drzewa XML (wcięcia, zwijanie).
do `public/sitemap.xsl` w projekcie. Zawiera responsywną tabelę (numer, URL,
data, języki) z dark mode. `stylesheetUrl: '/sitemap.xsl'` wskazuje na niego. **Z `cssUrl`** → stylujesz XML własnym CSS (kafelki, etykiety). To W3C standard
„Associating Style Sheets with XML" — `type="text/css"`, NIE wycofywane (w
przeciwieństwie do XSLT/`text/xsl`). Zero ostrzeżenia, ładny wygląd, bezpieczne
dla Google (crawlery ignorują dyrektywę).
```ts
const xml = buildSitemapXml(entries, { cssUrl: '/sitemap.css' })
```
### Starter CSS (public/sitemap.css)
Skopiuj do `public/sitemap.css` w projekcie, dostosuj do designu. Selektory to
bezpośrednio nazwy tagów XML:
```css
/* public/sitemap.css */
urlset {
display: block;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #090d16; color: #f1f5f9;
padding: 2rem 1.5rem; max-width: 1200px; margin: 0 auto;
}
url { /* każdy adres jako kafelek */
display: block;
background: #111827; border: 1px solid #1e293b; border-radius: 8px;
padding: 1rem 1.25rem; margin-bottom: 0.75rem;
}
loc { /* adres URL */
display: block; font-size: 0.95rem; font-weight: 600;
color: #f97316; margin-bottom: 0.5rem; word-break: break-all;
}
lastmod, changefreq, priority {
display: inline-block; font-size: 0.8rem; color: #94a3b8; margin-right: 1.5rem;
}
lastmod::before { content: 'Ostatnia modyfikacja: '; color: #64748b; }
changefreq::before { content: 'Częstotliwość: '; color: #64748b; }
priority::before { content: 'Priorytet: '; color: #64748b; }
link { /* tagi hreflang */
display: inline-block; font-size: 0.75rem;
background: #1e293b; color: #38bdf8; border: 1px solid #334155;
padding: 0.15rem 0.45rem; border-radius: 4px; margin: 0.4rem 0.35rem 0 0;
}
```
Kluczowe: `display: block` na `<url>`/`<loc>` zamienia „ścianę tekstu" w kafelki.
Etykiety („Ostatnia modyfikacja:") przez `::before`. Dostosuj kolory do projektu.
**Ograniczenie:** `<loc>` to tag XML, nie `<a href>` — CSS nie zrobi z niego
klikalnego linku (niektóre przeglądarki autodetektują URL). Zysk to czytelność
i organizacja, nie klikalność. Dla sitemap (głównie dla robotów) to akceptowalne.
### WAŻNE — jeden sitemap, nie dwa ### WAŻNE — jeden sitemap, nie dwa
Jeśli używasz route `app/sitemap.xml/route.ts` (styled), **USUŃ** `app/sitemap.ts` Jeśli używasz `app/sitemap.xml/route.ts`, USUŃ `app/sitemap.ts` (MetadataRoute).
(MetadataRoute). Dwa sitemapy pod różnymi ścieżkami mylą crawlery. Wybierz jeden: Dwa sitemapy pod różnymi ścieżkami mylą crawlery. Wybierz jeden:
- **styled** (`sitemap.xml/route.ts` + buildSitemapXml + XSL) — ładna tabela - **route.ts + buildSitemapXml** — czytelny XML w przeglądarce
- **prosty** (`sitemap.ts` + reeksport z lib/content) — bez stylu, mniej kodu - **sitemap.ts** (reeksport z lib/content) — mniej kodu, mniej czytelny w przeglądarce
Styl to kosmetyka (Google czyta oba tak samo) — rób, jeśli klient/audyt tego chce. Oba tak samo dobre dla Google — to kwestia czytelności dla człowieka, nie SEO.
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@intecion/ipal-kit", "name": "@intecion/ipal-kit",
"version": "1.4.0", "version": "1.4.2",
"description": "Intecion Payload Advanced Library — a Payload CMS 3 plugin: i18n, SEO, forms, consent, analytics, blog/archives.", "description": "Intecion Payload Advanced Library — a Payload CMS 3 plugin: i18n, SEO, forms, consent, analytics, blog/archives.",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
-85
View File
@@ -1,85 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html lang="pl">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Sitemap</title>
<style>
:root { color-scheme: light dark; }
* { box-sizing: border-box; }
body {
font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
margin: 0; padding: 2rem 1rem; line-height: 1.5;
color: #1a1a1a; background: #f7f7f8;
}
.wrap { max-width: 1100px; margin: 0 auto; }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
.meta { color: #666; font-size: .9rem; margin-bottom: 1.5rem; }
.count { font-weight: 600; }
table { width: 100%; border-collapse: collapse; background: #fff;
border-radius: .5rem; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
th, td { text-align: left; padding: .65rem .9rem; border-bottom: 1px solid #eee;
font-size: .9rem; }
th { background: #fafafa; font-weight: 600; color: #444;
position: sticky; top: 0; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f5f8ff; }
a { color: #2563eb; text-decoration: none; word-break: break-all; }
a:hover { text-decoration: underline; }
.num { color: #999; width: 3rem; text-align: right; font-variant-numeric: tabular-nums; }
.langs { color: #666; font-size: .8rem; }
@media (prefers-color-scheme: dark) {
body { color: #e5e5e5; background: #18181b; }
table { background: #232327; box-shadow: none; }
th { background: #27272a; color: #ccc; }
th, td { border-color: #333; }
tr:hover td { background: #1e293b; }
.meta, .num, .langs { color: #888; }
}
</style>
</head>
<body>
<div class="wrap">
<h1>Mapa witryny (Sitemap)</h1>
<p class="meta">
Ten plik jest odczytywany przez wyszukiwarki.
Adresów: <span class="count"><xsl:value-of select="count(s:urlset/s:url)"/></span>
</p>
<table>
<thead>
<tr>
<th class="num">#</th>
<th>Adres URL</th>
<th>Ostatnia zmiana</th>
<th>Języki</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="s:urlset/s:url">
<tr>
<td class="num"><xsl:value-of select="position()"/></td>
<td>
<a href="{s:loc}"><xsl:value-of select="s:loc"/></a>
</td>
<td><xsl:value-of select="s:lastmod"/></td>
<td class="langs">
<xsl:for-each select="xhtml:link">
<xsl:value-of select="@hreflang"/>
<xsl:if test="position() != last()"><xsl:text>, </xsl:text></xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
+31 -20
View File
@@ -1,39 +1,53 @@
import type { SitemapEntry } from './buildSitemapEntries.js' import type { SitemapEntry } from './buildSitemapEntries.js'
type BuildSitemapXmlOptions = {
/**
* URL of a CSS stylesheet to make the sitemap readable in the browser, e.g.
* '/sitemap.css'. Uses `type="text/css"` — the W3C "Associating Style Sheets
* with XML" mechanism, which is NOT deprecated (unlike XSLT / type="text/xsl",
* which Chrome/WebKit are removing). CSS on XML shows no warning, styles the
* raw tags directly (e.g. `url { display: block }` turns the wall of text into
* cards), and crawlers ignore the directive entirely.
*/
cssUrl?: string
}
/** /**
* Serializes sitemap entries to an XML STRING with an XSL stylesheet reference, * Serializes sitemap entries to a clean, indented XML STRING — valid for crawlers
* so /sitemap.xml renders as a readable table in the browser (not raw XML) while * and (with `cssUrl`) styled in the browser via plain CSS.
* staying a valid sitemap for crawlers.
* *
* Why this exists alongside the Next MetadataRoute sitemap: Next's app/sitemap.ts * Two viewing modes:
* (returning SitemapEntry[]) does NOT let you inject <?xml-stylesheet?>. To get * - No cssUrl → the browser's native formatted XML tree (indented, collapsible).
* the styled table, serve a custom route that returns this string instead: * - With cssUrl → a `<?xml-stylesheet type="text/css">` directive; the project's
* CSS styles the XML tags (cards, labels via ::before). NOT XSLT — that's being
* removed from browsers and shows a deprecation warning. CSS is safe and
* W3C-standard.
* *
* // app/sitemap.xml/route.ts * // app/sitemap.xml/route.ts
* import { buildSitemapXml } from '@intecion/ipal-kit' * 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 const dynamic = 'force-dynamic'
* export async function GET() { * export async function GET() {
* const entries = await sitemap() * const entries = await sitemap()
* const xml = buildSitemapXml(entries, { stylesheetUrl: '/sitemap.xsl' }) * const xml = buildSitemapXml(entries, { cssUrl: '/sitemap.css' })
* return new Response(xml, { * return new Response(xml, {
* headers: { 'Content-Type': 'application/xml; charset=utf-8' }, * headers: { 'Content-Type': 'application/xml; charset=utf-8' },
* }) * })
* } * }
* *
* Put sitemap.xsl in the project's /public (copy from the plugin's assets, or * Put sitemap.css in the project's /public and style the tags (see docs/seo.md
* serve it from a route). The <?xml-stylesheet?> points browsers at it; crawlers * for a starter). Note: <loc> is an XML tag, not <a href> — CSS can't make it a
* ignore it and read the XML. Include hreflang alternates as <xhtml:link>. * clickable link (some browsers auto-detect URLs); the win is readability, not
* clickability.
* *
* NOTE: if you use this custom route, DON'T also keep app/sitemap.ts — pick one * 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 * Two sitemaps confuse crawlers.
* confuse crawlers.
*/ */
export function buildSitemapXml( export function buildSitemapXml(
entries: SitemapEntry[], entries: SitemapEntry[],
opts: { stylesheetUrl?: string } = {}, opts: BuildSitemapXmlOptions = {},
): string { ): string {
const { stylesheetUrl } = opts const { cssUrl } = opts
const esc = (s: string): string => const esc = (s: string): string =>
s s
@@ -53,7 +67,6 @@ export function buildSitemapXml(
} }
if (e.changeFrequency) parts.push(` <changefreq>${e.changeFrequency}</changefreq>`) if (e.changeFrequency) parts.push(` <changefreq>${e.changeFrequency}</changefreq>`)
if (typeof e.priority === 'number') parts.push(` <priority>${e.priority}</priority>`) if (typeof e.priority === 'number') parts.push(` <priority>${e.priority}</priority>`)
// hreflang alternates
const alternates = e.alternates?.languages const alternates = e.alternates?.languages
if (alternates) { if (alternates) {
for (const [lang, href] of Object.entries(alternates)) { for (const [lang, href] of Object.entries(alternates)) {
@@ -68,9 +81,7 @@ export function buildSitemapXml(
}) })
.join('\n') .join('\n')
const stylesheet = stylesheetUrl const stylesheet = cssUrl ? `<?xml-stylesheet type="text/css" href="${esc(cssUrl)}"?>\n` : ''
? `<?xml-stylesheet type="text/xsl" href="${esc(stylesheetUrl)}"?>\n`
: ''
return ( return (
`<?xml version="1.0" encoding="UTF-8"?>\n` + `<?xml version="1.0" encoding="UTF-8"?>\n` +