fix: leave the i18n copies of a page in other locales out of the route report

With prefix_except_default (or prefix_and_default) @nuxtjs/i18n adds a copy
of every page per other locale (/pl/contact next to /contact). The route
report sent them as pages of their own, so the CMS registry got a second,
untranslated page for each. They are the page's translations: the report
now skips routes named with another locale's suffix (contact___pl).
This commit is contained in:
Jakub Zych
2026-11-04 20:13:05 +01:00
parent 75cc100336
commit b3a0078fb0
4 changed files with 50 additions and 7 deletions

View File

@@ -42,15 +42,35 @@ export function pageMetaRobots(meta: Record<string, unknown> | undefined | null)
/** The layer's own HTML sitemap pages, which the CMS knows already. */
const isLayerPage = (name: unknown) => typeof name === 'string' && name.startsWith('seo-sitemap-')
/**
* The route-name suffixes of @nuxtjs/i18n's copies of a page in the locales
* other than the default one (`contact___pl` with the default separator):
* with prefix_except_default or prefix_and_default the CMS knows those
* addresses as the page's translations, not as pages of their own. Nothing
* without i18n, with no_prefix or without a default locale.
*/
export function localeCopySuffixes(i18n: unknown): string[] {
if (!i18n || typeof i18n !== 'object') return []
const o = i18n as { strategy?: unknown; defaultLocale?: unknown; locales?: unknown; routesNameSeparator?: unknown }
const strategy = typeof o.strategy === 'string' ? o.strategy : 'prefix_except_default'
const def = typeof o.defaultLocale === 'string' ? o.defaultLocale : ''
if (strategy === 'no_prefix' || def === '' || !Array.isArray(o.locales)) return []
const sep = typeof o.routesNameSeparator === 'string' && o.routesNameSeparator !== '' ? o.routesNameSeparator : '___'
const codes = o.locales.map((l) => (typeof l === 'string' ? l : l && typeof l === 'object' ? (l as { code?: unknown }).code : undefined))
return codes.filter((c): c is string => typeof c === 'string' && c !== '' && c !== def).map((c) => `${sep}${c}`)
}
/**
* The paths without parameters that may be indexed, sorted: no page whose
* meta keeps it out of the index (requiresAuth, a noindex seo.robots), none
* under seo.noindexRoutes, none of the layer's HTML sitemap pages and no
* client-only page.
* under seo.noindexRoutes, none of the layer's HTML sitemap pages, no
* client-only page and no i18n copy of a page in another locale (a route
* whose name ends in one of localeCopies, see localeCopySuffixes).
*/
export function staticRoutes(pages: readonly RoutePage[], noindexRoutes: readonly string[]): string[] {
export function staticRoutes(pages: readonly RoutePage[], noindexRoutes: readonly string[], localeCopies: readonly string[] = []): string[] {
const out = new Set<string>()
for (const page of flattenPages(pages) as Array<RoutePage & { fullPath: string }>) {
if (typeof page.name === 'string' && localeCopies.some((suffix) => page.name!.endsWith(suffix))) continue
// A parent with children renders through one of them.
if (Array.isArray(page.children) && page.children.some((c) => c.path === '')) continue
const path = page.fullPath

View File

@@ -12,7 +12,7 @@ import { fileURLToPath } from 'node:url'
import { addServerTemplate, defineNuxtModule, extendPages, 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'
import { localeCopySuffixes, routePatterns, routesTemplate, staticRoutes, type RoutePage } from './routes.ts'
export interface SeoModuleOptions {
/**
@@ -182,7 +182,8 @@ export default defineNuxtModule<SeoModuleOptions>({
filename: '#seo-routes',
getContents: () => {
const resolved = (pages ?? []) as RoutePage[]
return routesTemplate(staticRoutes(resolved, noindexRoutes), routePatterns(resolved))
const i18n = (nuxt.options as { i18n?: unknown }).i18n
return routesTemplate(staticRoutes(resolved, noindexRoutes, localeCopySuffixes(i18n)), routePatterns(resolved))
},
})
nuxt.hook('nitro:build:before', (nitro) => {