Skip to content

Commit 1d0320b

Browse files
committed
feat: apply last-modified from metadata
1 parent 07d0ec0 commit 1d0320b

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/html-pipe.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { PipelineStatusError } from './PipelineStatusError.js';
3535
import { PipelineResponse } from './PipelineResponse.js';
3636
import { validatePathInfo } from './utils/path.js';
3737
import fetchMappedMetadata from './steps/fetch-mapped-metadata.js';
38-
import { setLastModified } from './utils/last-modified.js';
38+
import { applyMetaLastModified, setLastModified } from './utils/last-modified.js';
3939

4040
/**
4141
* Fetches the content and if not found, fetches the 404.html
@@ -168,6 +168,7 @@ export async function htmlPipe(state, req) {
168168
await render(state, req, res);
169169
state.timer?.update('serialize');
170170
await tohtml(state, req, res);
171+
await applyMetaLastModified(state, res);
171172
}
172173

173174
setLastModified(state, res);

src/utils/last-modified.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Records the last modified for the given source.
1515
*
1616
* @param {PipelineState} state
17-
* @param {PipelineResponse} res the pipeline context
17+
* @param {PipelineResponse} res the pipeline response
1818
* @param {string} source the source providing a last-modified date
1919
* @param {string} httpDate http-date string
2020
*/
@@ -39,7 +39,7 @@ export function recordLastModified(state, res, source, httpDate) {
3939
* and sets it on the `last-modified` header.
4040
*
4141
* @param {PipelineState} state
42-
* @param {PipelineResponse} res the pipeline context
42+
* @param {PipelineResponse} res the pipeline response
4343
*/
4444
export function setLastModified(state, res) {
4545
let latestTime = 0;
@@ -68,3 +68,13 @@ export function extractLastModified(headers) {
6868
}
6969
return headers.get('last-modified');
7070
}
71+
72+
/**
73+
* Sets the metadata last modified entry to the one define in the page specific metadata if
74+
* it exists. this allows to control the last-modified per metadata record.
75+
* @param {PipelineState} state
76+
* @param {PipelineResponse} res the pipeline response
77+
*/
78+
export function applyMetaLastModified(state, res) {
79+
recordLastModified(state, res, 'metadata', state.content.meta.page['last-modified']);
80+
}

test/fixtures/content/generic-product/metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
"Keywords": "Exactomento Mapped Folder",
6161
"og:publisher": "Adobe",
6262
"Short Title": "E"
63+
},
64+
{
65+
"URL": "/products/product2",
66+
"last-modified": "2024-12-25T03:33:33"
6367
}
6468
]
6569
}

test/rendering.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,20 @@ describe('Rendering', () => {
738738
'x-surrogate-key': 'AkcHu8fRFT7HarTR foo-id_metadata super-test--helix-pages--adobe_head foo-id AkcHu8fRFT7HarTR_metadata z8NGXvKB0X5Fzcnd',
739739
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
740740
});
741+
742+
// product2 has a custom last-modified defined in the metadata
743+
resp = await render(new URL('https://helix-pipeline.com/products/product2'));
744+
assert.strictEqual(resp.status, 200);
745+
assert.match(resp.body, /<meta name="short-title" content="E">/);
746+
assert.match(resp.body, /<meta property="og:publisher" content="Adobe">/);
747+
assert.match(resp.body, /<meta name="keywords" content="Exactomento Mapped Folder">/);
748+
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
749+
'access-control-allow-origin': '*',
750+
'content-type': 'text/html; charset=utf-8',
751+
'last-modified': 'Wed, 25 Dec 2024 02:33:33 GMT',
752+
'x-surrogate-key': 'AkcHu8fRFT7HarTR foo-id_metadata super-test--helix-pages--adobe_head foo-id AkcHu8fRFT7HarTR_metadata G03gAJ9i4zOGySKf',
753+
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
754+
});
741755
});
742756

743757
it('handles error while loading mapped metadata', async () => {

0 commit comments

Comments
 (0)