diff --git a/src/json-pipe.js b/src/json-pipe.js index acd58581..554adbf3 100644 --- a/src/json-pipe.js +++ b/src/json-pipe.js @@ -85,7 +85,8 @@ async function fetchJsonContent(state, req, res) { updateLastModified(state, res, extractLastModified(ret.headers)); } else { - state.content.sourceBus = 'content'; + // also add code surrogate key in case json is later added to code bus (#688) + state.content.sourceBus = 'code|content'; res.status = ret.status === 404 ? 404 : 502; res.error = `failed to load ${state.info.resourcePath}: ${ret.status}`; } @@ -93,19 +94,18 @@ async function fetchJsonContent(state, req, res) { async function computeSurrogateKeys(state) { const keys = []; - const pathKey = state.content?.sourceBus === 'code' - ? `${state.ref}--${state.repo}--${state.owner}${state.info.path}` - : `${state.contentBusId}${state.info.path}`; - if (state.info.path === '/config.json') { keys.push(await computeSurrogateKey(`${state.site}--${state.org}_config.json`)); } - keys.push(await computeSurrogateKey(pathKey)); - if (state.content?.sourceBus === 'content') { - keys.push(state.contentBusId); - } else { + if (state.content.sourceBus.includes('code')) { + keys.push(await computeSurrogateKey(`${state.ref}--${state.repo}--${state.owner}${state.info.path}`)); keys.push(`${state.ref}--${state.repo}--${state.owner}_code`); } + if (state.content.sourceBus.includes('content')) { + keys.push(await computeSurrogateKey(`${state.contentBusId}${state.info.path}`)); + keys.push(state.contentBusId); + } + return keys; } diff --git a/test/json-pipe.test.js b/test/json-pipe.test.js index e418122f..58224f02 100644 --- a/test/json-pipe.test.js +++ b/test/json-pipe.test.js @@ -148,7 +148,7 @@ describe('JSON Pipe Test', () => { assert.strictEqual(resp.status, 404); assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), { 'x-error': 'failed to load /config.json: 404', - 'x-surrogate-key': 'U_NW4adJU7Qazf-I kz8SoCaNqfp4ohQo foobar', + 'x-surrogate-key': 'U_NW4adJU7Qazf-I pzrU-nNKQOUYNTEf ref--repo--owner_code kz8SoCaNqfp4ohQo foobar', }); }); @@ -165,7 +165,7 @@ describe('JSON Pipe Test', () => { assert.strictEqual(resp.status, 200); assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), { 'content-type': 'application/json', - 'x-surrogate-key': 'U_NW4adJU7Qazf-I kz8SoCaNqfp4ohQo foobar', + 'x-surrogate-key': 'U_NW4adJU7Qazf-I pzrU-nNKQOUYNTEf ref--repo--owner_code kz8SoCaNqfp4ohQo foobar', }); assert.deepStrictEqual(await resp.json(), { public: { @@ -229,7 +229,7 @@ describe('JSON Pipe Test', () => { 'access-control-allow-origin': '*', 'content-security-policy': 'default-src \'self\'', 'x-error': 'failed to load /en/index.json: 404', - 'x-surrogate-key': 'Atrz_qDg26DmSe9a foobar', + 'x-surrogate-key': 'SIMSxecp2CJXqGYs ref--repo--owner_code Atrz_qDg26DmSe9a foobar', }); });