Skip to content

Commit

Permalink
Merge pull request #535 from adobe/serve-html
Browse files Browse the repository at this point in the history
fix: cherry-pick missing 5.x fixes
  • Loading branch information
tripodsan authored Feb 9, 2024
2 parents 52739ee + 049caf0 commit 9fd7b6d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 36 deletions.
17 changes: 7 additions & 10 deletions src/steps/set-x-surrogate-key-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ export async function getPathKey(state) {
*/
export default async function setXSurrogateKeyHeader(state, req, res) {
const {
content, contentBusId, owner, repo, ref,
contentBusId, owner, repo, ref,
} = state;

const keys = [];
if (content.sourceLocation) {
keys.push(await computeSurrogateKey(content.sourceLocation));
}

const hash = await getPathKey(state);
keys.push(hash);
keys.push(`${contentBusId}_metadata`);
keys.push(`${ref}--${repo}--${owner}_head`);
const keys = [
hash,
`${contentBusId}_metadata`,
`${ref}--${repo}--${owner}_head`,
];

// for folder-mapped resources, we also need to include the surrogate key of the mapped metadata
if (state.mapped) {
keys.push(`${hash}_metadata`);
}
Expand Down
19 changes: 8 additions & 11 deletions src/utils/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ export function getPathInfo(path) {
}
let resExt = info.extension;
if (info.selector) {
if (resExt === '.html') {
// force .plain.html as markdown resources
if (info.selector === 'plain' && resExt === '.html') {
// force .plain.html as markdown resources and remove selector from path
resExt = '.md';
fileName = `${baseName}${resExt}`;
} else {
fileName = `${baseName}.${info.selector}${resExt}`;
}
segs.push(`${baseName}.${info.selector}${info.extension}`);
} else {
segs.push(`${baseName}${resExt}`);
fileName = `${baseName}${resExt}`;
}
fileName = `${baseName}${resExt}`;
}

info.path = `/${segs.join('/')}`;
Expand All @@ -94,14 +97,8 @@ export function validatePathInfo(info) {
return false;
}

// only support empty selector or plain with html
if (info.selector) {
if (info.selector !== 'plain') {
return false;
}
return info.extension === '.html';
}
return true;
// only support selector for html
return info.selector === '' || info.extension === '.html';
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/code/my-block.selector.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>static</body></html>
37 changes: 32 additions & 5 deletions test/html-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const DEFAULT_STATE = (config = DEFAULT_CONFIG, opts = {}) => (new PipelineState
describe('HTML Pipe Test', () => {
it('responds with 404 for invalid path', async () => {
const resp = await htmlPipe(
DEFAULT_STATE(DEFAULT_CONFIG, { path: '/foo.hidden.html' }),
DEFAULT_STATE(DEFAULT_CONFIG, { path: '/../etc/passwd' }),
new PipelineRequest(new URL('https://www.hlx.live/')),
);
assert.strictEqual(resp.status, 404);
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('HTML Pipe Test', () => {
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'content-type': 'text/markdown; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': '-RNwtJ99NJmYY2L- FzT3jXtDSYMYOTq1 foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'FzT3jXtDSYMYOTq1 foo-id_metadata super-test--helix-pages--adobe_head',
});
});

Expand All @@ -144,7 +144,7 @@ describe('HTML Pipe Test', () => {
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': '-RNwtJ99NJmYY2L- FzT3jXtDSYMYOTq1 foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'FzT3jXtDSYMYOTq1 foo-id_metadata super-test--helix-pages--adobe_head',
});
});

Expand All @@ -169,7 +169,7 @@ describe('HTML Pipe Test', () => {
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'content-type': 'text/markdown; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'GWAg-4KOc8drP8BG iQzO-EvK0WKNO_o0 foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'iQzO-EvK0WKNO_o0 foo-id_metadata super-test--helix-pages--adobe_head',
});
});

Expand All @@ -194,7 +194,34 @@ describe('HTML Pipe Test', () => {
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'GWAg-4KOc8drP8BG iQzO-EvK0WKNO_o0 foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'iQzO-EvK0WKNO_o0 foo-id_metadata super-test--helix-pages--adobe_head',
});
});

it('renders static html with selector my-block.selector.html', async () => {
const s3Loader = new FileS3Loader();
const state = DEFAULT_STATE(DEFAULT_CONFIG, {
log: console,
s3Loader,
owner: 'adobe',
repo: 'helix-pages',
ref: 'super-test',
partition: 'live',
path: '/my-block.selector.html',
timer: {
update: () => { },
},
});
const resp = await htmlPipe(
state,
new PipelineRequest(new URL('https://www.hlx.live/')),
);
assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.body, '<html><body>static</body></html>\n');
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'kvcvppnfHtt5omSX foo-id_metadata super-test--helix-pages--adobe_head',
});
});
});
12 changes: 6 additions & 6 deletions test/rendering.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,17 @@ describe('Rendering', () => {
describe('Miscellaneous', () => {
it('sets the surrogate-keys correctly', async () => {
const resp = await testRender('page-block-empty-cols');
assert.strictEqual(resp.headers.get('x-surrogate-key'), '_5g3dEf12QuYUAwe rDFj9gBeGHx_FI2T foo-id_metadata super-test--helix-pages--adobe_head');
assert.strictEqual(resp.headers.get('x-surrogate-key'), 'rDFj9gBeGHx_FI2T foo-id_metadata super-test--helix-pages--adobe_head');
});

it('sets the surrogate-keys correctly for plain', async () => {
const resp = await testRenderPlain('one-section');
assert.strictEqual(resp.headers.get('x-surrogate-key'), '0j8f6rmY3lU5kgOE oHjg_WDu20CBS4rD foo-id_metadata super-test--helix-pages--adobe_head');
assert.strictEqual(resp.headers.get('x-surrogate-key'), 'oHjg_WDu20CBS4rD foo-id_metadata super-test--helix-pages--adobe_head');
});

it('sets the surrogate-keys correctly for index.plain.html', async () => {
const resp = await testRenderPlain('one-section/index', 'one-section/index');
assert.strictEqual(resp.headers.get('x-surrogate-key'), '-RNwtJ99NJmYY2L- Vp-I6NB8PSor1sI6 foo-id_metadata super-test--helix-pages--adobe_head');
assert.strictEqual(resp.headers.get('x-surrogate-key'), 'Vp-I6NB8PSor1sI6 foo-id_metadata super-test--helix-pages--adobe_head');
});

it('renders the fedpub header correctly', async () => {
Expand Down Expand Up @@ -593,7 +593,7 @@ describe('Rendering', () => {
'access-control-allow-origin': '*',
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'zxdhoulVcSRWb0Ky SUhNxkR2spoxY489 foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'SUhNxkR2spoxY489 foo-id_metadata super-test--helix-pages--adobe_head',
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
});
});
Expand Down Expand Up @@ -626,7 +626,7 @@ describe('Rendering', () => {
assert.match(body, /<link rel="canonical" href="https:\/\/helix-pipeline\.com\/blog\/">/);
assert.deepStrictEqual(Object.fromEntries(headers.entries()), {
'content-type': 'text/html; charset=utf-8',
'x-surrogate-key': '-RNwtJ99NJmYY2L- o_fNQBWBLWTIfYqV foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'o_fNQBWBLWTIfYqV foo-id_metadata super-test--helix-pages--adobe_head',
'last-modified': 'Wed, 12 Jan 2022 11:33:01 GMT',
});
});
Expand All @@ -639,7 +639,7 @@ describe('Rendering', () => {
'access-control-allow-origin': '*',
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'S7iL12Odk1BYBsOI VmeAc3K7QsCRzj5Z foo-id_metadata super-test--helix-pages--adobe_head',
'x-surrogate-key': 'VmeAc3K7QsCRzj5Z foo-id_metadata super-test--helix-pages--adobe_head',
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/sitemap-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('Sitemap Pipe Test', () => {
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'content-type': 'application/xml; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'rCCgYLwPe4ckYgJ7 RXei-6EcTEMTEIqi foobar_metadata ref--repo--owner_head',
'x-surrogate-key': 'RXei-6EcTEMTEIqi foobar_metadata ref--repo--owner_head',
});
assert.strictEqual(resp.body, `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
Expand Down
6 changes: 3 additions & 3 deletions test/utils/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('Path Utils Test - getPathInfo', () => {
selector: 'plain',
extension: '.json',
path: '/en/header.plain.json',
resourcePath: '/en/header.json',
resourcePath: '/en/header.plain.json',
originalExtension: '.json',
originalFilename: 'header.plain.json',
originalPath: '/en/header.plain.json',
Expand Down Expand Up @@ -206,8 +206,8 @@ describe('Path Utils Test - validatePathInfo', () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.plain.html')), true);
});

it('rejects path with html.plain.html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.html.plain.html')), false);
it('allows path with html.plain.html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.html.plain.html')), true);
});

it('rejects path with plain.json', async () => {
Expand Down

0 comments on commit 9fd7b6d

Please sign in to comment.