From c5b1e30c38bffe508c6033e72fe3921bebad6c8b Mon Sep 17 00:00:00 2001 From: Tobias Bocanegra Date: Wed, 26 Jun 2024 13:17:11 +0200 Subject: [PATCH] fix: add unmapped surrogate key for 404.html --- src/steps/fetch-404.js | 16 +++++++++++++++- test/rendering.test.js | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/steps/fetch-404.js b/src/steps/fetch-404.js index 7a381fce..22c45fbf 100644 --- a/src/steps/fetch-404.js +++ b/src/steps/fetch-404.js @@ -40,5 +40,19 @@ export default async function fetch404(state, req, res) { // set 404 keys in any case const pathKey = await getPathKey(state); - res.headers.set('x-surrogate-key', `${pathKey} ${contentBusId} ${ref}--${repo}--${owner}_404 ${ref}--${repo}--${owner}_code`); + const keys = [ + pathKey, + contentBusId, + `${ref}--${repo}--${owner}_404`, + `${ref}--${repo}--${owner}_code`, + ]; + + if (state.info.unmappedPath) { + keys.push(await getPathKey({ + contentBusId, + info: { path: state.info.unmappedPath }, + })); + } + + res.headers.set('x-surrogate-key', keys.join(' ')); } diff --git a/test/rendering.test.js b/test/rendering.test.js index 68c8ea2c..9ee7f537 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -120,6 +120,7 @@ const DEFAULT_CONFIG = { '/products': '/generic-product', '/articles/': '/special/default-article', '/app': '/spa/index.html', + '/broken': '/not-a-page', }, headers: HEADERS, metadata: { @@ -527,6 +528,18 @@ describe('Rendering', () => { assert.strictEqual(body.trim(), ''); }); + it('renders 404 for folder mapped not found', async () => { + const { headers, body } = await testRender(new URL('https://helix-pipeline.com/broken/folder'), 'html', 404); + assert.deepStrictEqual(Object.fromEntries(headers.entries()), { + 'access-control-allow-origin': '*', + 'content-type': 'text/html; charset=utf-8', + link: '; rel=modulepreload; as=script; crossorigin=use-credentials', + 'x-error': 'failed to load /not-a-page.md from content-bus: 404', + 'x-surrogate-key': 'gPHXKWdMY_R8KV2Z foo-id super-test--helix-pages--adobe_404 super-test--helix-pages--adobe_code QJqsV4atnOA47sHc', + }); + assert.strictEqual(body.trim(), ''); + }); + it('renders 301 for redirect file', async () => { loader.headers('one-section.md', 'x-amz-meta-redirect-location', 'https://www.adobe.com'); const ret = await render(new URL('https://localhost/one-section'), '', 301);