Redirects of the old WordPress addresses as redirects.json (format of the CMS host)

The list is in config/redirectRules.ts (exact paths, from the old sitemap),
written into dist/client by the build; check-artifact.sh requires it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 21:24:08 +01:00
co-authored by Claude Sonnet 5.5
parent 1f087581d4
commit 95f16295f2
6 changed files with 388 additions and 2 deletions
+173
View File
@@ -0,0 +1,173 @@
import { describe, expect, it } from 'vitest';
import {
MAX_REDIRECTS,
REDIRECT_FORMAT_VERSION,
redirectProblems,
redirectRules,
redirectTargets,
redirectsJson,
type RedirectRule,
} from './redirectRules';
// What the host does with the file (the format is in docs/redirect-rules.md);
// the host has its own implementation and tests
function resolve(rules: readonly RedirectRule[], rawPath: string) {
const path = rawPath.toLowerCase().replace(/\/+$/, '') || '/';
return rules.find((rule) => rule.from.toLowerCase() === path);
}
describe('redirectRules', () => {
it('has no problems', () => {
expect(redirectProblems(redirectRules)).toEqual([]);
});
it('sends every 301 to a page of the site or the sitemap', () => {
for (const rule of redirectRules.filter((r) => r.status === 301)) {
expect(redirectTargets).toContain(rule.to);
}
});
it.each([
['/sitemap_index.xml', 301, '/sitemap.xml'],
['/wp-sitemap.xml', 301, '/sitemap.xml'],
['/page-sitemap.xml', 301, '/sitemap.xml'],
['/portfolio-item-sitemap.xml', 301, '/sitemap.xml'],
['/masonry-gallery-category-sitemap.xml', 301, '/sitemap.xml'],
['/portfolio-tag-sitemap.xml', 301, '/sitemap.xml'],
['/testimonials-category-sitemap.xml', 301, '/sitemap.xml'],
['/portfolio-item/', 301, '/portfolio'],
['/portfolio-item/decoration/', 301, '/portfolio'],
['/portfolio-item/wedding-dress/', 301, '/portfolio'],
['/portfolio-item/best-girls/', 301, '/portfolio'],
['/portfolio-item/mm-1/', 301, '/portfolio'],
['/portfolio-item/wedding-cake/', 301, '/portfolio'],
['/portfolio-item/mm2/', 301, '/portfolio'],
['/portfolio-item/cake-smash-1/', 301, '/portfolio'],
['/portfolio-item/hairstyles/', 301, '/portfolio'],
['/portfolio-item/360/', 301, '/portfolio'],
['/portfolio-item/366/', 301, '/portfolio'],
['/portfolio-item/353/', 301, '/portfolio'],
['/portfolio-item/homepage-foto/', 301, '/portfolio'],
['/portfolio-item/getting-ready/', 301, '/portfolio'],
['/portfolio-item/m1/', 301, '/portfolio'],
['/portfolio-item/f1/', 301, '/portfolio'],
['/Portfolio-Item/MM1', undefined, undefined],
['/portfolio-item/MM-1', 301, '/portfolio'],
['/portfolio-tag/homepage/', 301, '/portfolio'],
['/portfolio-tag/portfolio-gallerij/', 301, '/portfolio'],
['/portfolio-tag/simple/', 301, '/portfolio'],
['/testimonials-category/photo/', 301, '/'],
['/masonry-gallery-category/test-masonry-gallery/', 410, undefined],
['/feed/', 410, undefined],
['/comments/feed/', 410, undefined],
])('%s gives %s %s', (path, status, to) => {
const rule = resolve(redirectRules, path);
expect(rule?.status).toBe(status);
expect(rule?.to).toBe(to);
});
it('leaves the real pages and their canonical variants to the host', () => {
for (const path of [
'/',
'/over-mij',
'/over-mij/',
'/portfolio',
'/soorten-fotoshoots/newborn',
'/contact',
'/robots.txt',
'/sitemap.xml',
'/assets/index-abc.js',
'/admin/',
'/api/v1/offerings',
'/wp-login.php',
]) {
expect(resolve(redirectRules, path)).toBeUndefined();
}
});
});
describe('redirectProblems', () => {
const rule = (extra: Partial<RedirectRule>): RedirectRule => ({
from: '/a',
to: '/b',
status: 301,
...extra,
});
it.each([
[rule({ from: 'a' }), 'not a usable path'],
[rule({ from: '/' }), 'not a usable path'],
[rule({ from: '//a' }), 'not a usable path'],
[rule({ from: '/a/../b' }), 'not a usable path'],
[rule({ from: '/a?x=1' }), 'not a usable path'],
[rule({ from: '/a#b' }), 'not a usable path'],
[rule({ from: '/Admin/x' }), 'answered by the host'],
[rule({ from: '/api/v1' }), 'answered by the host'],
[rule({ from: '/health' }), 'answered by the host'],
[rule({ to: undefined }), 'needs a path'],
[rule({ to: 'https://evil.example/' }), 'needs a path'],
[rule({ to: '//evil.example' }), 'needs a path'],
[rule({ to: '/A/' }), 'the same'],
[rule({ status: 410 }), 'only a 301 has a target'],
[rule({ status: 404 }), 'only a 301 has a target'],
])('flags %j', (bad, text) => {
expect(redirectProblems([bad]).join()).toContain(text);
});
it('flags a duplicate, also in another case or with a slash', () => {
expect(redirectProblems([rule({}), rule({})]).join()).toContain(
'duplicate',
);
expect(
redirectProblems([
rule({ from: '/A' }),
rule({ from: '/a/' }),
]).join(),
).toContain('duplicate');
});
it('flags a chain', () => {
expect(
redirectProblems([
rule({ from: '/a', to: '/b' }),
rule({ from: '/b', to: '/c' }),
]).join(),
).toContain('is a rule too');
});
it('flags too many rules', () => {
const many = Array.from({ length: MAX_REDIRECTS + 1 }, (_, i) =>
rule({ from: `/p${i}`, to: '/x' }),
);
expect(redirectProblems(many).join()).toContain('more than');
});
it('accepts a 404 and a 410 without a target', () => {
expect(
redirectProblems([
rule({ status: 404, to: undefined }),
rule({ from: '/b', status: 410, to: undefined }),
]),
).toEqual([]);
});
});
describe('redirectsJson', () => {
it('writes the version and the redirects', () => {
const parsed = JSON.parse(redirectsJson());
expect(parsed.version).toBe(REDIRECT_FORMAT_VERSION);
expect(parsed.redirects).toEqual(redirectRules);
expect(Object.keys(parsed)).toEqual(['version', 'redirects']);
expect(redirectsJson().endsWith('\n')).toBe(true);
});
it('stays far below the size limit of the host', () => {
expect(redirectsJson().length).toBeLessThan(100_000);
});
it('refuses a list with problems', () => {
expect(() => redirectsJson([{ from: 'x', status: 404 }])).toThrow(
'Invalid redirect rules',
);
});
});
+155
View File
@@ -0,0 +1,155 @@
// Redirects for the old WordPress addresses of andreaslop.nl (decision of
// Bryan, 2026-11-18; the inventory is aidlc-docs/features/asf-production-iis/
// oude-urls.md). The site is the source: this list is written into the build
// as redirects.json and the CMS host reads it, the same on test and on
// production. The format is the one of the host (design of SMC, 2026-11-18):
// exact paths only, no patterns; see docs/redirect-rules.md.
//
// Not in this list: the slash, case and .html variants of the real pages
// (/over-mij/ and so on; the host redirects those canonically, see
// config/canonicalUrls.ts), query-string addresses (?page_id=, decision of
// Bryan) and addresses that are a 404 anyway (/wp-admin, /wp-login.php,
// /xmlrpc.php, /wp-json, /wp-content, /category, /tag, /author, /?s=): an
// unknown path gives the 404 page without a rule.
import { pagePaths } from './canonicalUrls.ts';
export type RedirectStatus = 301 | 404 | 410;
export interface RedirectRule {
// A path with one leading slash. The host matches it case insensitive and
// with or without a slash at the end (so give it once).
from: string;
// Only for 301: a path of this site
to?: string;
status: RedirectStatus;
}
export const REDIRECT_FORMAT_VERSION = 1;
export const MAX_REDIRECTS = 5000;
// The host answers these paths itself; a rule there would never be read
const RESERVED_FIRST_SEGMENTS = ['admin', 'health', 'api'];
const permanent = (from: string, to: string): RedirectRule => ({
from,
to,
status: 301,
});
const gone = (from: string): RedirectRule => ({ from, status: 410 });
// The old sitemaps: search engines find the new one
const sitemaps = [
'/sitemap_index.xml',
'/wp-sitemap.xml',
'/page-sitemap.xml',
'/portfolio-item-sitemap.xml',
'/masonry-gallery-category-sitemap.xml',
'/portfolio-tag-sitemap.xml',
'/testimonials-category-sitemap.xml',
];
// The gallery items of the theme (from the old sitemap, 2026-11-18): the
// portfolio is the nearest page
const portfolioItems = [
'decoration',
'wedding-dress',
'best-girls',
'mm-1',
'wedding-cake',
'mm2',
'cake-smash-1',
'hairstyles',
'360',
'366',
'353',
'homepage-foto',
'getting-ready',
'm1',
'f1',
];
const portfolioTags = ['homepage', 'portfolio-gallerij', 'simple'];
export const redirectRules: readonly RedirectRule[] = [
...sitemaps.map((from) => permanent(from, '/sitemap.xml')),
permanent('/portfolio-item', '/portfolio'),
...portfolioItems.map((name) =>
permanent(`/portfolio-item/${name}`, '/portfolio'),
),
...portfolioTags.map((name) =>
permanent(`/portfolio-tag/${name}`, '/portfolio'),
),
// The theme archive of testimonials: they are on the home page
permanent('/testimonials-category/photo', '/'),
// Theme demo content and the feeds: no equivalent and no berichten
gone('/masonry-gallery-category/test-masonry-gallery'),
gone('/feed'),
gone('/comments/feed'),
];
const pathPattern = /^\/[A-Za-z0-9._~/%-]*$/;
const isPath = (value: string): boolean =>
pathPattern.test(value) && !value.startsWith('//') && !value.includes('..');
// What the host compares: lower case, without a slash at the end
const key = (path: string): string =>
path.toLowerCase().replace(/\/+$/, '') || '/';
// The reasons a rule list is not usable; empty when it is. The host validates
// the file again; the tests check this list and what is written from it.
export function redirectProblems(rules: readonly RedirectRule[]): string[] {
const problems: string[] = [];
if (rules.length > MAX_REDIRECTS) {
problems.push(`more than ${MAX_REDIRECTS} rules`);
}
const froms = new Map<string, RedirectRule>();
rules.forEach((rule, index) => {
const label = `rule ${index + 1} (${rule.from})`;
if (!isPath(rule.from) || rule.from === '/') {
problems.push(`${label}: from is not a usable path`);
return;
}
const first = key(rule.from).split('/')[1];
if (RESERVED_FIRST_SEGMENTS.includes(first)) {
problems.push(`${label}: ${first} is answered by the host itself`);
}
if (froms.has(key(rule.from))) problems.push(`${label}: duplicate`);
froms.set(key(rule.from), rule);
if (rule.status === 301) {
if (!rule.to || !isPath(rule.to)) {
problems.push(
`${label}: a 301 needs a path of this site in to`,
);
} else if (key(rule.to) === key(rule.from)) {
problems.push(`${label}: from and to are the same`);
}
} else if (rule.to !== undefined) {
problems.push(`${label}: only a 301 has a target`);
}
});
// No chains: a target is never itself a rule (the host would flatten it,
// or it is gone)
for (const rule of rules) {
if (rule.status === 301 && rule.to && froms.has(key(rule.to))) {
problems.push(
`rule ${rule.from}: the target ${rule.to} is a rule too`,
);
}
}
return problems;
}
// The pages a 301 may point to: every page of the site and the sitemap
export const redirectTargets: readonly string[] = [
...pagePaths,
'/sitemap.xml',
];
export function redirectsJson(
rules: readonly RedirectRule[] = redirectRules,
): string {
const problems = redirectProblems(rules);
if (problems.length > 0) {
throw new Error(`Invalid redirect rules:\n${problems.join('\n')}`);
}
return `${JSON.stringify({ version: REDIRECT_FORMAT_VERSION, redirects: rules }, null, 2)}\n`;
}
+10
View File
@@ -0,0 +1,10 @@
// Writes redirects.json next to the pages of the build (the last step of
// pnpm build, after externalizeInlineScripts). Runs directly with Node.
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { redirectsJson } from './redirectRules.ts';
const outDir = process.argv[2] ?? join('dist', 'client');
mkdirSync(outDir, { recursive: true });
writeFileSync(join(outDir, 'redirects.json'), redirectsJson());
console.log(`redirects.json written to ${outDir}`);
+1 -1
View File
@@ -22,7 +22,7 @@ fail() {
failures=$((failures + 1))
}
for file in index.html 404.html over-mij.html; do
for file in index.html 404.html over-mij.html redirects.json; do
if [ -f "$dist/$file" ]; then
echo "gevonden: $file"
else
+1 -1
View File
@@ -6,7 +6,7 @@
"packageManager": "pnpm@12.11.2",
"scripts": {
"dev": "vite",
"build": "node config/fetchOfferings.ts && tsc && vite build && node config/externalizeInlineScripts.ts",
"build": "node config/fetchOfferings.ts && tsc && vite build && node config/externalizeInlineScripts.ts && node config/writeRedirects.ts",
"preview": "node config/previewServer.ts",
"e2e": "vitest run --config vitest.e2e.config.ts",
"postinstall": "chakra typegen ./src/theme.ts",