fix: canonical and social URLs answered as paths become absolute on the request origin

This commit is contained in:
Jakub Zych
2026-10-31 13:53:30 +01:00
parent ac59f3c80d
commit 7d52c5d2e5
8 changed files with 192 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import {
SEO_STAGING_ROBOTS,
seoAbsolute,
seoAnswer,
seoHeadInput,
seoRedirectTarget,
@@ -108,3 +109,16 @@ test('verification names: one segment, three extensions, never a reserved name',
assert.equal(seoVerificationName(p, reserved), null, p)
}
})
test('paths in canonical and social URLs become absolute on the request origin', () => {
assert.equal(seoAbsolute('/a?b=1', 'https://acme.test'), 'https://acme.test/a?b=1')
assert.equal(seoAbsolute('/a', 'https://acme.test/'), 'https://acme.test/a')
assert.equal(seoAbsolute('https://x.test/a', 'https://acme.test'), 'https://x.test/a')
assert.equal(seoAbsolute('//x.test/a', 'https://acme.test'), '//x.test/a')
assert.equal(seoAbsolute('/a', undefined), '/a')
const head = seoHeadInput(okAnswer({ canonical: '/about', og: { title: '', description: '', image: '/img.png', url: '/about' } }), 'https://site.test')
assert.deepEqual(head.link, [{ rel: 'canonical', href: 'https://site.test/about' }])
const meta = Object.fromEntries(head.meta.map((m) => [m.name ?? m.property, m.content]))
assert.equal(meta['og:image'], 'https://site.test/img.png')
assert.equal(meta['og:url'], 'https://site.test/about')
})