feat: HTML sitemap pages and footer link
- the module registers seo-sitemap-<locale> at seo.htmlSitemap.paths (defaults pl /mapa-strony, en /sitemap) and seo-sitemap-group-<locale> at <path>/:group (plus <path>/:group/<segment>/:page in the path form); a path an app page already has stays the app's - SeoHtmlSitemapPage (global, overridable): H1, an H2 per non-empty group, categories nested, a large group's link to its group page, sitemap.empty; group pages with a built trail, page suffix and <SeoPagination>; 404 for an unknown group or a page past the last, 301 for ?page=1, 503 when the sitemap API fails - GET /api/__seo__/sitemap proxies the sitemap plugin's HTML API (only while the CMS has the HTML sitemap on for the locale) - <SeoSitemapLink>: the footer link while the HTML sitemap is on - L3 default styles
This commit is contained in:
62
server/api/__seo__/sitemap.get.ts
Normal file
62
server/api/__seo__/sitemap.get.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
// GET /api/__seo__/sitemap?locale=&group=&page=: the HTML sitemap (D-34)
|
||||
// from the sitemap plugin, for the layer's HTML sitemap pages
|
||||
// (SeoHtmlSitemapPage). Without group: the overview of the locale; with
|
||||
// group: one page of that group.
|
||||
//
|
||||
// 404 the layer is off, the backend has no site answer (an older SEO
|
||||
// plugin) or the HTML sitemap is off or has no path for the
|
||||
// locale, a malformed parameter, an unknown group or a page past
|
||||
// the last (the plugin's 404 is passed through)
|
||||
// 503 the backend did not answer or answered anything else (with
|
||||
// Retry-After: 60), so the page renders an error, never an empty
|
||||
// sitemap that a route cache could keep
|
||||
//
|
||||
// The target is the server-only backend base plus a checked group slug;
|
||||
// only locale and page travel as query values. Never route-cached
|
||||
// ('/api/__seo__/**' cache false) and never indexed.
|
||||
|
||||
import { seoServerConfig } from '../../lib/config.ts'
|
||||
import { HTML_SITEMAP_GROUP, HTML_SITEMAP_LOCALE, htmlSitemapGroupPage, htmlSitemapOverview, htmlSitemapPage } from '../../lib/htmlSitemap.ts'
|
||||
import { siteAnswer } from '../../lib/site.ts'
|
||||
|
||||
const notFound = () => createError({ statusCode: 404, statusMessage: 'Not Found' })
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
setResponseHeaders(event, { 'cache-control': 'no-store', 'x-robots-tag': 'noindex' })
|
||||
const unavailable = () => {
|
||||
setResponseHeader(event, 'retry-after', '60')
|
||||
return createError({ statusCode: 503, statusMessage: 'Sitemap unavailable' })
|
||||
}
|
||||
const cfg = seoServerConfig(useRuntimeConfig(event))
|
||||
if (!cfg.enabled) throw notFound()
|
||||
const q = getQuery(event)
|
||||
const locale = typeof q.locale === 'string' ? q.locale : ''
|
||||
const group = typeof q.group === 'string' ? q.group : ''
|
||||
const page = htmlSitemapPage(q.page)
|
||||
if (!HTML_SITEMAP_LOCALE.test(locale) || (group !== '' && !HTML_SITEMAP_GROUP.test(group)) || page === null) throw notFound()
|
||||
|
||||
const forwarded = event.context.seoForwardedFor as string | undefined
|
||||
const headers: Record<string, string> = forwarded ? { 'x-forwarded-for': forwarded } : {}
|
||||
const state = await siteAnswer(cfg, headers)
|
||||
if (state.state === 'unknown') throw unavailable()
|
||||
if (state.state === 'absent') throw notFound()
|
||||
const html = state.site.html_sitemap
|
||||
if (!html || html.enabled !== true || !html.paths || !html.paths[locale]) throw notFound()
|
||||
|
||||
const path = group ? `/_sitemap/api/v1/html/${encodeURIComponent(group)}` : '/_sitemap/api/v1/html'
|
||||
let res: { status: number; _data?: unknown }
|
||||
try {
|
||||
res = await $fetch.raw(path, {
|
||||
baseURL: cfg.backendBase,
|
||||
query: group ? { locale, page } : { locale },
|
||||
headers,
|
||||
timeout: Math.max(cfg.timeoutMs, 3000),
|
||||
ignoreResponseError: true,
|
||||
})
|
||||
} catch {
|
||||
throw unavailable()
|
||||
}
|
||||
if (res.status === 404) throw notFound()
|
||||
if (res.status !== 200 || !res._data || typeof res._data !== 'object') throw unavailable()
|
||||
return group ? htmlSitemapGroupPage(res._data, group) : htmlSitemapOverview(res._data)
|
||||
})
|
||||
59
server/lib/htmlSitemap.ts
Normal file
59
server/lib/htmlSitemap.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
// The HTML sitemap answers of the sitemap plugin (GET
|
||||
// <backendBase>/_sitemap/api/v1/html?locale= and
|
||||
// /_sitemap/api/v1/html/{group}?locale=&page=) reduced to the fields the
|
||||
// layer renders, plus the checks of the layer's proxy route. Pure, so
|
||||
// `node --test` drives it.
|
||||
|
||||
import type { SeoSitemapEntry, SeoSitemapGroup, SeoSitemapGroupPage, SeoSitemapOverview } from '../../app/utils/types.ts'
|
||||
|
||||
export const HTML_SITEMAP_LOCALE = /^[a-z]{2,3}(?:[-_][A-Za-z0-9]{2,8})*$/
|
||||
export const HTML_SITEMAP_GROUP = /^[a-z0-9][a-z0-9-]{0,63}$/
|
||||
/** Nesting deeper than this (a category tree) is cut off. */
|
||||
const MAX_DEPTH = 8
|
||||
|
||||
const str = (v: unknown) => (typeof v === 'string' ? v : '')
|
||||
const int = (v: unknown) => (typeof v === 'number' && Number.isInteger(v) && v >= 0 ? v : 0)
|
||||
|
||||
function entries(value: unknown, depth = 0): SeoSitemapEntry[] {
|
||||
if (!Array.isArray(value) || depth > MAX_DEPTH) return []
|
||||
const out: SeoSitemapEntry[] = []
|
||||
for (const e of value) {
|
||||
if (!e || typeof e !== 'object') continue
|
||||
const o = e as Record<string, unknown>
|
||||
const title = str(o.title)
|
||||
if (!title) continue
|
||||
const children = entries(o.children, depth + 1)
|
||||
out.push(children.length > 0 ? { title, url: str(o.url), children } : { title, url: str(o.url) })
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** The overview with only its non-empty groups. */
|
||||
export function htmlSitemapOverview(value: unknown): SeoSitemapOverview {
|
||||
const groups: SeoSitemapGroup[] = []
|
||||
const raw = value && typeof value === 'object' ? (value as { groups?: unknown }).groups : null
|
||||
for (const g of Array.isArray(raw) ? raw : []) {
|
||||
if (!g || typeof g !== 'object') continue
|
||||
const o = g as Record<string, unknown>
|
||||
const slug = str(o.slug)
|
||||
const list = entries(o.entries)
|
||||
if (!HTML_SITEMAP_GROUP.test(slug) || list.length === 0) continue
|
||||
groups.push({ key: str(o.key) || slug, slug, title: str(o.title) || slug, total: int(o.total) || list.length, entries: list, more: o.more === true })
|
||||
}
|
||||
return { groups }
|
||||
}
|
||||
|
||||
/** One page of a group. */
|
||||
export function htmlSitemapGroupPage(value: unknown, group: string): SeoSitemapGroupPage {
|
||||
const o = value && typeof value === 'object' ? (value as Record<string, unknown>) : {}
|
||||
const page = int(o.page) || 1
|
||||
return { group, title: str(o.title) || group, page, pages: Math.max(int(o.pages), page), entries: entries(o.entries) }
|
||||
}
|
||||
|
||||
/** A page query value: 1 when absent, the number for 1 to 999999, else null. */
|
||||
export function htmlSitemapPage(raw: unknown): number | null {
|
||||
if (raw === undefined || raw === null || raw === '') return 1
|
||||
if (typeof raw !== 'string' || !/^\d{1,6}$/.test(raw)) return null
|
||||
const n = Number(raw)
|
||||
return n >= 1 ? n : null
|
||||
}
|
||||
Reference in New Issue
Block a user