@lwrjs/core 0.8.0-alpha.11 → 0.8.0-alpha.13

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.
@@ -342,7 +342,7 @@ function apiMiddleware(app, context) {
342
342
  const specifier = req.params[0] ? `/${req.params[0]}` : req.originalUrl;
343
343
  const basePathSpecifier = `${basePath}${specifier}`;
344
344
  try {
345
- const assetObj = await context.assetRegistry.getAsset({specifier: basePathSpecifier, signature, type}, runtimeEnvironment);
345
+ const assetObj = await context.assetRegistry.getAsset({specifier: basePathSpecifier, signature, type}, runtimeEnvironment, req.isSiteGeneration());
346
346
  if (immutable) {
347
347
  }
348
348
  if (assetObj.mime) {
@@ -31,6 +31,7 @@ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
31
  var import_view_registry = __toModule(require("@lwrjs/view-registry"));
32
32
  var import_utils = __toModule(require("./utils.cjs"));
33
33
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
34
+ var import_router = __toModule(require("@lwrjs/router"));
34
35
  function uiMiddleware(app, context) {
35
36
  const {viewRegistry, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment} = context;
36
37
  const {environment: environmentConfig, routes, errorRoutes} = context.appConfig;
@@ -145,16 +146,25 @@ function uiMiddleware(app, context) {
145
146
  await sendViewResponse(req, res, route);
146
147
  });
147
148
  } else {
148
- app.get(route.path, async (req, res) => {
149
- await sendViewResponse(req, res, route);
150
- });
151
- app.get([
152
- `/:apiVersion/application/:format/l/:locale/ai/:appId${route.path}`,
153
- `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${route.path}`,
154
- `/:apiVersion/application/:format/ai/:appId${route.path}`,
155
- `/:apiVersion/application/:format/e/:environment/ai/:appId${route.path}`
156
- ], async (req, res) => {
157
- await sendViewResponse(req, res, route);
149
+ const serverPath = route.path;
150
+ const paths = [serverPath];
151
+ const subRoutes = route.subRoutes ? (0, import_router.getClientRoutes)(route.subRoutes) : void 0;
152
+ if (subRoutes) {
153
+ const prefix = serverPath === "/" ? "" : serverPath;
154
+ subRoutes.routes.forEach((r) => paths.push(`${prefix}${r.uri}`));
155
+ }
156
+ paths.forEach((path) => {
157
+ app.get(path, async (req, res) => {
158
+ await sendViewResponse(req, res, route);
159
+ });
160
+ app.get([
161
+ `/:apiVersion/application/:format/l/:locale/ai/:appId${path}`,
162
+ `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${path}`,
163
+ `/:apiVersion/application/:format/ai/:appId${path}`,
164
+ `/:apiVersion/application/:format/e/:environment/ai/:appId${path}`
165
+ ], async (req, res) => {
166
+ await sendViewResponse(req, res, route);
167
+ });
158
168
  });
159
169
  }
160
170
  });
@@ -117,7 +117,7 @@ var SiteGenerator = class {
117
117
  if (resourceType === "route") {
118
118
  await this.handleHtmlResource(url, context, siteConfig, dispatcher);
119
119
  } else if (resourceType === "asset" || resourceType === "resource") {
120
- await this.handleAssetOrResource(url, context, siteConfig);
120
+ await this.handleAssetOrResource(url, context, siteConfig, dispatcher);
121
121
  } else if (resourceType == "mapping") {
122
122
  await this.handleMappingResource(url, context, siteConfig, dispatcher);
123
123
  } else if (resourceType === "js") {
@@ -212,7 +212,7 @@ var SiteGenerator = class {
212
212
  const assets = viewDefinition.viewRecord.assetReferences || [];
213
213
  for (const asset of assets) {
214
214
  const assetUrl = asset.override?.uri || asset.url;
215
- if (assetUrl && !assetUrl.startsWith("data:")) {
215
+ if (assetUrl && !(0, import_shared_utils.isSelfUrl)(assetUrl)) {
216
216
  dispatchRequests.push(this.dispatchResourceRecursive(assetUrl, dispatcher, {resourceType: "asset", asset}, siteConfig));
217
217
  }
218
218
  }
@@ -274,9 +274,19 @@ var SiteGenerator = class {
274
274
  }
275
275
  }
276
276
  }
277
- async handleAssetOrResource(url, context, siteConfig) {
277
+ async handleAssetOrResource(url, context, siteConfig, dispatcher) {
278
+ const metadata = context.fs?.metadata;
278
279
  const fullPath = this.getResourcePathFromUrl(siteConfig, url);
279
280
  await (0, import_stream.writeResponse)(context, fullPath);
281
+ const assetReferences = metadata?.asset?.metadata?.assetReferences || [];
282
+ const dispatchRequests = [];
283
+ for (const ref of assetReferences) {
284
+ const refUrl = ref.override?.uri || ref.url;
285
+ dispatchRequests.push(this.dispatchResourceRecursive(refUrl, dispatcher, {resourceType: "asset", asset: metadata?.asset}, siteConfig).catch((err) => {
286
+ import_shared_utils.logger.warn(`Failed to fetch asset refrence => ${refUrl} from ${url}`, err);
287
+ }));
288
+ }
289
+ return Promise.all(dispatchRequests);
280
290
  }
281
291
  getResourcePathFromUrl(siteConfig, url) {
282
292
  const {outputDir} = siteConfig;
@@ -310,7 +320,7 @@ var SiteGenerator = class {
310
320
  if (assetSrcDir && import_fs_extra.default.existsSync(assetSrcDir)) {
311
321
  import_fs_extra.default.copySync(assetSrcDir, assetOutputDir);
312
322
  } else {
313
- throw new Error("Could not find assets to copy at path: " + assetSrcDir);
323
+ console.warn("[WARN] Could not find assets to copy at path: " + assetSrcDir);
314
324
  }
315
325
  } catch (e) {
316
326
  console.error("Error occurred processing asset config: " + JSON.stringify(asset));
@@ -390,7 +390,7 @@ export default function apiMiddleware(app, context) {
390
390
  const specifier = req.params[0] ? `/${req.params[0]}` : req.originalUrl;
391
391
  const basePathSpecifier = `${basePath}${specifier}`;
392
392
  try {
393
- const assetObj = await context.assetRegistry.getAsset({ specifier: basePathSpecifier, signature, type: type }, runtimeEnvironment);
393
+ const assetObj = await context.assetRegistry.getAsset({ specifier: basePathSpecifier, signature, type: type }, runtimeEnvironment, req.isSiteGeneration());
394
394
  if (immutable) {
395
395
  // WIP: ?
396
396
  }
@@ -3,6 +3,7 @@ import { DiagnosticsError } from '@lwrjs/diagnostics';
3
3
  import { LwrViewHandler } from '@lwrjs/view-registry';
4
4
  import { isSupportedEnvironment } from './utils.js';
5
5
  import { decodeViewPath, getClientBootstrapConfigurationRoutes, extractRequestParams, } from '@lwrjs/shared-utils';
6
+ import { getClientRoutes } from '@lwrjs/router';
6
7
  export default function uiMiddleware(app, context) {
7
8
  const { viewRegistry, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment } = context;
8
9
  const { environment: environmentConfig, routes, errorRoutes } = context.appConfig;
@@ -135,18 +136,30 @@ export default function uiMiddleware(app, context) {
135
136
  });
136
137
  }
137
138
  else {
138
- // vanity urls
139
- app.get(route.path, async (req, res) => {
140
- await sendViewResponse(req, res, route);
141
- });
142
- // canonical URL
143
- app.get([
144
- `/:apiVersion/application/:format/l/:locale/ai/:appId${route.path}`,
145
- `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${route.path}`,
146
- `/:apiVersion/application/:format/ai/:appId${route.path}`,
147
- `/:apiVersion/application/:format/e/:environment/ai/:appId${route.path}`,
148
- ], async (req, res) => {
149
- await sendViewResponse(req, res, route);
139
+ // Get the client-side routes
140
+ const serverPath = route.path;
141
+ const paths = [serverPath];
142
+ const subRoutes = route.subRoutes ? getClientRoutes(route.subRoutes) : undefined;
143
+ if (subRoutes) {
144
+ // Concatenate each client uri to the server route.path, creating the full list of composite paths
145
+ const prefix = serverPath === '/' ? '' : serverPath;
146
+ subRoutes.routes.forEach((r) => paths.push(`${prefix}${r.uri}`));
147
+ }
148
+ // Register the server route.path and each composite client path, to enable full page refreshes of client pages
149
+ paths.forEach((path) => {
150
+ // vanity urls
151
+ app.get(path, async (req, res) => {
152
+ await sendViewResponse(req, res, route);
153
+ });
154
+ // canonical URL
155
+ app.get([
156
+ `/:apiVersion/application/:format/l/:locale/ai/:appId${path}`,
157
+ `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${path}`,
158
+ `/:apiVersion/application/:format/ai/:appId${path}`,
159
+ `/:apiVersion/application/:format/e/:environment/ai/:appId${path}`,
160
+ ], async (req, res) => {
161
+ await sendViewResponse(req, res, route);
162
+ });
150
163
  });
151
164
  }
152
165
  });
@@ -1,5 +1,5 @@
1
1
  import { performance } from 'perf_hooks';
2
- import { getSpecifier, getFeatureFlags, hashContent, getModuleUriPrefix, getMappingUriPrefix, } from '@lwrjs/shared-utils';
2
+ import { getSpecifier, getFeatureFlags, hashContent, isSelfUrl, getModuleUriPrefix, getMappingUriPrefix, logger, } from '@lwrjs/shared-utils';
3
3
  import { join, dirname, extname } from 'path';
4
4
  import fs from 'fs-extra';
5
5
  import { writeResponse } from './utils/stream.js';
@@ -129,7 +129,7 @@ export default class SiteGenerator {
129
129
  // -- Process Assets (css, images, ...) && Resources (lwr-loader-shim ...)
130
130
  }
131
131
  else if (resourceType === 'asset' || resourceType === 'resource') {
132
- await this.handleAssetOrResource(url, context, siteConfig);
132
+ await this.handleAssetOrResource(url, context, siteConfig, dispatcher);
133
133
  // -- Import Metadata Mappings
134
134
  }
135
135
  else if (resourceType == 'mapping') {
@@ -306,8 +306,8 @@ export default class SiteGenerator {
306
306
  const assets = viewDefinition.viewRecord.assetReferences || [];
307
307
  for (const asset of assets) {
308
308
  const assetUrl = asset.override?.uri || asset.url;
309
- // skip empty asset urls / data urls (i.e. <img src="" /> <img src="data:image/png;base64, iVBORw0..." />)
310
- if (assetUrl && !assetUrl.startsWith('data:')) {
309
+ // skip self referential asset urls / data urls (i.e. <img src="" /> <img src="data:image/png;base64, iVBORw0..." />)
310
+ if (assetUrl && !isSelfUrl(assetUrl)) {
311
311
  dispatchRequests.push(this.dispatchResourceRecursive(assetUrl, dispatcher, { resourceType: 'asset', asset }, siteConfig));
312
312
  }
313
313
  }
@@ -394,9 +394,21 @@ export default class SiteGenerator {
394
394
  * @param context - Response Context
395
395
  * @param siteConfig - Global metadata about the site
396
396
  */
397
- async handleAssetOrResource(url, context, siteConfig) {
397
+ async handleAssetOrResource(url, context, siteConfig, dispatcher) {
398
+ const metadata = context.fs?.metadata;
398
399
  const fullPath = this.getResourcePathFromUrl(siteConfig, url);
399
400
  await writeResponse(context, fullPath);
401
+ // Call and referenced assets...
402
+ const assetReferences = metadata?.asset?.metadata?.assetReferences || [];
403
+ const dispatchRequests = [];
404
+ for (const ref of assetReferences) {
405
+ const refUrl = ref.override?.uri || ref.url;
406
+ dispatchRequests.push(this.dispatchResourceRecursive(refUrl, dispatcher, { resourceType: 'asset', asset: metadata?.asset }, siteConfig).catch((err) => {
407
+ // Warn the user that the we failed to fetch a referenced asset
408
+ logger.warn(`Failed to fetch asset refrence => ${refUrl} from ${url}`, err);
409
+ }));
410
+ }
411
+ return Promise.all(dispatchRequests);
400
412
  }
401
413
  getResourcePathFromUrl(siteConfig, url) {
402
414
  const { outputDir } = siteConfig;
@@ -451,7 +463,7 @@ export default class SiteGenerator {
451
463
  fs.copySync(assetSrcDir, assetOutputDir);
452
464
  }
453
465
  else {
454
- throw new Error('Could not find assets to copy at path: ' + assetSrcDir);
466
+ console.warn('[WARN] Could not find assets to copy at path: ' + assetSrcDir);
455
467
  }
456
468
  }
457
469
  catch (e) {
@@ -8,7 +8,7 @@ export interface RouteResourceOpts extends BaseResourceContextOpts {
8
8
  }
9
9
  export interface AssetResourceOpts extends BaseResourceContextOpts {
10
10
  resourceType: 'asset';
11
- asset: RenderedAssetReference;
11
+ asset?: RenderedAssetReference;
12
12
  }
13
13
  export interface JsResourceOpts extends BaseResourceContextOpts {
14
14
  resourceType: 'js';
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.8.0-alpha.11",
7
+ "version": "0.8.0-alpha.13",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -32,32 +32,32 @@
32
32
  "package.cjs"
33
33
  ],
34
34
  "dependencies": {
35
- "@lwrjs/app-service": "0.8.0-alpha.11",
36
- "@lwrjs/asset-registry": "0.8.0-alpha.11",
37
- "@lwrjs/asset-transformer": "0.8.0-alpha.11",
38
- "@lwrjs/base-template-engine": "0.8.0-alpha.11",
39
- "@lwrjs/base-view-provider": "0.8.0-alpha.11",
40
- "@lwrjs/base-view-transformer": "0.8.0-alpha.11",
41
- "@lwrjs/client-modules": "0.8.0-alpha.11",
42
- "@lwrjs/compiler": "0.8.0-alpha.11",
43
- "@lwrjs/config": "0.8.0-alpha.11",
44
- "@lwrjs/diagnostics": "0.8.0-alpha.11",
45
- "@lwrjs/fs-asset-provider": "0.8.0-alpha.11",
46
- "@lwrjs/html-view-provider": "0.8.0-alpha.11",
47
- "@lwrjs/loader": "0.8.0-alpha.11",
48
- "@lwrjs/lwc-module-provider": "0.8.0-alpha.11",
49
- "@lwrjs/lwc-ssr": "0.8.0-alpha.11",
50
- "@lwrjs/markdown-view-provider": "0.8.0-alpha.11",
51
- "@lwrjs/module-bundler": "0.8.0-alpha.11",
52
- "@lwrjs/module-registry": "0.8.0-alpha.11",
53
- "@lwrjs/npm-module-provider": "0.8.0-alpha.11",
54
- "@lwrjs/nunjucks-view-provider": "0.8.0-alpha.11",
55
- "@lwrjs/o11y": "0.8.0-alpha.11",
56
- "@lwrjs/resource-registry": "0.8.0-alpha.11",
57
- "@lwrjs/router": "0.8.0-alpha.11",
58
- "@lwrjs/server": "0.8.0-alpha.11",
59
- "@lwrjs/shared-utils": "0.8.0-alpha.11",
60
- "@lwrjs/view-registry": "0.8.0-alpha.11",
35
+ "@lwrjs/app-service": "0.8.0-alpha.13",
36
+ "@lwrjs/asset-registry": "0.8.0-alpha.13",
37
+ "@lwrjs/asset-transformer": "0.8.0-alpha.13",
38
+ "@lwrjs/base-template-engine": "0.8.0-alpha.13",
39
+ "@lwrjs/base-view-provider": "0.8.0-alpha.13",
40
+ "@lwrjs/base-view-transformer": "0.8.0-alpha.13",
41
+ "@lwrjs/client-modules": "0.8.0-alpha.13",
42
+ "@lwrjs/compiler": "0.8.0-alpha.13",
43
+ "@lwrjs/config": "0.8.0-alpha.13",
44
+ "@lwrjs/diagnostics": "0.8.0-alpha.13",
45
+ "@lwrjs/fs-asset-provider": "0.8.0-alpha.13",
46
+ "@lwrjs/html-view-provider": "0.8.0-alpha.13",
47
+ "@lwrjs/loader": "0.8.0-alpha.13",
48
+ "@lwrjs/lwc-module-provider": "0.8.0-alpha.13",
49
+ "@lwrjs/lwc-ssr": "0.8.0-alpha.13",
50
+ "@lwrjs/markdown-view-provider": "0.8.0-alpha.13",
51
+ "@lwrjs/module-bundler": "0.8.0-alpha.13",
52
+ "@lwrjs/module-registry": "0.8.0-alpha.13",
53
+ "@lwrjs/npm-module-provider": "0.8.0-alpha.13",
54
+ "@lwrjs/nunjucks-view-provider": "0.8.0-alpha.13",
55
+ "@lwrjs/o11y": "0.8.0-alpha.13",
56
+ "@lwrjs/resource-registry": "0.8.0-alpha.13",
57
+ "@lwrjs/router": "0.8.0-alpha.13",
58
+ "@lwrjs/server": "0.8.0-alpha.13",
59
+ "@lwrjs/shared-utils": "0.8.0-alpha.13",
60
+ "@lwrjs/view-registry": "0.8.0-alpha.13",
61
61
  "fs-extra": "^10.1.0",
62
62
  "ms": "^2.1.3",
63
63
  "path-to-regexp": "^6.2.0",
@@ -65,7 +65,7 @@
65
65
  "ws": "^8.8.1"
66
66
  },
67
67
  "devDependencies": {
68
- "@lwrjs/types": "0.8.0-alpha.11",
68
+ "@lwrjs/types": "0.8.0-alpha.13",
69
69
  "@types/ws": "^8.5.3"
70
70
  },
71
71
  "peerDependencies": {
@@ -74,5 +74,5 @@
74
74
  "engines": {
75
75
  "node": ">=14.15.4 <19"
76
76
  },
77
- "gitHead": "e8a2c6811e4a52227219f2d94648e0eae9800fbc"
77
+ "gitHead": "cc4bc856f34d2ec15df550503dd7463fa31b30e5"
78
78
  }