Lets the test build present itself under its own address

VITE_SITE_URL sets the origin used for canonical, og:url, og:image, JSON-LD
and the sitemap. Only the test build sets it (https://test.andreaslop.nl);
the production artifact keeps https://andreaslop.nl. Only a clean https
origin (or http on localhost) is accepted, anything else falls back to
production.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 11:50:11 +01:00
co-authored by Claude Sonnet 5.5
parent 6ff415e1bc
commit 9eeb05f8fa
7 changed files with 93 additions and 7 deletions
+33
View File
@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { PRODUCTION_SITE_URL, resolveSiteUrl } from './siteUrl';
describe('resolveSiteUrl', () => {
it('uses the production address without a setting', () => {
expect(resolveSiteUrl(undefined)).toBe(PRODUCTION_SITE_URL);
expect(resolveSiteUrl('')).toBe(PRODUCTION_SITE_URL);
expect(resolveSiteUrl(42)).toBe(PRODUCTION_SITE_URL);
});
it.each([
['https://test.andreaslop.nl', 'https://test.andreaslop.nl'],
['https://test.andreaslop.nl/', 'https://test.andreaslop.nl'],
[' https://test.andreaslop.nl ', 'https://test.andreaslop.nl'],
['HTTPS://Test.AndreaSlop.nl', 'https://test.andreaslop.nl'],
['http://localhost:4173', 'http://localhost:4173'],
])('accepts the origin %s', (value, expected) => {
expect(resolveSiteUrl(value)).toBe(expected);
});
it.each([
'test.andreaslop.nl',
'http://test.andreaslop.nl',
'ftp://test.andreaslop.nl',
'https://test.andreaslop.nl/pad',
'https://test.andreaslop.nl/?a=1',
'https://test.andreaslop.nl/#x',
'https://gebruiker:wachtwoord@test.andreaslop.nl',
'javascript:alert(1)',
])('falls back to production for %s', (value) => {
expect(resolveSiteUrl(value)).toBe(PRODUCTION_SITE_URL);
});
});
+27
View File
@@ -0,0 +1,27 @@
// The address the site presents itself under: canonical, og:url, og:image,
// twitter:image, JSON-LD and the sitemap are all built from it.
export const PRODUCTION_SITE_URL = 'https://andreaslop.nl';
// What a build may set through VITE_SITE_URL (only the test build does, so a
// shared link from the test site shows its own address and picture). Only an
// origin counts: https (http for localhost, to try a build on this machine),
// no path, query or hash. Anything else, or nothing, gives the production
// address, so a typo can never send the production site to a wrong domain.
export function resolveSiteUrl(configured: unknown): string {
if (typeof configured !== 'string') return PRODUCTION_SITE_URL;
let url: URL;
try {
url = new URL(configured.trim());
} catch {
return PRODUCTION_SITE_URL;
}
const secure = url.protocol === 'https:';
const local = url.protocol === 'http:' && url.hostname === 'localhost';
const originOnly =
url.pathname === '/' &&
url.search === '' &&
url.hash === '' &&
url.username === '' &&
url.password === '';
return (secure || local) && originOnly ? url.origin : PRODUCTION_SITE_URL;
}