Skip to content

Commit 4f2e381

Browse files
committed
fix: set code and content surrogate keys for json 404
fixes #688
1 parent 6e6bc37 commit 4f2e381

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/json-pipe.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,27 @@ async function fetchJsonContent(state, req, res) {
8585

8686
updateLastModified(state, res, extractLastModified(ret.headers));
8787
} else {
88-
state.content.sourceBus = 'content';
88+
// also add code surrogate key in case json is later added to code bus (#688)
89+
state.content.sourceBus = 'code|content';
8990
res.status = ret.status === 404 ? 404 : 502;
9091
res.error = `failed to load ${state.info.resourcePath}: ${ret.status}`;
9192
}
9293
}
9394

9495
async function computeSurrogateKeys(state) {
9596
const keys = [];
96-
const pathKey = state.content?.sourceBus === 'code'
97-
? `${state.ref}--${state.repo}--${state.owner}${state.info.path}`
98-
: `${state.contentBusId}${state.info.path}`;
99-
10097
if (state.info.path === '/config.json') {
10198
keys.push(await computeSurrogateKey(`${state.site}--${state.org}_config.json`));
10299
}
103-
keys.push(await computeSurrogateKey(pathKey));
104-
if (state.content?.sourceBus === 'content') {
105-
keys.push(state.contentBusId);
106-
} else {
100+
if (state.content.sourceBus.includes('code')) {
101+
keys.push(await computeSurrogateKey(`${state.ref}--${state.repo}--${state.owner}${state.info.path}`));
107102
keys.push(`${state.ref}--${state.repo}--${state.owner}_code`);
108103
}
104+
if (state.content.sourceBus.includes('content')) {
105+
keys.push(await computeSurrogateKey(`${state.contentBusId}${state.info.path}`));
106+
keys.push(state.contentBusId);
107+
}
108+
109109
return keys;
110110
}
111111

test/json-pipe.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('JSON Pipe Test', () => {
148148
assert.strictEqual(resp.status, 404);
149149
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
150150
'x-error': 'failed to load /config.json: 404',
151-
'x-surrogate-key': 'U_NW4adJU7Qazf-I kz8SoCaNqfp4ohQo foobar',
151+
'x-surrogate-key': 'U_NW4adJU7Qazf-I pzrU-nNKQOUYNTEf ref--repo--owner_code kz8SoCaNqfp4ohQo foobar',
152152
});
153153
});
154154

@@ -165,7 +165,7 @@ describe('JSON Pipe Test', () => {
165165
assert.strictEqual(resp.status, 200);
166166
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
167167
'content-type': 'application/json',
168-
'x-surrogate-key': 'U_NW4adJU7Qazf-I kz8SoCaNqfp4ohQo foobar',
168+
'x-surrogate-key': 'U_NW4adJU7Qazf-I pzrU-nNKQOUYNTEf ref--repo--owner_code kz8SoCaNqfp4ohQo foobar',
169169
});
170170
assert.deepStrictEqual(await resp.json(), {
171171
public: {
@@ -229,7 +229,7 @@ describe('JSON Pipe Test', () => {
229229
'access-control-allow-origin': '*',
230230
'content-security-policy': 'default-src \'self\'',
231231
'x-error': 'failed to load /en/index.json: 404',
232-
'x-surrogate-key': 'Atrz_qDg26DmSe9a foobar',
232+
'x-surrogate-key': 'SIMSxecp2CJXqGYs ref--repo--owner_code Atrz_qDg26DmSe9a foobar',
233233
});
234234
});
235235

0 commit comments

Comments
 (0)