Lets the deployment check cope with pages the CMS host has filled
The check after a deploy compared the server's answer with dist byte for byte, which fails once the host writes current data into the markers. Both sides are now normalised first (regions, data block, offers, inline prices, site address); everything else still has to match. The stand-in host no longer injects the 404 page, like the real one. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -37,6 +37,10 @@ Committing, pushing and deploying a feature branch to test is allowed for the se
|
||||
|
||||
`feature/contact-form-cms` carries commit **`d449e76` "TEST ONLY: enable the contact form for the ASF test deploy"**, which sets `contactForm.enabled: true` in `web/src/content/siteContent.ts` so the ASF test host shows the real form. It must not reach `master` (the form stays off in production until Bryan gives an explicit go). Before merging, revert it (`git revert d449e76`, or drop it from the branch) and confirm `enabled` is `false` again. The packages branch is based on the contact-form branch, so the merge order is `contact-form-cms` first, then `asf-packages-cms`. Both merges need Bryan's go.
|
||||
|
||||
## The check after a deploy and the host injection
|
||||
|
||||
`deploy/verify-deployment.sh` compares what the server sends with the files in `dist`. Once the CMS host injects (packages, banners, prices, JSON-LD price, site address), the pages differ from `dist` by design, so both sides are first normalised with `config/normalizeInjected.ts` (the contents of the CMS regions, the data block, `offers` in structured data, inline prices and the site address are taken out). Everything else must still be equal byte for byte, so an old build or a wrong server config is still caught; the 404 page is not injected and is compared as it is. The preview server can stand in for the host (`node config/previewServer.ts --cms-data=<json>`), which is how the script is tried locally.
|
||||
|
||||
## Own address of the test site
|
||||
|
||||
The test site stands on its own: in the test build `VITE_SITE_URL` is `https://test.andreaslop.nl`, so canonical, `og:url`, `og:image`, the JSON-LD ids and urls and `sitemap.xml` all point to the test host. A link shared from the test site (WhatsApp, Discord) therefore shows its own picture, not one from production. The production artifact (job `check`) is built without the variable and keeps `https://andreaslop.nl`. `src/lib/siteUrl.ts` only accepts an origin (https, or http on localhost); anything else falls back to production, so a typo cannot move production to another domain. This is a build setting, not TEST ONLY code: nothing has to be reverted before a merge. `robots.txt` on the test host is the nginx one (`Disallow: /`, plus `X-Robots-Tag: noindex`), not `public/robots.txt`, so the test site stays out of search engines. To try it locally: `VITE_SITE_URL=http://localhost:4173 pnpm build`.
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { injectCms, type LiveData } from './hostInjector.ts';
|
||||
import { normalizeInjected } from './normalizeInjected.ts';
|
||||
|
||||
const AUTHORED = 'https://andreaslop.nl';
|
||||
|
||||
const baked = (name: string) => {
|
||||
try {
|
||||
return readFileSync(
|
||||
join(process.cwd(), 'dist', 'client', name),
|
||||
'utf8',
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const live: LiveData = {
|
||||
offerings: [
|
||||
{
|
||||
slug: 'newborn',
|
||||
title: 'Newborn',
|
||||
price: '€ 999',
|
||||
features: ['x'],
|
||||
displayOrder: 0,
|
||||
},
|
||||
],
|
||||
banners: { top: { text: 'Boven' }, bottom: null },
|
||||
};
|
||||
|
||||
describe('normalizeInjected', () => {
|
||||
it('makes a page equal to the same page after the host filled the markers', () => {
|
||||
const pages = ['index.html', 'soorten-fotoshoots/newborn.html'];
|
||||
for (const page of pages) {
|
||||
const html = baked(page);
|
||||
// The tests of the unit run need no build; skip a page that is not built
|
||||
if (html === null) continue;
|
||||
const injected = injectCms(html, {
|
||||
data: live,
|
||||
authoredBaseUrl: AUTHORED,
|
||||
publicBaseUrl: 'https://test.example.nl',
|
||||
});
|
||||
expect(injected).not.toBe(html);
|
||||
expect(
|
||||
normalizeInjected(injected, [
|
||||
AUTHORED,
|
||||
'https://test.example.nl',
|
||||
]),
|
||||
).toBe(
|
||||
normalizeInjected(html, [AUTHORED, 'https://test.example.nl']),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const page = (inner: string) => `<body>${inner}</body>`;
|
||||
|
||||
it('empties the regions but keeps the markers', () => {
|
||||
expect(
|
||||
normalizeInjected(
|
||||
page('<!--CMS:OFFERINGS-HTML-->a<!--/CMS:OFFERINGS-HTML-->'),
|
||||
),
|
||||
).toBe(page('<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->'));
|
||||
});
|
||||
|
||||
it('removes the data block with or without its marker', () => {
|
||||
const block =
|
||||
'<script type="application/json" id="cms-data">{}</script>';
|
||||
expect(normalizeInjected(page('<!--CMS:DATA-->' + block))).toBe(
|
||||
page(''),
|
||||
);
|
||||
expect(normalizeInjected(page(block))).toBe(page(''));
|
||||
expect(normalizeInjected(page('<!--CMS:DATA-->'))).toBe(page(''));
|
||||
});
|
||||
|
||||
it('drops offers from structured data and the marker before it', () => {
|
||||
const withOffers =
|
||||
'<!--CMS:OFFER-JSON-LD slug="x"--><script type="application/ld+json">{"a":1,"offers":{"price":1}}</script>';
|
||||
const without =
|
||||
'<script type="application/ld+json">{ "a": 1 }</script>';
|
||||
expect(normalizeInjected(page(withOffers))).toBe(
|
||||
normalizeInjected(page(without)),
|
||||
);
|
||||
});
|
||||
|
||||
it('leaves structured data that is not JSON, so it still differs', () => {
|
||||
const broken = '<script type="application/ld+json">{kapot</script>';
|
||||
expect(normalizeInjected(page(broken))).toBe(page(broken));
|
||||
});
|
||||
|
||||
it('empties inline prices and replaces the site addresses', () => {
|
||||
expect(
|
||||
normalizeInjected(
|
||||
page('<span data-cms-price="x" a>€ 5</span> https://a.nl/x'),
|
||||
['https://a.nl'],
|
||||
),
|
||||
).toBe(page('<span data-cms-price="x" a></span> @SITE@/x'));
|
||||
});
|
||||
|
||||
it('still sees a real difference', () => {
|
||||
expect(normalizeInjected(page('<p>een</p>'))).not.toBe(
|
||||
normalizeInjected(page('<p>twee</p>')),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
// Makes a prerendered page and the same page after the CMS host filled its
|
||||
// markers comparable. The deployment check (deploy/verify-deployment.sh)
|
||||
// compares what the server sends with the file in dist; since the host
|
||||
// writes the current packages, banners, prices and address into the page,
|
||||
// those parts are taken out of both sides first. Everything else must still be
|
||||
// byte for byte equal, so an old build or a wrong server config is still
|
||||
// caught. The rules mirror the host contract in
|
||||
// aidlc-docs/features/asf-cms-host-injection/plan.md.
|
||||
//
|
||||
// Usage: node config/normalizeInjected.ts <input.html> <output.html> [site-url ...]
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
|
||||
const REGION =
|
||||
/(<!--CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML)-->)[\s\S]*?(<!--\/CMS:(?:OFFERINGS-HTML|BANNER-TOP-HTML|BANNER-BOTTOM-HTML)-->)/g;
|
||||
const DATA_BLOCK =
|
||||
/(?:<!--CMS:DATA-->)?<script type="application\/json" id="cms-data">[\s\S]*?<\/script>|<!--CMS:DATA-->/g;
|
||||
const OFFER_MARKER = /<!--CMS:OFFER-JSON-LD slug="[a-z0-9-]+"-->/g;
|
||||
const LD_JSON =
|
||||
/(<script type="application\/ld\+json">)([\s\S]*?)(<\/script>)/g;
|
||||
const PRICE_SPAN = /(<span data-cms-price="[a-z0-9-]+"[^>]*>)[^<]*(<\/span>)/g;
|
||||
|
||||
// JSON-LD: parse, drop every "offers" (the host rewrites or removes them),
|
||||
// write it back the same way for both sides
|
||||
function dropOffers(value: unknown): unknown {
|
||||
if (Array.isArray(value)) return value.map(dropOffers);
|
||||
if (typeof value !== 'object' || value === null) return value;
|
||||
return Object.fromEntries(
|
||||
Object.entries(value)
|
||||
.filter(([key]) => key !== 'offers')
|
||||
.map(([key, child]) => [key, dropOffers(child)]),
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeInjected(
|
||||
html: string,
|
||||
siteUrls: readonly string[] = [],
|
||||
): string {
|
||||
let out = html
|
||||
.replace(REGION, '$1$2')
|
||||
.replace(DATA_BLOCK, '')
|
||||
.replace(OFFER_MARKER, '')
|
||||
.replace(PRICE_SPAN, '$1$2')
|
||||
.replace(
|
||||
LD_JSON,
|
||||
(match, open: string, json: string, close: string) => {
|
||||
try {
|
||||
return `${open}${JSON.stringify(dropOffers(JSON.parse(json)))}${close}`;
|
||||
} catch {
|
||||
// Not JSON: leave it, so a broken block still shows up as a
|
||||
// difference
|
||||
return match;
|
||||
}
|
||||
},
|
||||
);
|
||||
for (const url of siteUrls) out = out.split(url).join('@SITE@');
|
||||
return out;
|
||||
}
|
||||
|
||||
function main(args: string[]) {
|
||||
const [input, output, ...siteUrls] = args;
|
||||
if (!input || !output) {
|
||||
console.error(
|
||||
'Gebruik: node config/normalizeInjected.ts <in.html> <uit.html> [site-url ...]',
|
||||
);
|
||||
process.exitCode = 2;
|
||||
return;
|
||||
}
|
||||
writeFileSync(
|
||||
output,
|
||||
normalizeInjected(readFileSync(input, 'utf8'), siteUrls),
|
||||
);
|
||||
}
|
||||
|
||||
// Only when run as a script, not when a test imports it
|
||||
if (process.argv[1]?.endsWith('normalizeInjected.ts')) {
|
||||
main(process.argv.slice(2));
|
||||
}
|
||||
@@ -312,7 +312,11 @@ export function startPreviewServer(
|
||||
);
|
||||
return;
|
||||
}
|
||||
const injection = file.endsWith('.html') ? options.inject?.() : null;
|
||||
// Like the real host, the 404 page is not injected
|
||||
const injection =
|
||||
file.endsWith('.html') && status === 200
|
||||
? options.inject?.()
|
||||
: null;
|
||||
if (injection) {
|
||||
const body = injectCms(readFileSync(target, 'utf8'), injection);
|
||||
send(
|
||||
|
||||
@@ -115,13 +115,27 @@ check_exact_headers() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Is het antwoord gelijk aan het bestand uit dist? De CMS-host mag de markers in
|
||||
# de pagina's vullen (pakketten, banners, prijzen, JSON-LD-prijs, adres); die
|
||||
# delen worden aan beide kanten weggehaald (config/normalizeInjected.ts), de
|
||||
# rest moet nog steeds byte voor byte gelijk zijn.
|
||||
same_page() {
|
||||
local body="$1" expected="$2" here
|
||||
cmp -s "$body" "$expected" && return 0
|
||||
case "$expected" in *.html) ;; *) return 1 ;; esac
|
||||
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
node "$here/../config/normalizeInjected.ts" "$body" "$work/norm-body.html" "https://andreaslop.nl" "${base%/}" || return 1
|
||||
node "$here/../config/normalizeInjected.ts" "$expected" "$work/norm-dist.html" "https://andreaslop.nl" "${base%/}" || return 1
|
||||
cmp -s "$work/norm-body.html" "$work/norm-dist.html"
|
||||
}
|
||||
|
||||
# Een 200 met de bytes van het verwachte bestand uit dist
|
||||
expect_file() {
|
||||
local path="$1" file="$2"
|
||||
fetch "$path"
|
||||
if [ "$status" != "200" ]; then
|
||||
fail "$path: status $status, verwacht 200"
|
||||
elif ! cmp -s "$work/body" "$dist/$file"; then
|
||||
elif ! same_page "$work/body" "$dist/$file"; then
|
||||
fail "$path: antwoord is niet gelijk aan $file uit dist (oude serverconfig of oude build?)"
|
||||
fi
|
||||
check_security_headers "$path"
|
||||
|
||||
Reference in New Issue
Block a user