feat: CMS head and one JSON-LD graph on every server-rendered page (tracer)

- Nitro middleware resolves page requests once and hands the answer to the
  render (event.context.seo); the route middleware reuses it
- useCmsSeo: html lang, hreflang links, og:locale and exactly one JSON-LD
  script with one @graph (CMS nodes plus useSchemaNode nodes, merged by @id)
- local module (configKey seo): cms (alias of public.seo.enabled),
  breadcrumbs, links.newTabHint, rootFiles, resolveFiles
- GET /api/__seo__/resolve for client navigation through the Nuxt server
- smoke harness: count=, exact header=, noheader=, reqheader=, repeated
  options, resolves/apicalls/noreport lines, --no-site; v0.1.1 suite runs
  unchanged against a backend without /site
This commit is contained in:
Jakub Zych
2026-11-04 09:15:26 +01:00
parent 7d52c5d2e5
commit 160b53b2f5
25 changed files with 1074 additions and 140 deletions

63
tests/graph.test.ts Normal file
View File

@@ -0,0 +1,63 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { mergeGraph } from '../app/utils/graph.ts'
const cms = {
'@context': 'https://schema.org',
'@graph': [
{ '@type': 'WebPage', '@id': 'https://acme.test/x#webpage', name: 'CMS name' },
{ '@type': 'Organization', '@id': 'https://acme.test/#org', name: 'Acme' },
],
}
test('mergeGraph: a layer node with a CMS @id merges into it, CMS values first', () => {
const g = mergeGraph(cms, [{ '@id': 'https://acme.test/x#webpage', name: 'Layer name', inLanguage: 'pl' }])
assert.ok(g)
assert.equal(g['@graph'].length, 2)
assert.deepEqual(g['@graph'][0], { '@id': 'https://acme.test/x#webpage', name: 'CMS name', inLanguage: 'pl', '@type': 'WebPage' })
})
test('mergeGraph: a node with a new @id or none is appended, in order', () => {
const g = mergeGraph(cms, [{ '@type': 'ItemList', '@id': 'https://acme.test/x#itemlist' }, { '@type': 'Thing' }])
assert.ok(g)
assert.deepEqual(
g['@graph'].map((n) => n['@type']),
['WebPage', 'Organization', 'ItemList', 'Thing'],
)
})
test('mergeGraph: no duplicate @id, also within the CMS graph and among layer nodes', () => {
const g = mergeGraph({ '@graph': [{ '@id': 'a', x: 1 }, { '@id': 'a', y: 2 }] }, [{ '@id': 'a', z: 3 }, { '@id': 'b' }, { '@id': 'b', w: 4 }])
assert.ok(g)
const ids = g['@graph'].map((n) => n['@id'])
assert.deepEqual(ids, ['a', 'b'])
assert.deepEqual(g['@graph'][0], { '@id': 'a', x: 1, y: 2, z: 3 })
assert.deepEqual(g['@graph'][1], { '@id': 'b', w: 4 })
})
test('mergeGraph: one context, nested @context removed from nodes', () => {
const g = mergeGraph(null, [{ '@context': 'https://schema.org', '@type': 'FAQPage', '@id': 'f' }])
assert.ok(g)
assert.equal(g['@context'], 'https://schema.org')
assert.deepEqual(g['@graph'], [{ '@type': 'FAQPage', '@id': 'f' }])
})
test('mergeGraph: a single-node jsonld counts as one node', () => {
const g = mergeGraph({ '@context': 'https://schema.org', '@type': 'WebSite', '@id': 'w' }, [])
assert.deepEqual(g, { '@context': 'https://schema.org', '@graph': [{ '@type': 'WebSite', '@id': 'w' }] })
})
test('mergeGraph: nothing to emit is null (no JSON-LD script)', () => {
assert.equal(mergeGraph(null, []), null)
assert.equal(mergeGraph(undefined), null)
assert.equal(mergeGraph({ '@graph': [] }, [null, 'x', 3]), null)
assert.equal(mergeGraph('nope', []), null)
})
test('mergeGraph: inputs are not mutated', () => {
const input = JSON.parse(JSON.stringify(cms))
const node = { '@id': 'https://acme.test/x#webpage', extra: true }
mergeGraph(input, [node])
assert.deepEqual(input, cms)
assert.deepEqual(node, { '@id': 'https://acme.test/x#webpage', extra: true })
})

31
tests/pagePath.test.ts Normal file
View File

@@ -0,0 +1,31 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { isPagePath } from '../app/utils/pagePath.ts'
test('isPagePath: Nuxt, layer and API paths are skipped', () => {
for (const p of ['/_nuxt/x', '/_nuxt/entry.js', '/__nuxt_error?url=/x', '/_seo/api/v1/resolve', '/_journal/api/v1/posts', '/api/x', '/api', '/api?x=1']) {
assert.equal(isPagePath(p), false, p)
}
})
test('isPagePath: asset and root files are skipped', () => {
for (const p of ['/favicon.ico', '/robots.txt', '/sitemap.xml', '/google0123.html', '/img/a.webp', '/fonts/x.woff2', '/blog/x/_payload.json', '/doc.pdf']) {
assert.equal(isPagePath(p), false, p)
}
})
test('isPagePath: pages are resolved, with their query', () => {
for (const p of ['/', '/blog/x', '/blog?page=2', '/autor/info@golem15.com', '/a.b/c', '/v1.2']) {
assert.equal(isPagePath(p), true, p)
}
})
test('isPagePath: resolveFiles extensions are resolved', () => {
assert.equal(isPagePath('/doc.pdf', ['pdf']), true)
assert.equal(isPagePath('/DOC.PDF', ['pdf']), true)
assert.equal(isPagePath('/a.png', ['pdf']), false)
})
test('isPagePath: no protocol-relative or relative input', () => {
for (const p of ['//evil.test/x', 'blog', '']) assert.equal(isPagePath(p), false, p)
})

View File

@@ -4,6 +4,7 @@ import {
SEO_STAGING_ROBOTS,
seoAbsolute,
seoAnswer,
seoEnabled,
seoHeadInput,
seoRedirectTarget,
seoResolvableUrl,
@@ -122,3 +123,45 @@ test('paths in canonical and social URLs become absolute on the request origin',
assert.equal(meta['og:image'], 'https://site.test/img.png')
assert.equal(meta['og:url'], 'https://site.test/about')
})
test('seoHeadInput: v0.2.0 keys add html lang, og:locale and hreflang links', () => {
const answer = {
...okAnswer({ og: { title: '', description: '', image: '', url: '', locale: 'pl_PL', locale_alternates: ['en_US', 'de_DE'] } }),
html_lang: 'pl',
alternates: [
{ hreflang: 'pl', url: 'https://acme.test/about' },
{ hreflang: 'x-default', url: '/about' },
],
}
const head = seoHeadInput(answer, 'https://site.test')
assert.deepEqual(head.htmlAttrs, { lang: 'pl' })
assert.ok(head.meta.some((m) => m.property === 'og:locale' && m.content === 'pl_PL'))
assert.deepEqual(
head.meta.filter((m) => m.property === 'og:locale:alternate').map((m) => m.content),
['en_US', 'de_DE'],
)
assert.deepEqual(
head.link.filter((l) => l.rel === 'alternate'),
[
{ rel: 'alternate', hreflang: 'pl', href: 'https://acme.test/about', key: 'alt-pl' },
{ rel: 'alternate', hreflang: 'x-default', href: 'https://site.test/about', key: 'alt-x-default' },
],
)
})
test('seoHeadInput: an answer without the v0.2.0 keys gives the v0.1.1 head', () => {
const head = seoHeadInput(okAnswer(), 'https://site.test')
assert.equal(head.htmlAttrs, undefined)
assert.ok(!head.meta.some((m) => m.property?.startsWith('og:locale')))
assert.ok(!head.link.some((l) => l.rel === 'alternate'))
})
test('seoEnabled: enabled and its alias cms, booleans or strings', () => {
assert.equal(seoEnabled(undefined), true)
assert.equal(seoEnabled({}), true)
assert.equal(seoEnabled({ enabled: true, cms: true }), true)
assert.equal(seoEnabled({ enabled: false }), false)
assert.equal(seoEnabled({ enabled: 'false' }), false)
assert.equal(seoEnabled({ cms: false }), false)
assert.equal(seoEnabled({ enabled: true, cms: 'false' }), false)
})