Skip to content

Commit d269d81

Browse files
authored
fix: add unmapped surrogate key for 404.html (#633) (#634)
fixes #632
1 parent 3dc6656 commit d269d81

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/steps/fetch-404.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,19 @@ export default async function fetch404(state, req, res) {
4040

4141
// set 404 keys in any case
4242
const pathKey = await getPathKey(state);
43-
res.headers.set('x-surrogate-key', `${pathKey} ${contentBusId} ${ref}--${repo}--${owner}_404 ${ref}--${repo}--${owner}_code`);
43+
const keys = [
44+
pathKey,
45+
contentBusId,
46+
`${ref}--${repo}--${owner}_404`,
47+
`${ref}--${repo}--${owner}_code`,
48+
];
49+
50+
if (state.info.unmappedPath) {
51+
keys.push(await getPathKey({
52+
contentBusId,
53+
info: { path: state.info.unmappedPath },
54+
}));
55+
}
56+
57+
res.headers.set('x-surrogate-key', keys.join(' '));
4458
}

test/fixtures/code/helix-config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"folders": {
1212
"/products": "/generic-product",
1313
"/articles/": "/special/default-article",
14-
"/app": "/spa/index.html"
14+
"/app": "/spa/index.html",
15+
"/broken": "/not-a-page"
1516
}
1617
},
1718
"content": {

test/rendering.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,19 @@ describe('Rendering', () => {
407407
assert.strictEqual(body.trim(), '');
408408
});
409409

410+
it('renders 404 for folder mapped not found', async () => {
411+
const { headers, body } = await testRender(new URL('https://helix-pipeline.com/broken/folder'), 'html', 404);
412+
assert.deepStrictEqual(Object.fromEntries(headers.entries()), {
413+
'access-control-allow-origin': '*',
414+
'content-type': 'text/html; charset=utf-8',
415+
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
416+
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
417+
'x-error': 'failed to load /not-a-page.md from content-bus: 404',
418+
'x-surrogate-key': 'gPHXKWdMY_R8KV2Z foo-id super-test--helix-pages--adobe_404 super-test--helix-pages--adobe_code QJqsV4atnOA47sHc',
419+
});
420+
assert.strictEqual(body.trim(), '');
421+
});
422+
410423
it('renders 400 for invalid helix-config', async () => {
411424
loader.rewrite('helix-config.json', 'helix-config.corrupt');
412425
await testRender('no-head-html', 'html', 400);

0 commit comments

Comments
 (0)