Builds the markers, data block and stand-in host for the CMS host injection
The pages carry the CMS markers (data block, list regions, JSON-LD price, inline price); PackagesProvider reads the host's data block after hydration and asks the API only when there is none; the site address comes from the block. A stand-in for the host (config/hostInjector.ts, --cms-data in the preview server) and browser tests check the behaviour with different data. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
// slash aan het eind krijgen één 301 (BR-HST-03, 04). /404.html is een echte
|
||||
// 404; Lighthouse auditeert de 404-pagina via de opt-in route /__audit/404
|
||||
// (--audit-route, BR-HST-22).
|
||||
import { createReadStream, statSync } from 'node:fs';
|
||||
import { createReadStream, readFileSync, statSync } from 'node:fs';
|
||||
import {
|
||||
createServer,
|
||||
type IncomingMessage,
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
import { extname, join, resolve, sep, win32 } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { createGzip } from 'node:zlib';
|
||||
import { injectCms, type InjectOptions } from './hostInjector.ts';
|
||||
import { canonicalRedirect, stripTrailingSlashes } from './canonicalUrls.ts';
|
||||
import {
|
||||
compressibleExtensions,
|
||||
@@ -197,6 +198,10 @@ export interface PreviewOptions {
|
||||
// 202. Productie gebruikt dit nooit; tests kunnen het per pagina met
|
||||
// page.route vervangen (bijvoorbeeld 404 of een captcha).
|
||||
contactStub?: boolean;
|
||||
// Opt-in stand-in for the CMS host's injection (--cms-data=<json file>):
|
||||
// called for every HTML page; the answer is what the host would write into
|
||||
// the markers (see hostInjector.ts), null serves the page as built.
|
||||
inject?: () => InjectOptions | null;
|
||||
}
|
||||
|
||||
const CONTACT_API = '/api/v1/contact-messages';
|
||||
@@ -307,6 +312,21 @@ export function startPreviewServer(
|
||||
);
|
||||
return;
|
||||
}
|
||||
const injection = file.endsWith('.html') ? options.inject?.() : null;
|
||||
if (injection) {
|
||||
const body = injectCms(readFileSync(target, 'utf8'), injection);
|
||||
send(
|
||||
response,
|
||||
status,
|
||||
{
|
||||
'Content-Type': contentTypeOf(file),
|
||||
'Cache-Control': 'no-cache',
|
||||
'Content-Length': Buffer.byteLength(body),
|
||||
},
|
||||
request.method === 'HEAD' ? null : body,
|
||||
);
|
||||
return;
|
||||
}
|
||||
const headers: Record<string, string | number> = {
|
||||
'Content-Type': contentTypeOf(file),
|
||||
};
|
||||
@@ -346,6 +366,7 @@ function parseArguments(args: string[]) {
|
||||
dir: 'dist/client',
|
||||
auditRoute: false,
|
||||
contactStub: false,
|
||||
cmsData: '',
|
||||
};
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const [name, inline] = args[i].split('=', 2);
|
||||
@@ -355,6 +376,7 @@ function parseArguments(args: string[]) {
|
||||
else if (name === '--strictPort') options.strictPort = true;
|
||||
else if (name === '--audit-route') options.auditRoute = true;
|
||||
else if (name === '--contact-stub') options.contactStub = true;
|
||||
else if (name === '--cms-data') options.cmsData = value();
|
||||
}
|
||||
return options;
|
||||
}
|
||||
@@ -362,7 +384,7 @@ function parseArguments(args: string[]) {
|
||||
const MAX_PORT_TRIES = 10;
|
||||
|
||||
async function main(args: string[]) {
|
||||
const { port, strictPort, dir, auditRoute, contactStub } =
|
||||
const { port, strictPort, dir, auditRoute, contactStub, cmsData } =
|
||||
parseArguments(args);
|
||||
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
||||
console.error(`Ongeldige poort: ${port}`);
|
||||
@@ -377,6 +399,24 @@ async function main(args: string[]) {
|
||||
const server = await startPreviewServer(dir, port + attempt, {
|
||||
auditRoute,
|
||||
contactStub,
|
||||
// Read again for every page, so the file can change while the
|
||||
// server runs: { offerings, banners, publicBaseUrl? }
|
||||
...(cmsData
|
||||
? {
|
||||
inject: () => {
|
||||
const file = JSON.parse(
|
||||
readFileSync(cmsData, 'utf8'),
|
||||
) as InjectOptions['data'] & {
|
||||
publicBaseUrl?: string;
|
||||
};
|
||||
return {
|
||||
data: file,
|
||||
authoredBaseUrl: 'https://andreaslop.nl',
|
||||
publicBaseUrl: file.publicBaseUrl,
|
||||
};
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
const address = server.address();
|
||||
const actual =
|
||||
|
||||
Reference in New Issue
Block a user