Monitoring settings (Sentry, Umami) and the CSP origins come from the build environment

One build per environment: PROD_* variables for the production artifact,
TEST_* for the test build and the CSP check. The build fails when a variable
is empty. Tests use the production values from config/monitoringTestEnv.ts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 19:29:51 +01:00
co-authored by Claude Sonnet 5.5
parent 6475696709
commit 49b1d5f0ba
14 changed files with 286 additions and 85 deletions
@@ -1,5 +1,5 @@
import { render } from '@testing-library/react';
import { afterEach, describe, expect, it } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import UmamiAnalytics from './UmamiAnalytics';
const umamiScripts = () =>
@@ -8,6 +8,7 @@ const umamiScripts = () =>
);
afterEach(() => {
vi.unstubAllEnvs();
umamiScripts().forEach((script) => script.remove());
});
@@ -18,18 +19,28 @@ describe('UmamiAnalytics', () => {
});
it.each([
['andreaslop.nl', '3edbb7d1-4388-4f2d-9cef-11ade88ace96'],
['test.andreaslop.nl', '7fb301ab-6ecc-4aea-beee-71bcb6009bfe'],
])('telt op %s met de juiste website', (hostname, websiteId) => {
render(<UmamiAnalytics hostname={hostname} />);
['production', 'andreaslop.nl', '3edbb7d1-4388-4f2d-9cef-11ade88ace96'],
['test', 'test.andreaslop.nl', 'test-website'],
])(
'telt in de %s-build op %s met de juiste website',
(environment, hostname, websiteId) => {
vi.stubEnv('VITE_APP_ENV', environment);
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', websiteId);
render(<UmamiAnalytics hostname={hostname} />);
const [script] = umamiScripts();
expect(script.defer).toBe(true);
expect(script.dataset).toMatchObject({
websiteId,
domains: hostname,
doNotTrack: 'true',
});
const [script] = umamiScripts();
expect(script.defer).toBe(true);
expect(script.dataset).toMatchObject({
websiteId,
domains: hostname,
doNotTrack: 'true',
});
},
);
it('voegt geen script toe op de host van de andere omgeving', () => {
render(<UmamiAnalytics hostname="test.andreaslop.nl" />);
expect(umamiScripts()).toHaveLength(0);
});
it('voegt het script maar één keer toe', () => {
@@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { monitoringFor, umamiOrigin } from '@/monitoring/config';
import { monitoringFor } from '@/monitoring/config';
const SCRIPT_ELEMENT_ID = 'umami-analytics-script';
@@ -16,7 +16,7 @@ export default function UmamiAnalytics({ hostname }: { hostname?: string }) {
const script = document.createElement('script');
script.id = SCRIPT_ELEMENT_ID;
script.src = `${umamiOrigin}/script.js`;
script.src = monitoring.umamiScriptUrl;
script.defer = true;
script.dataset.websiteId = monitoring.umamiWebsiteId;
script.dataset.domains = host;