Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 797aad2

Browse files
feat: add ref--repo--owner_code surrogate key for pipeline responses depending on codebus resources
1 parent edaa518 commit 797aad2

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

src/json-pipe.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ async function fetchJsonContent(state, req, res) {
6262
res.headers.delete('content-type');
6363
res.headers.set('location', redirectLocation);
6464
const keys = [];
65-
keys.push(await computeSurrogateKey(`${contentBusId}${info.path}`));
6665
if (state.content.sourceBus === 'content') {
66+
keys.push(await computeSurrogateKey(`${contentBusId}${info.path}`));
6767
keys.push(contentBusId);
68+
} else {
69+
keys.push(`${ref}--${repo}--${owner}_code`);
70+
keys.push(await computeSurrogateKey(`${ref}--${repo}--${owner}${info.path}`));
6871
}
6972
res.headers.set('x-surrogate-key', keys.join(' '));
7073
res.error = 'moved';

src/robots-pipe.js

-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ async function computeSurrogateKeys(state) {
120120

121121
const pathKey = `${state.ref}--${state.repo}--${state.owner}${state.info.path}`;
122122
keys.push(await computeSurrogateKey(`${state.site}--${state.org}_config.json`));
123-
keys.push(pathKey.replace(/\//g, '_')); // TODO: remove
124123
keys.push(await computeSurrogateKey(pathKey));
125124
return keys;
126125
}

test/json-pipe.test.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,29 @@ describe('JSON Pipe Test', () => {
253253
});
254254
});
255255

256-
it('ignores newer last modified from metadata.json even if newer', async () => {
256+
it('respects redirects (code)', async () => {
257+
const state = createDefaultState();
258+
state.s3Loader
259+
.reply(
260+
'helix-code-bus',
261+
'owner/repo/ref/en/index.json',
262+
new PipelineResponse(TEST_SINGLE_SHEET, {
263+
headers: {
264+
'content-type': 'application/json',
265+
'x-amz-meta-redirect-location': '/de/index.json',
266+
},
267+
}),
268+
)
269+
.reply('helix-content-bus', 'foobar/preview/en/index.json', null);
270+
const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=10&offset=5'));
271+
assert.strictEqual(resp.status, 301);
272+
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
273+
'location': '/de/index.json',
274+
'x-surrogate-key': 'ref--repo--owner_code SIMSxecp2CJXqGYs',
275+
});
276+
});
277+
278+
it('ignores last modified from metadata.json even if newer', async () => {
257279
const state = createDefaultState();
258280
state.s3Loader.reply(
259281
'helix-content-bus',
@@ -267,18 +289,6 @@ describe('JSON Pipe Test', () => {
267289
},
268290
}),
269291
);
270-
state.s3Loader.reply(
271-
'helix-code-bus',
272-
'foobar/preview/metadata.json',
273-
new PipelineResponse(JSON.stringify({
274-
data: [
275-
],
276-
}), {
277-
headers: {
278-
'last-modified': 'Wed, 15 Oct 2009 17:50:00 GMT',
279-
},
280-
}),
281-
);
282292
const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=10&offset=5'));
283293
assert.strictEqual(resp.status, 200);
284294
assert.deepStrictEqual(await resp.json(), {

test/robots-pipe.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Robots Pipe Test', () => {
6969
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
7070
'content-type': 'text/plain; charset=utf-8',
7171
vary: 'x-forwarded-host',
72-
'x-surrogate-key': 'U_NW4adJU7Qazf-I ref--repo--owner_robots.txt ZcR1sjWODctSccZh',
72+
'x-surrogate-key': 'U_NW4adJU7Qazf-I ZcR1sjWODctSccZh',
7373
});
7474
assert.strictEqual(resp.body, 'this is my robots.txt');
7575
});
@@ -149,7 +149,7 @@ describe('Robots Pipe Test', () => {
149149
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
150150
'content-type': 'text/plain; charset=utf-8',
151151
vary: 'x-forwarded-host',
152-
'x-surrogate-key': 'U_NW4adJU7Qazf-I ref--repo--owner_robots.txt ZcR1sjWODctSccZh',
152+
'x-surrogate-key': 'U_NW4adJU7Qazf-I ZcR1sjWODctSccZh',
153153
});
154154
assert.strictEqual(resp.body, `User-Agent: *
155155
Allow: /
@@ -184,7 +184,7 @@ Sitemap: https://www.example.com/sitemap.xml`);
184184
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
185185
'content-type': 'text/plain; charset=utf-8',
186186
vary: 'x-forwarded-host',
187-
'x-surrogate-key': 'U_NW4adJU7Qazf-I ref--repo--owner_robots.txt ZcR1sjWODctSccZh',
187+
'x-surrogate-key': 'U_NW4adJU7Qazf-I ZcR1sjWODctSccZh',
188188
});
189189
assert.strictEqual(resp.body, `User-Agent: *
190190
Allow: /

0 commit comments

Comments
 (0)