@adobe/helix-html-pipeline 3.11.7 → 3.11.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [3.11.9](https://github.com/adobe/helix-html-pipeline/compare/v3.11.8...v3.11.9) (2023-05-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * properly set surrogate keys for 404 with .plain.html ([#321](https://github.com/adobe/helix-html-pipeline/issues/321)) ([c7a956a](https://github.com/adobe/helix-html-pipeline/commit/c7a956a1684e758b5c0cf343c68a8dba1f8bc226)), closes [#320](https://github.com/adobe/helix-html-pipeline/issues/320)
7
+
8
+ ## [3.11.8](https://github.com/adobe/helix-html-pipeline/compare/v3.11.7...v3.11.8) (2023-05-22)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * don't load 404.html too early ([#318](https://github.com/adobe/helix-html-pipeline/issues/318)) ([6fa9f12](https://github.com/adobe/helix-html-pipeline/commit/6fa9f120eed6daca3f006062cb2f81f96708a5db))
14
+
1
15
  ## [3.11.7](https://github.com/adobe/helix-html-pipeline/compare/v3.11.6...v3.11.7) (2023-05-22)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-html-pipeline",
3
- "version": "3.11.7",
3
+ "version": "3.11.9",
4
4
  "description": "Helix HTML Pipeline",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/html-pipe.js CHANGED
@@ -17,6 +17,7 @@ import createPictures from './steps/create-pictures.js';
17
17
  import extractMetaData from './steps/extract-metadata.js';
18
18
  import fetchConfig from './steps/fetch-config.js';
19
19
  import fetchContent from './steps/fetch-content.js';
20
+ import fetch404 from './steps/fetch-404.js';
20
21
  import fetchConfigAll from './steps/fetch-config-all.js';
21
22
  import fixSections from './steps/fix-sections.js';
22
23
  import folderMapping from './steps/folder-mapping.js';
@@ -39,6 +40,20 @@ import { validatePathInfo } from './utils/path.js';
39
40
  import { initAuthRoute } from './utils/auth.js';
40
41
  import fetchMappedMetadata from './steps/fetch-mapped-metadata.js';
41
42
 
43
+ /**
44
+ * Fetches the content and if not found, fetches the 404.html
45
+ * @param state
46
+ * @param req
47
+ * @param res
48
+ * @returns {Promise<void>}
49
+ */
50
+ async function fetchContentWith404Fallback(state, req, res) {
51
+ await fetchContent(state, req, res);
52
+ if (res.status === 404) {
53
+ await fetch404(state, req, res);
54
+ }
55
+ }
56
+
42
57
  /**
43
58
  * Runs the default pipeline and returns the response.
44
59
  * @param {PipelineState} state
@@ -88,7 +103,9 @@ export async function htmlPipe(state, req) {
88
103
  if (res.status === 404) {
89
104
  await folderMapping(state);
90
105
  if (state.info.unmappedPath) {
91
- contentPromise = fetchContent(state, req, res);
106
+ contentPromise = fetchContentWith404Fallback(state, req, res);
107
+ } else {
108
+ contentPromise = fetch404(state, req, res);
92
109
  }
93
110
  }
94
111
 
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright 2023 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { extractLastModified } from '../utils/last-modified.js';
13
+ import { getPathKey } from './set-x-surrogate-key-header.js';
14
+
15
+ /**
16
+ * Loads the 404.html from code-bus and stores it in `res.body`
17
+ * @type PipelineStep
18
+ * @param {PipelineState} state
19
+ * @param {PipelineRequest} req
20
+ * @param {PipelineResponse} res
21
+ * @returns {Promise<void>}
22
+ */
23
+ export default async function fetch404(state, req, res) {
24
+ const { owner, repo, ref } = state;
25
+ const ret = await state.s3Loader.getObject('helix-code-bus', `${owner}/${repo}/${ref}/404.html`);
26
+ if (ret.status === 200) {
27
+ // override last-modified if source-last-modified is set
28
+ const lastModified = extractLastModified(ret.headers);
29
+ if (lastModified) {
30
+ ret.headers.set('last-modified', lastModified);
31
+ }
32
+
33
+ // keep 404 response status
34
+ res.body = ret.body;
35
+ res.headers.set('last-modified', ret.headers.get('last-modified'));
36
+ res.headers.set('content-type', 'text/html; charset=utf-8');
37
+ }
38
+
39
+ // set 404 keys in any case
40
+ const pathKey = await getPathKey(state);
41
+ res.headers.set('x-surrogate-key', `${pathKey} ${ref}--${repo}--${owner}_404`);
42
+ }
@@ -69,23 +69,4 @@ export default async function fetchContent(state, req, res) {
69
69
  res.status = ret.status === 404 ? 404 : 502;
70
70
  res.error = `failed to load ${info.resourcePath} from ${state.content.sourceBus}-bus: ${ret.status}`;
71
71
  }
72
-
73
- if (res.status === 404) {
74
- // try to load 404.html from code-bus
75
- const ret404 = await state.s3Loader.getObject('helix-code-bus', `${owner}/${repo}/${ref}/404.html`);
76
- if (ret404.status === 200) {
77
- // override last-modified if source-last-modified is set
78
- const lastModified = extractLastModified(ret404.headers);
79
- if (lastModified) {
80
- ret404.headers.set('last-modified', lastModified);
81
- }
82
-
83
- // keep 404 response status
84
- res.body = ret404.body;
85
- res.headers.set('last-modified', ret404.headers.get('last-modified'));
86
- res.headers.set('content-type', 'text/html; charset=utf-8');
87
- const pathKey = await computeSurrogateKey(`${contentBusId}${info.path}`);
88
- res.headers.set('x-surrogate-key', `${pathKey} ${ref}--${repo}--${owner}_404`);
89
- }
90
- }
91
72
  }
@@ -11,6 +11,24 @@
11
11
  */
12
12
  import { computeSurrogateKey } from '@adobe/helix-shared-utils';
13
13
 
14
+ /**
15
+ * Returns the surrogate key based on the contentBusId and the resource path
16
+ * @param state
17
+ * @returns {Promise<string>}
18
+ */
19
+ export async function getPathKey(state) {
20
+ const { contentBusId, info } = state;
21
+ let { path } = info;
22
+ // surrogate key for path
23
+ // strip [index].plain.html
24
+ if (path.endsWith('index.plain.html')) {
25
+ path = path.substring(0, path.length - 'index.plain.html'.length);
26
+ } else if (path.endsWith('.plain.html')) {
27
+ path = path.substring(0, path.length - '.plain.html'.length);
28
+ }
29
+ return computeSurrogateKey(`${contentBusId}${path}`);
30
+ }
31
+
14
32
  /**
15
33
  * @type PipelineStep
16
34
  * @param {PipelineState} state
@@ -20,26 +38,16 @@ import { computeSurrogateKey } from '@adobe/helix-shared-utils';
20
38
  */
21
39
  export default async function setXSurrogateKeyHeader(state, req, res) {
22
40
  const {
23
- content, contentBusId, owner, repo, ref, info,
41
+ content, contentBusId, owner, repo, ref,
24
42
  } = state;
25
43
 
26
- let { path } = info;
27
-
28
44
  const keys = [];
29
45
  if (content.sourceLocation) {
30
46
  keys.push(await computeSurrogateKey(content.sourceLocation));
31
47
  }
32
48
 
33
- // surrogate key for path
34
- // strip [index].plain.html
35
- if (path.endsWith('index.plain.html')) {
36
- path = path.substring(0, path.length - 'index.plain.html'.length);
37
- } else if (path.endsWith('.plain.html')) {
38
- path = path.substring(0, path.length - '.plain.html'.length);
39
- }
40
- const hash = await computeSurrogateKey(`${contentBusId}${path}`);
49
+ const hash = await getPathKey(state);
41
50
  keys.push(hash);
42
-
43
51
  keys.push(`${contentBusId}_metadata`);
44
52
  keys.push(`${ref}--${repo}--${owner}_head`);
45
53