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:
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
+4
-1
@@ -13,13 +13,16 @@ import type {
|
||||
} from './content/types.ts';
|
||||
import { knowsAboutList } from './lib/knowsAboutList.ts';
|
||||
import { richTextToPlain } from './lib/richTextToPlain.ts';
|
||||
import { resolveSiteUrl } from './lib/siteUrl.ts';
|
||||
import { shootPagePath } from './lib/shootPages.ts';
|
||||
import type { BreadcrumbItem, ShootPageView } from './lib/shootPageView.ts';
|
||||
import { visibleSocials } from './lib/visibleSocials.ts';
|
||||
import type { RoutePath } from './navigation.ts';
|
||||
import sitePages from './sitePages.ts';
|
||||
import { configuredSiteUrl } from './siteUrlEnv.ts';
|
||||
|
||||
export const siteUrl = 'https://andreaslop.nl';
|
||||
// Production unless the build sets VITE_SITE_URL (the test build does)
|
||||
export const siteUrl = resolveSiteUrl(configuredSiteUrl());
|
||||
export const siteName = 'Andrea Slop Fotografie';
|
||||
|
||||
// Preview-afbeelding voor WhatsApp, Facebook en LinkedIn (public/og-image.jpg)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Reads VITE_SITE_URL, the one place that touches the environment for the site
|
||||
// address (see lib/siteUrl.ts). In the Vite build (client and prerender) the
|
||||
// value is substituted in `import.meta.env`; the build scripts that run under
|
||||
// plain Node (sitemap check, sitemap plugin) get it from the process
|
||||
// environment. Both see the same value because the build sets it once.
|
||||
export function configuredSiteUrl(): unknown {
|
||||
const fromVite = import.meta.env?.VITE_SITE_URL as unknown;
|
||||
if (fromVite !== undefined) return fromVite;
|
||||
return typeof process === 'undefined'
|
||||
? undefined
|
||||
: process.env.VITE_SITE_URL;
|
||||
}
|
||||
Reference in New Issue
Block a user