@lwrjs/core 0.15.0-alpha.13 → 0.15.0-alpha.15

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.
@@ -45,7 +45,7 @@ function requestProcessorMiddleware(app, context) {
45
45
  }
46
46
  }
47
47
  requestClass = req.headers[MRT_REQUEST_CLASS_KEY];
48
- requestDepth = (0, import_shared_utils.parseRequestDepthHeader)(req.headers);
48
+ requestDepth = (0, import_shared_utils.parseRequestDepth)(req.headers, req.query);
49
49
  const forwarded = req.headers["forwarded"];
50
50
  const host = req.headers["host"];
51
51
  const forwardedProto = req.headers["x-forwarded-proto"];
@@ -57,7 +57,7 @@ function requestProcessorMiddleware(app, context) {
57
57
  });
58
58
  }
59
59
  }
60
- if (requestDepth && requestDepth > 0) {
60
+ if (requestDepth && requestDepth > 1) {
61
61
  import_diagnostics.logger.error({
62
62
  label: "request-processor-middleware",
63
63
  message: `Lambda SSR request cycle detected: ${req.originalUrl}`
@@ -115,11 +115,19 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
115
115
  if (traceId?.length) {
116
116
  res.setHeader("x-trace-id", traceId);
117
117
  }
118
- let status;
118
+ let status = resolvedRoute.status || viewResponse.status || 200;
119
+ const redirect = viewResponse.metadata?.viewDefinition?.redirect;
119
120
  if (viewResponse.status === 301 || viewResponse.status === 302) {
120
121
  status = viewResponse.status;
121
- } else {
122
- status = resolvedRoute.status || viewResponse.status || 200;
122
+ } else if (redirect) {
123
+ if ((0, import_shared_utils.isURL)(redirect.location) || redirect.location.startsWith("/")) {
124
+ status = redirect.status || 302;
125
+ res.set({
126
+ Location: addRedirectQueryParam(redirect.location, (0, import_shared_utils.parseRequestDepth)(req.headers, req.query))
127
+ });
128
+ } else {
129
+ import_diagnostics.logger.warn(`[view-middleware] Redirect failed because the URL is invalid: "${redirect.location}"`);
130
+ }
123
131
  }
124
132
  res.status(status);
125
133
  res.send(viewResponse.body);
@@ -225,3 +233,9 @@ function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
225
233
  return res.sendStatus(301);
226
234
  });
227
235
  }
236
+ function addRedirectQueryParam(redirectUrl, depth) {
237
+ const fakeOrigin = "http://parse.com";
238
+ const url = (0, import_shared_utils.isURL)(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
239
+ url.searchParams.set(import_shared_utils.REQUEST_DEPTH_KEY, String(depth + 1));
240
+ return url.toString().replace(fakeOrigin, "");
241
+ }
@@ -7,7 +7,7 @@
7
7
  *
8
8
  */
9
9
  import { logger } from '@lwrjs/diagnostics';
10
- import { REQUEST_DEPTH_HEADER, isLambdaEnv, parseRequestDepthHeader } from '@lwrjs/shared-utils';
10
+ import { REQUEST_DEPTH_HEADER, isLambdaEnv, parseRequestDepth } from '@lwrjs/shared-utils';
11
11
  const MRT_REQUEST_CLASS = 'X-Mobify-Request-Class';
12
12
  const MRT_REQUEST_CLASS_KEY = MRT_REQUEST_CLASS.toLowerCase();
13
13
  export function requestProcessorMiddleware(app, context) {
@@ -27,7 +27,7 @@ export function requestProcessorMiddleware(app, context) {
27
27
  }
28
28
  }
29
29
  requestClass = req.headers[MRT_REQUEST_CLASS_KEY];
30
- requestDepth = parseRequestDepthHeader(req.headers);
30
+ requestDepth = parseRequestDepth(req.headers, req.query);
31
31
  const forwarded = req.headers['forwarded'];
32
32
  const host = req.headers['host'];
33
33
  const forwardedProto = req.headers['x-forwarded-proto'];
@@ -40,8 +40,7 @@ export function requestProcessorMiddleware(app, context) {
40
40
  });
41
41
  }
42
42
  }
43
- // TODO Clean up once we have a long term solution
44
- if (requestDepth && requestDepth > 0) {
43
+ if (requestDepth && requestDepth > 1) {
45
44
  logger.error({
46
45
  label: 'request-processor-middleware',
47
46
  message: `Lambda SSR request cycle detected: ${req.originalUrl}`,
@@ -1,6 +1,6 @@
1
1
  import { descriptions, logger } from '@lwrjs/diagnostics';
2
2
  import { getClientRoutes } from '@lwrjs/router';
3
- import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, shortestTtl, } from '@lwrjs/shared-utils';
3
+ import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, isURL, parseRequestDepth, REQUEST_DEPTH_KEY, shortestTtl, } from '@lwrjs/shared-utils';
4
4
  import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
5
5
  import { handleErrors } from './utils/error-handling.js';
6
6
  import { LwrViewHandler } from '@lwrjs/view-registry';
@@ -94,12 +94,22 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
94
94
  if (traceId?.length) {
95
95
  res.setHeader('x-trace-id', traceId);
96
96
  }
97
- let status;
97
+ let status = resolvedRoute.status || viewResponse.status || 200;
98
+ const redirect = viewResponse.metadata?.viewDefinition?.redirect;
98
99
  if (viewResponse.status === 301 || viewResponse.status === 302) {
100
+ // route handle redirect status takes precedence
99
101
  status = viewResponse.status;
100
102
  }
101
- else {
102
- status = resolvedRoute.status || viewResponse.status || 200;
103
+ else if (redirect) {
104
+ if (isURL(redirect.location) || redirect.location.startsWith('/')) {
105
+ status = redirect.status || 302;
106
+ res.set({
107
+ Location: addRedirectQueryParam(redirect.location, parseRequestDepth(req.headers, req.query)),
108
+ });
109
+ }
110
+ else {
111
+ logger.warn(`[view-middleware] Redirect failed because the URL is invalid: "${redirect.location}"`);
112
+ }
103
113
  }
104
114
  res.status(status);
105
115
  res.send(viewResponse.body);
@@ -229,4 +239,12 @@ function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
229
239
  return res.sendStatus(301);
230
240
  });
231
241
  }
242
+ function addRedirectQueryParam(redirectUrl, depth) {
243
+ // add a request depth query param to the URL
244
+ // the depth header cannot be used since headers cannot be added to a redirect
245
+ const fakeOrigin = 'http://parse.com';
246
+ const url = isURL(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
247
+ url.searchParams.set(REQUEST_DEPTH_KEY, String(depth + 1));
248
+ return url.toString().replace(fakeOrigin, '');
249
+ }
232
250
  //# sourceMappingURL=view-middleware.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.15.0-alpha.13",
7
+ "version": "0.15.0-alpha.15",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -43,34 +43,34 @@
43
43
  "build": "tsc -b"
44
44
  },
45
45
  "dependencies": {
46
- "@lwrjs/app-service": "0.15.0-alpha.13",
47
- "@lwrjs/asset-registry": "0.15.0-alpha.13",
48
- "@lwrjs/asset-transformer": "0.15.0-alpha.13",
49
- "@lwrjs/base-view-provider": "0.15.0-alpha.13",
50
- "@lwrjs/base-view-transformer": "0.15.0-alpha.13",
51
- "@lwrjs/client-modules": "0.15.0-alpha.13",
52
- "@lwrjs/config": "0.15.0-alpha.13",
53
- "@lwrjs/diagnostics": "0.15.0-alpha.13",
54
- "@lwrjs/esbuild": "0.15.0-alpha.13",
55
- "@lwrjs/fs-asset-provider": "0.15.0-alpha.13",
56
- "@lwrjs/fs-watch": "0.15.0-alpha.13",
57
- "@lwrjs/html-view-provider": "0.15.0-alpha.13",
58
- "@lwrjs/instrumentation": "0.15.0-alpha.13",
59
- "@lwrjs/loader": "0.15.0-alpha.13",
60
- "@lwrjs/lwc-module-provider": "0.15.0-alpha.13",
61
- "@lwrjs/lwc-ssr": "0.15.0-alpha.13",
62
- "@lwrjs/markdown-view-provider": "0.15.0-alpha.13",
63
- "@lwrjs/module-bundler": "0.15.0-alpha.13",
64
- "@lwrjs/module-registry": "0.15.0-alpha.13",
65
- "@lwrjs/npm-module-provider": "0.15.0-alpha.13",
66
- "@lwrjs/nunjucks-view-provider": "0.15.0-alpha.13",
67
- "@lwrjs/o11y": "0.15.0-alpha.13",
68
- "@lwrjs/resource-registry": "0.15.0-alpha.13",
69
- "@lwrjs/router": "0.15.0-alpha.13",
70
- "@lwrjs/server": "0.15.0-alpha.13",
71
- "@lwrjs/shared-utils": "0.15.0-alpha.13",
72
- "@lwrjs/static": "0.15.0-alpha.13",
73
- "@lwrjs/view-registry": "0.15.0-alpha.13",
46
+ "@lwrjs/app-service": "0.15.0-alpha.15",
47
+ "@lwrjs/asset-registry": "0.15.0-alpha.15",
48
+ "@lwrjs/asset-transformer": "0.15.0-alpha.15",
49
+ "@lwrjs/base-view-provider": "0.15.0-alpha.15",
50
+ "@lwrjs/base-view-transformer": "0.15.0-alpha.15",
51
+ "@lwrjs/client-modules": "0.15.0-alpha.15",
52
+ "@lwrjs/config": "0.15.0-alpha.15",
53
+ "@lwrjs/diagnostics": "0.15.0-alpha.15",
54
+ "@lwrjs/esbuild": "0.15.0-alpha.15",
55
+ "@lwrjs/fs-asset-provider": "0.15.0-alpha.15",
56
+ "@lwrjs/fs-watch": "0.15.0-alpha.15",
57
+ "@lwrjs/html-view-provider": "0.15.0-alpha.15",
58
+ "@lwrjs/instrumentation": "0.15.0-alpha.15",
59
+ "@lwrjs/loader": "0.15.0-alpha.15",
60
+ "@lwrjs/lwc-module-provider": "0.15.0-alpha.15",
61
+ "@lwrjs/lwc-ssr": "0.15.0-alpha.15",
62
+ "@lwrjs/markdown-view-provider": "0.15.0-alpha.15",
63
+ "@lwrjs/module-bundler": "0.15.0-alpha.15",
64
+ "@lwrjs/module-registry": "0.15.0-alpha.15",
65
+ "@lwrjs/npm-module-provider": "0.15.0-alpha.15",
66
+ "@lwrjs/nunjucks-view-provider": "0.15.0-alpha.15",
67
+ "@lwrjs/o11y": "0.15.0-alpha.15",
68
+ "@lwrjs/resource-registry": "0.15.0-alpha.15",
69
+ "@lwrjs/router": "0.15.0-alpha.15",
70
+ "@lwrjs/server": "0.15.0-alpha.15",
71
+ "@lwrjs/shared-utils": "0.15.0-alpha.15",
72
+ "@lwrjs/static": "0.15.0-alpha.15",
73
+ "@lwrjs/view-registry": "0.15.0-alpha.15",
74
74
  "chokidar": "^3.6.0",
75
75
  "esbuild": "^0.9.7",
76
76
  "fs-extra": "^11.2.0",
@@ -80,7 +80,7 @@
80
80
  "ws": "^8.18.0"
81
81
  },
82
82
  "devDependencies": {
83
- "@lwrjs/types": "0.15.0-alpha.13",
83
+ "@lwrjs/types": "0.15.0-alpha.15",
84
84
  "@types/ws": "^8.5.12",
85
85
  "memfs": "^4.9.3"
86
86
  },
@@ -93,5 +93,5 @@
93
93
  "volta": {
94
94
  "extends": "../../../package.json"
95
95
  },
96
- "gitHead": "687327a328b33a1abc3ac45e1406d72ebe7d37b4"
96
+ "gitHead": "6399f18c87c36ded4daf9d90360a7e044a769394"
97
97
  }