feat: route report and drift check at server start

- the module writes #seo-routes (static paths and route patterns) from the
  resolved pages; options seo.routeReport, seo.pagination and
  seo.htmlSitemap reach the public runtime config
- seo-route-report: POST /registry/routes once per process with the
  route-report token, one retry after 60 s on failure, nothing without a
  token, with the layer off or with routeReport false
- seo-drift: one "[vue-seo-layer] drift:" warning per mismatch between
  the CMS's site answer (URL patterns, pagination, HTML sitemap paths)
  and the build
- GET /api/__seo__/site and useSeoSite()
- smoke grammar routereport and log, fixture registry route, drift run
This commit is contained in:
Jakub Zych
2026-11-04 11:49:21 +01:00
parent 9cc2cda924
commit 3abc986f42
21 changed files with 599 additions and 20 deletions

View File

@@ -8,8 +8,10 @@
// app still uses it: each module reads only its own keys, so the layer's
// options never use a name nuxt-seo-utils has (enabled, fallbackTitle …).
import { defineNuxtModule, useLogger } from 'nuxt/kit'
import { addServerTemplate, defineNuxtModule, useLogger } from 'nuxt/kit'
import { findOffenders, guardMessage, noindexRule, type GuardPage } from './guard.ts'
import { htmlSitemapOption, paginationOption, type SeoHtmlSitemapOption, type SeoPaginationOption } from './options.ts'
import { routePatterns, routesTemplate, staticRoutes, type RoutePage } from './routes.ts'
export interface SeoModuleOptions {
/**
@@ -45,6 +47,25 @@ export interface SeoModuleOptions {
* build guard then checks nothing. Default true.
*/
indexable: boolean
/**
* Report the site's static routes to the CMS's page registry at server
* start (D-33). Needs the route-report token (NUXT_SEO_ROUTE_REPORT_TOKEN,
* else NUXT_SEO_NOTFOUND_TOKEN). Default true.
*/
routeReport: boolean
/**
* The pagination URL form the site's routes are built for (D-40): query
* (?page=2) or path (/blog/strona/2, the segment per locale). The CMS's
* resolve answer wins at runtime; the startup drift check logs a
* difference.
*/
pagination: SeoPaginationOption
/**
* The HTML sitemap pages (D-34), by locale: the overview at the path, a
* group at <path>/<group>. false registers none. A path an app page
* already has is left to the app.
*/
htmlSitemap: SeoHtmlSitemapOption
}
export const SEO_MODULE_DEFAULTS: SeoModuleOptions = {
@@ -55,6 +76,9 @@ export const SEO_MODULE_DEFAULTS: SeoModuleOptions = {
resolveFiles: ['pdf'],
noindexRoutes: [],
indexable: true,
routeReport: true,
pagination: { form: 'query', param: 'page', segments: { pl: 'strona', en: 'page' } },
htmlSitemap: { paths: { pl: '/mapa-strony', en: '/sitemap' } },
}
const strings = (v: unknown): string[] => (Array.isArray(v) ? v.filter((e): e is string => typeof e === 'string' && e !== '') : [])
@@ -72,6 +96,8 @@ export default defineNuxtModule<SeoModuleOptions>({
// a global ssr: false into a '/**' route rule.
const ssr = nuxt.options.ssr !== false
const inlineRouteRules = nuxt.options.experimental?.inlineRouteRules === true
const pagination = paginationOption(options.pagination)
const htmlSitemap = htmlSitemapOption(options.htmlSitemap)
// requiresAuth and seo are read from definePageMeta at build time, so
// the route report leaves signed-in and noindex pages out (D-23).
@@ -123,6 +149,17 @@ export default defineNuxtModule<SeoModuleOptions>({
pages = resolved as GuardPage[]
guard()
})
// The route artefact (#seo-routes) for the route report and the drift
// check: written from the resolved pages when Nitro bundles the
// server, so no network call happens at build time.
addServerTemplate({
filename: '#seo-routes',
getContents: () => {
const resolved = (pages ?? []) as RoutePage[]
return routesTemplate(staticRoutes(resolved, noindexRoutes), routePatterns(resolved))
},
})
nuxt.hook('nitro:build:before', (nitro) => {
nitroRules = nitro.options.routeRules as Record<string, unknown>
if (!checked || nuxt.options.dev) guard()
@@ -141,6 +178,9 @@ export default defineNuxtModule<SeoModuleOptions>({
resolveFiles: Array.isArray(options.resolveFiles) ? [...options.resolveFiles] : [...SEO_MODULE_DEFAULTS.resolveFiles],
noindexRoutes,
indexable,
routeReport: options.routeReport !== false,
pagination,
htmlSitemap,
}
},
})