- lib/guard.ts: pure checks (isNoindex with Nitro route-rule patterns, findOffenders over the app's ssr flag, every merged route rule, .client.vue pages and experimental.inlineRouteRules) - module options seo.noindexRoutes and seo.indexable; declared noindex routes get X-Robots-Tag: noindex, nofollow through their route rule - a production build fails with the D-70 message; nuxt dev and nuxt prepare only warn - testing/guard-check.mjs builds four fixture apps (guard:check)
140 lines
5.7 KiB
TypeScript
140 lines
5.7 KiB
TypeScript
// The layer's local Nuxt module (configKey `seo`). It copies the module
|
|
// options into runtimeConfig.public.seo, where the layer's app and server
|
|
// code read them. It lives in lib/ rather than modules/ so Nuxt's
|
|
// local-module scan cannot register it a second time. Layer source never
|
|
// uses the ~ or @ aliases: they resolve to the consuming project.
|
|
//
|
|
// The config key `seo` is shared with nuxt-seo-utils (@nuxtjs/seo) where an
|
|
// 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 { findOffenders, guardMessage, noindexRule, type GuardPage } from './guard.ts'
|
|
|
|
export interface SeoModuleOptions {
|
|
/**
|
|
* false turns every CMS call off (no resolve, no head from the CMS, no
|
|
* redirects, no root files, no 404 reports), for a site without the SEO
|
|
* plugin. An alias of public.seo.enabled (NUXT_PUBLIC_SEO_ENABLED), which
|
|
* keeps working; either one false switches the layer off.
|
|
*/
|
|
cms: boolean
|
|
/** Breadcrumbs from the CMS (components in 33-08). */
|
|
breadcrumbs: boolean
|
|
links: {
|
|
/**
|
|
* A visually hidden "(opens in a new tab)" hint inside external links
|
|
* while the CMS's links_new_tab is on. Copied to
|
|
* runtimeConfig.public.seo.links.newTabHint for the body-link pipeline.
|
|
*/
|
|
newTabHint: boolean
|
|
}
|
|
/** Serve robots.txt, the sitemaps, llms.txt and the CMS's root files from the backend. */
|
|
rootFiles: boolean
|
|
/** File extensions the Nitro middleware still resolves, so an old file address can redirect. */
|
|
resolveFiles: string[]
|
|
/**
|
|
* Route-rule patterns ('/p/**', '/konto', '/x/*') the app declares
|
|
* noindex: they are served with X-Robots-Tag: noindex, nofollow and may
|
|
* render in the browser only (ssr: false, a .client.vue page) without
|
|
* failing the build guard (D-70, D-23).
|
|
*/
|
|
noindexRoutes: string[]
|
|
/**
|
|
* false for a site that is never indexed (an internal tool, a panel): the
|
|
* build guard then checks nothing. Default true.
|
|
*/
|
|
indexable: boolean
|
|
}
|
|
|
|
export const SEO_MODULE_DEFAULTS: SeoModuleOptions = {
|
|
cms: true,
|
|
breadcrumbs: true,
|
|
links: { newTabHint: true },
|
|
rootFiles: true,
|
|
resolveFiles: ['pdf'],
|
|
noindexRoutes: [],
|
|
indexable: true,
|
|
}
|
|
|
|
const strings = (v: unknown): string[] => (Array.isArray(v) ? v.filter((e): e is string => typeof e === 'string' && e !== '') : [])
|
|
|
|
export default defineNuxtModule<SeoModuleOptions>({
|
|
meta: {
|
|
name: 'vue-seo-layer',
|
|
configKey: 'seo',
|
|
},
|
|
defaults: SEO_MODULE_DEFAULTS,
|
|
setup(options, nuxt) {
|
|
const noindexRoutes = strings(options.noindexRoutes)
|
|
const indexable = options.indexable !== false
|
|
// The app's own ssr flag, read before Nuxt's server setup may rewrite
|
|
// a global ssr: false into a '/**' route rule.
|
|
const ssr = nuxt.options.ssr !== false
|
|
const inlineRouteRules = nuxt.options.experimental?.inlineRouteRules === true
|
|
|
|
// Build guard (D-70). Nitro's config hook runs once every layer and
|
|
// module has merged its rules into it; there the declared noindex
|
|
// routes get their X-Robots-Tag rule. The pages are resolved after
|
|
// that, so the check runs when both are known: once the pages are
|
|
// resolved, or before the Nitro build for an app without pages. It
|
|
// reads the final Nitro rules (nuxt.options.routeRules, nitro.routeRules
|
|
// and every layer's), the .client.vue pages and the inline route rules
|
|
// flag.
|
|
let nitroRules: Record<string, unknown> | null = null
|
|
let pages: GuardPage[] | null = null
|
|
let checked = false
|
|
const logger = useLogger('vue-seo-layer')
|
|
const guard = () => {
|
|
if (nitroRules === null) return
|
|
checked = true
|
|
const offenders = findOffenders({
|
|
ssr,
|
|
indexable,
|
|
routeRules: { ...(nuxt.options.routeRules ?? {}), ...nitroRules },
|
|
pages: pages ?? [],
|
|
noindexRoutes,
|
|
inlineRouteRules,
|
|
})
|
|
if (offenders.length === 0) return
|
|
const message = guardMessage(offenders)
|
|
// A production build fails; nuxt dev and nuxt prepare (run on
|
|
// install) only warn, so a developer can still fix the page.
|
|
if (nuxt.options.dev || nuxt.options._prepare) logger.warn(message)
|
|
else throw new Error(message)
|
|
}
|
|
nuxt.hook('nitro:config', (config) => {
|
|
config.routeRules ||= {}
|
|
const rules = config.routeRules as Record<string, unknown>
|
|
for (const pattern of noindexRoutes) rules[pattern] = noindexRule(rules[pattern])
|
|
nitroRules = rules
|
|
})
|
|
nuxt.hook('nitro:init', (nitro) => {
|
|
nitroRules = nitro.options.routeRules as Record<string, unknown>
|
|
})
|
|
nuxt.hook('pages:resolved', (resolved) => {
|
|
pages = resolved as GuardPage[]
|
|
guard()
|
|
})
|
|
nuxt.hook('nitro:build:before', (nitro) => {
|
|
nitroRules = nitro.options.routeRules as Record<string, unknown>
|
|
if (!checked || nuxt.options.dev) guard()
|
|
})
|
|
|
|
const runtime = nuxt.options.runtimeConfig as { public: Record<string, unknown> }
|
|
const current = (runtime.public.seo ?? {}) as Record<string, unknown>
|
|
// Only the layer's own keys: the shared config key may carry
|
|
// nuxt-seo-utils options, which stay out of the public runtime config.
|
|
runtime.public.seo = {
|
|
...current,
|
|
cms: options.cms !== false,
|
|
breadcrumbs: options.breadcrumbs !== false,
|
|
links: { ...((current.links as Record<string, unknown>) ?? {}), newTabHint: options.links?.newTabHint !== false },
|
|
rootFiles: options.rootFiles !== false,
|
|
resolveFiles: Array.isArray(options.resolveFiles) ? [...options.resolveFiles] : [...SEO_MODULE_DEFAULTS.resolveFiles],
|
|
noindexRoutes,
|
|
indexable,
|
|
}
|
|
},
|
|
})
|