Skip to content
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

fix: respect x-source-last-modified for json #51

Merged
merged 1 commit into from
May 8, 2022
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
4 changes: 2 additions & 2 deletions src/json-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import fetchMetadata from './steps/fetch-metadata.js';
import setCustomResponseHeaders from './steps/set-custom-response-headers.js';
import { PipelineResponse } from './PipelineResponse.js';
import jsonFilter from './utils/json-filter.js';
import { updateLastModified } from './utils/last-modified.js';
import { extractLastModified, updateLastModified } from './utils/last-modified.js';

/**
* Runs the default pipeline and returns the response.
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function jsonPipe(state, req) {
});

// set last-modified
updateLastModified(state, response, dataResponse.headers.get('last-modified'));
updateLastModified(state, response, extractLastModified(dataResponse.headers));

// set surrogate key
response.headers.set('x-surrogate-key', `${contentBusId}${path}`.replace(/\//g, '_'));
Expand Down
4 changes: 2 additions & 2 deletions src/steps/fetch-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default async function fetchMetadata(state, req, res) {
}
state.metadata = data;

if (state.type === 'html') {
// also update last-modified (only for html pipeline)
if (state.type === 'html' && state.info.selector !== 'plain') {
// also update last-modified (only for extensionless html pipeline)
updateLastModified(state, res, extractLastModified(ret.headers));
}
return;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/last-modified.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export function updateLastModified(state, res, httpDate) {
/**
* Returns the last modified date from response headers, giving 'x-amz-meta-x-source-last-modified'
* preference.
* @param {object} headers
* @param {Map<string, string>} headers
* @return {string} the last modified date
*/
export function extractLastModified(headers) {
return headers.get('x-amz-meta-x-source-last-modified') ?? headers.get('last-modified');
Expand Down
32 changes: 32 additions & 0 deletions test/json-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,38 @@ describe('JSON Pipe Test', () => {
});
});

it('prefers x-source-last-modified', async () => {
const state = createDefaultState();
state.s3Loader.reply(
'helix-content-bus',
'foobar/preview/en/index.json',
new PipelineResponse(TEST_SINGLE_SHEET, {
headers: {
'content-type': 'application/json',
'x-amz-meta-x-source-location': 'foo-bar',
'last-modified': 'Wed, 12 Oct 2009 17:50:00 GMT',
'x-amz-meta-x-source-last-modified': 'Wed, 12 Oct 2009 15:50:00 GMT',
},
}),
);

const resp = await jsonPipe(state, new PipelineRequest('https://json-filter.com/?limit=10&offset=5'));
assert.strictEqual(resp.status, 200);
assert.deepStrictEqual(await resp.json(), {
':type': 'sheet',
offset: 5,
limit: 10,
total: TEST_DATA.length,
data: TEST_DATA.slice(5, 15),
});
const headers = Object.fromEntries(resp.headers.entries());
assert.deepStrictEqual(headers, {
'last-modified': 'Wed, 12 Oct 2009 15:50:00 GMT',
'x-surrogate-key': 'foobar_en_index.json',
'content-type': 'application/json',
});
});

it('falls back to code bus if content is not found', async () => {
const state = new PipelineState({
path: '/en/index.json',
Expand Down
15 changes: 15 additions & 0 deletions test/rendering.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ describe('Rendering', () => {
});
});

it('ignores last modified from metadata.json for plain', async () => {
loader
.headers('helix-config.json', 'x-amz-meta-x-source-last-modified', 'Wed, 12 Oct 2009 11:50:00 GMT')
.headers('one-section.md', 'x-amz-meta-x-source-last-modified', 'Wed, 12 Oct 2022 12:50:00 GMT')
.headers('metadata.json', 'x-amz-meta-x-source-last-modified', 'Wed, 12 Oct 2022 15:33:01 GMT');
const { status, body, headers } = await render(new URL('https://helix-pipeline.com/blog/one-section'), '.plain');
assert.strictEqual(status, 200);
assert.match(body, /<div class="test"><h1 id="hello">Hello<\/h1><p>This is the first section.<\/p><\/div>/);
assert.deepStrictEqual(Object.fromEntries(headers.entries()), {
'content-type': 'text/html; charset=utf-8',
'x-surrogate-key': '0j8f6rmY3lU5kgOE',
'last-modified': 'Wed, 12 Oct 2022 12:50:00 GMT',
});
});

it('uses response headers from metadata.json', async () => {
loader.rewrite('metadata.json', 'metadata-headers.json');
const { headers } = await testRender('meta-response-headers', 'head');
Expand Down