Skip to content

fix: set code and content surrogate keys for json 404 (#692) #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/json-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@ 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}`;
}
}

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}`;

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;
}

Expand Down
2 changes: 1 addition & 1 deletion test/json-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,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',
});
});

Expand Down
Loading