@adobe/helix-html-pipeline 3.11.6 → 3.11.8

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.8](https://github.com/adobe/helix-html-pipeline/compare/v3.11.7...v3.11.8) (2023-05-22)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * 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))
7
+
8
+ ## [3.11.7](https://github.com/adobe/helix-html-pipeline/compare/v3.11.6...v3.11.7) (2023-05-22)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * folder mapping should only be applied if direct addressed resource is missing ([#307](https://github.com/adobe/helix-html-pipeline/issues/307)) ([cf9cfc0](https://github.com/adobe/helix-html-pipeline/commit/cf9cfc0f00aed7a8fd7a502d0b2c77e9aa6e710d))
14
+
1
15
  ## [3.11.6](https://github.com/adobe/helix-html-pipeline/compare/v3.11.5...v3.11.6) (2023-05-13)
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.6",
3
+ "version": "3.11.8",
4
4
  "description": "Helix HTML Pipeline",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -66,8 +66,8 @@
66
66
  "mime": "3.0.0",
67
67
  "rehype-format": "4.0.1",
68
68
  "rehype-parse": "8.0.4",
69
- "remark-parse": "10.0.1",
70
- "strip-markdown": "5.0.0",
69
+ "remark-parse": "10.0.2",
70
+ "strip-markdown": "5.0.1",
71
71
  "unified": "10.1.2",
72
72
  "unist-util-map": "3.1.3",
73
73
  "unist-util-remove": "3.1.1",
@@ -83,11 +83,11 @@
83
83
  "@semantic-release/git": "10.0.1",
84
84
  "@semantic-release/npm": "10.0.3",
85
85
  "c8": "7.13.0",
86
- "eslint": "8.40.0",
86
+ "eslint": "8.41.0",
87
87
  "eslint-import-resolver-exports": "1.0.0-beta.5",
88
88
  "eslint-plugin-header": "3.1.1",
89
89
  "eslint-plugin-import": "2.27.5",
90
- "esmock": "2.2.2",
90
+ "esmock": "2.2.3",
91
91
  "husky": "8.0.3",
92
92
  "js-yaml": "4.1.0",
93
93
  "jsdom": "22.0.0",
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
@@ -82,13 +97,23 @@ export async function htmlPipe(state, req) {
82
97
  }
83
98
 
84
99
  // ...and apply the folder mapping
85
- await folderMapping(state);
100
+ state.timer?.update('content-fetch');
101
+ let contentPromise = await fetchContent(state, req, res);
102
+ // ...but only if the current resource doesn't exist
103
+ if (res.status === 404) {
104
+ await folderMapping(state);
105
+ if (state.info.unmappedPath) {
106
+ contentPromise = fetchContentWith404Fallback(state, req, res);
107
+ } else {
108
+ contentPromise = fetch404(state, req, res);
109
+ }
110
+ }
86
111
 
87
112
  // load metadata and content in parallel
88
- state.timer?.update('content-fetch');
113
+ state.timer?.update('metadata-fetch');
89
114
  await Promise.all([
90
115
  fetchConfigAll(state, req, res),
91
- fetchContent(state, req, res),
116
+ contentPromise,
92
117
  fetchMappedMetadata(state),
93
118
  ]);
94
119
 
package/src/json-pipe.js CHANGED
@@ -54,6 +54,8 @@ async function fetchJsonContent(state, req, res) {
54
54
  ret = await s3Loader.getObject('helix-code-bus', `${owner}/${repo}/${ref}${path}`);
55
55
  }
56
56
  if (ret.status === 200) {
57
+ res.status = 200;
58
+ delete res.error;
57
59
  state.content.data = ret.body;
58
60
 
59
61
  // store extra source location if present
@@ -117,7 +119,6 @@ export async function jsonPipe(state, req) {
117
119
  },
118
120
  });
119
121
  }
120
- await folderMapping(state);
121
122
 
122
123
  /** @type PipelineResponse */
123
124
  const res = new PipelineResponse('', {
@@ -126,10 +127,20 @@ export async function jsonPipe(state, req) {
126
127
  },
127
128
  });
128
129
 
130
+ // apply the folder mapping if the current resource doesn't exist
129
131
  state.timer?.update('json-fetch');
132
+ let contentPromise = await fetchJsonContent(state, req, res);
133
+ if (res.status === 404) {
134
+ await folderMapping(state);
135
+ if (state.info.unmappedPath) {
136
+ contentPromise = fetchJsonContent(state, req, res);
137
+ }
138
+ }
139
+
140
+ state.timer?.update('json-metadata-fetch');
130
141
  await Promise.all([
131
142
  fetchConfigAll(state, req, res),
132
- fetchJsonContent(state, req, res),
143
+ contentPromise,
133
144
  ]);
134
145
 
135
146
  await authenticate(state, req, res);
@@ -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 { computeSurrogateKey } from '@adobe/helix-shared-utils';
13
+ import { extractLastModified } from '../utils/last-modified.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 {
25
+ contentBusId, info, owner, repo, ref,
26
+ } = state;
27
+ const ret = await state.s3Loader.getObject('helix-code-bus', `${owner}/${repo}/${ref}/404.html`);
28
+ if (ret.status === 200) {
29
+ // override last-modified if source-last-modified is set
30
+ const lastModified = extractLastModified(ret.headers);
31
+ if (lastModified) {
32
+ ret.headers.set('last-modified', lastModified);
33
+ }
34
+
35
+ // keep 404 response status
36
+ res.body = ret.body;
37
+ res.headers.set('last-modified', ret.headers.get('last-modified'));
38
+ res.headers.set('content-type', 'text/html; charset=utf-8');
39
+ const pathKey = await computeSurrogateKey(`${contentBusId}${info.path}`);
40
+ res.headers.set('x-surrogate-key', `${pathKey} ${ref}--${repo}--${owner}_404`);
41
+ }
42
+ }
@@ -48,6 +48,8 @@ export default async function fetchContent(state, req, res) {
48
48
  }
49
49
 
50
50
  if (ret.status === 200) {
51
+ res.status = 200;
52
+ delete res.error;
51
53
  state.content.data = ret.body;
52
54
 
53
55
  // store extra source location if present
@@ -67,23 +69,4 @@ export default async function fetchContent(state, req, res) {
67
69
  res.status = ret.status === 404 ? 404 : 502;
68
70
  res.error = `failed to load ${info.resourcePath} from ${state.content.sourceBus}-bus: ${ret.status}`;
69
71
  }
70
-
71
- if (res.status === 404) {
72
- // try to load 404.html from code-bus
73
- const ret404 = await state.s3Loader.getObject('helix-code-bus', `${owner}/${repo}/${ref}/404.html`);
74
- if (ret404.status === 200) {
75
- // override last-modified if source-last-modified is set
76
- const lastModified = extractLastModified(ret404.headers);
77
- if (lastModified) {
78
- ret404.headers.set('last-modified', lastModified);
79
- }
80
-
81
- // keep 404 response status
82
- res.body = ret404.body;
83
- res.headers.set('last-modified', ret404.headers.get('last-modified'));
84
- res.headers.set('content-type', 'text/html; charset=utf-8');
85
- const pathKey = await computeSurrogateKey(`${contentBusId}${info.path}`);
86
- res.headers.set('x-surrogate-key', `${pathKey} ${ref}--${repo}--${owner}_404`);
87
- }
88
- }
89
72
  }