@lwrjs/view-registry 0.9.0-alpha.3 → 0.9.0-alpha.31

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.
@@ -278,8 +278,11 @@ var LwrViewRegistry = class {
278
278
  importer: importer || renderedView.compiledView.filePath
279
279
  };
280
280
  const stringBuilder = (0, import_shared_utils.createStringBuilder)(renderedViewContent);
281
+ let pageTtl;
281
282
  for (const viewTransformer of this.viewTransformers) {
282
- await viewTransformer.link?.(stringBuilder, mergedViewContext, linkedMetadata);
283
+ const linkResults = await viewTransformer.link?.(stringBuilder, mergedViewContext, linkedMetadata);
284
+ const ttl = linkResults && linkResults.cache?.ttl;
285
+ pageTtl = (0, import_shared_utils.shortestTtl)(ttl || void 0, pageTtl);
283
286
  }
284
287
  const linkedAssetContent = stringBuilder.toString();
285
288
  if (linkedAssetContent.includes(lwrResourcesId)) {
@@ -290,7 +293,8 @@ var LwrViewRegistry = class {
290
293
  moduleBundler,
291
294
  resourceRegistry,
292
295
  runtimeEnvironment,
293
- runtimeParams
296
+ runtimeParams,
297
+ bundleConfig: this.globalConfig.bundleConfig
294
298
  });
295
299
  return {
296
300
  renderedView: linkedView,
@@ -298,7 +302,8 @@ var LwrViewRegistry = class {
298
302
  viewRecord: {
299
303
  assetReferences: (0, import_utils.reduceSourceAssetReferences)(linkedMetadata.assetReferences),
300
304
  ...viewRecord
301
- }
305
+ },
306
+ cache: {ttl: pageTtl}
302
307
  };
303
308
  }
304
309
  return {
@@ -306,7 +311,8 @@ var LwrViewRegistry = class {
306
311
  immutable,
307
312
  viewRecord: {
308
313
  assetReferences: (0, import_utils.reduceSourceAssetReferences)(linkedMetadata.assetReferences)
309
- }
314
+ },
315
+ cache: {ttl: pageTtl}
310
316
  };
311
317
  }
312
318
  };
@@ -37,9 +37,20 @@ async function getHtmlResources(view, viewParams, resourceContext) {
37
37
  moduleRegistry,
38
38
  moduleBundler,
39
39
  resourceRegistry,
40
- viewMetadata
40
+ viewMetadata,
41
+ bundleConfig: {external = {}}
41
42
  } = resourceContext;
42
- const {id: appName, bootstrap: {services} = {services: []}} = view;
43
+ const isExternal = function(rawSpecifier) {
44
+ const {specifier} = (0, import_shared_utils.explodeSpecifier)(rawSpecifier);
45
+ return Object.keys(external).some((e) => specifier === e);
46
+ };
47
+ const {
48
+ id: appName,
49
+ bootstrap: {services, module: bootstrapModule, preloadModules = []} = {
50
+ services: [],
51
+ preloadModules: []
52
+ }
53
+ } = view;
43
54
  const {lwrVersion, format, hmrEnabled, bundle, debug, minify} = runtimeEnvironment;
44
55
  const {customElements} = viewMetadata;
45
56
  const version = lwrVersion;
@@ -49,7 +60,8 @@ async function getHtmlResources(view, viewParams, resourceContext) {
49
60
  format: runtimeEnvironment.format,
50
61
  resourceType: import_identity.AppResourceEnum.MODULE
51
62
  };
52
- const bootstrapSpecifier = (0, import_identity.getAppSpecifier)(appIdentity);
63
+ const bootstrapSpecifier = bootstrapModule || (0, import_identity.getAppSpecifier)(appIdentity);
64
+ const preloadModuleUris = new Set();
53
65
  const moduleResources = [];
54
66
  const requiredResources = [];
55
67
  const configResources = [];
@@ -60,35 +72,49 @@ async function getHtmlResources(view, viewParams, resourceContext) {
60
72
  const preloadAmdModules = [];
61
73
  const isSSR = view.bootstrap?.ssr;
62
74
  if (isAMD) {
63
- const shimBundle = !bundle ? "lwr-loader-shim-legacy.js" : debug || minify === false ? "lwr-loader-shim-legacy.bundle.js" : "lwr-loader-shim-legacy.bundle.min.js";
75
+ const shimBundle = debug || minify === false ? "lwr-loader-shim-legacy.bundle.js" : "lwr-loader-shim-legacy.bundle.min.js";
64
76
  const def = await resourceRegistry.getResource({specifier: shimBundle, version}, runtimeEnvironment, runtimeParams);
65
77
  if (!def) {
66
78
  throw Error("Failed to find definition of resource: " + shimBundle);
67
79
  }
68
- if (def.stream) {
69
- def.stream.destroy();
70
- }
71
80
  requiredResources.push(def);
72
81
  const errorShimDef = await resourceRegistry.getResource({specifier: "lwr-error-shim.js", version}, runtimeEnvironment, runtimeParams);
73
82
  if (!errorShimDef) {
74
83
  throw Error("Failed to find definition of resource: lwr-error-shim.js");
75
84
  }
76
85
  requiredResources.push(errorShimDef);
77
- if (!bundle) {
78
- requiredResources.push(await (0, import_utils.getModuleResource)({
79
- specifier: "lwr/loaderLegacy",
80
- version
81
- }, runtimeEnvironment, {
82
- isPreload: false,
83
- isSSR
84
- }, moduleRegistry, runtimeParams));
85
- }
86
86
  }
87
87
  const depth = isAMD ? {static: import_shared_utils.GraphDepth.ALL, dynamic: 1} : {static: import_shared_utils.GraphDepth.NONE, dynamic: 0};
88
- const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
88
+ const defRegistry = bundle ? moduleBundler : moduleRegistry;
89
+ const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
89
90
  const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
90
91
  const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
91
92
  moduleResources.push((0, import_utils.getModuleResourceByUri)(uri, runtimeEnvironment, {isPreload: false, isSSR}));
93
+ for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
94
+ if (!isExternal(depSpecifier)) {
95
+ preloadModuleUris.add(bootstrapModuleGraph.uriMap[depSpecifier]);
96
+ preloadAmdModules.push(depSpecifier);
97
+ }
98
+ }
99
+ for (let i = 0; i < preloadModules.length; i++) {
100
+ const specifier = preloadModules[i];
101
+ if (isExternal(specifier)) {
102
+ import_shared_utils.logger.warn(`"${specifier}" is configured in both bundleConfig.externals and bootstrap.preloadModules. We are treating it as external.`);
103
+ } else {
104
+ const versionedModuleId = await (0, import_shared_utils.getVersionedModuleId)(specifier, moduleRegistry);
105
+ const versionedModuleSpecifier = (0, import_shared_utils.getSpecifier)({
106
+ specifier,
107
+ version: (0, import_shared_utils.normalizeVersionToUri)(versionedModuleId.version)
108
+ });
109
+ const uri2 = bootstrapModuleGraph.uriMap[versionedModuleSpecifier] || await defRegistry.resolveModuleUri(versionedModuleId, runtimeEnvironment, runtimeParams);
110
+ preloadModuleUris.add(uri2);
111
+ if (versionedModuleId.version == import_shared_utils.VERSION_NOT_PROVIDED) {
112
+ preloadAmdModules.push(specifier);
113
+ } else {
114
+ preloadAmdModules.push(versionedModuleSpecifier);
115
+ }
116
+ }
117
+ }
92
118
  if (isAMD) {
93
119
  requiredAmdModules.push(versionedSpecifier);
94
120
  imports[versionedSpecifier] = uri;
@@ -96,7 +122,6 @@ async function getHtmlResources(view, viewParams, resourceContext) {
96
122
  const uri2 = bootstrapModuleGraph.uriMap[staticDep];
97
123
  imports[staticDep] = uri2;
98
124
  if (services && services.length) {
99
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
100
125
  requiredAmdModules.push(staticDep);
101
126
  }
102
127
  }
@@ -112,11 +137,19 @@ async function getHtmlResources(view, viewParams, resourceContext) {
112
137
  const customElementsRecords = [];
113
138
  const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements, isSSR);
114
139
  await Promise.all(flattenedElements.map(async ({tagName: element, props}) => {
115
- const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabCaseToModuleSpecifer)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
140
+ const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabCaseToModuleSpecifier)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
116
141
  customElementsRecords.push({elementName: element, flatGraph: graph});
117
142
  const specifier = graph.graphs[0].specifier;
118
143
  const uri2 = graph.uriMap[specifier];
119
144
  moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
145
+ if (bundle) {
146
+ for (const depSpecifier of graph.graphs[0].static) {
147
+ if (!isExternal(depSpecifier)) {
148
+ preloadModuleUris.add(graph.uriMap[depSpecifier]);
149
+ preloadAmdModules.push(depSpecifier);
150
+ }
151
+ }
152
+ }
120
153
  rootComponents.push(specifier);
121
154
  imports[specifier] = uri2;
122
155
  if (isAMD) {
@@ -131,6 +164,7 @@ async function getHtmlResources(view, viewParams, resourceContext) {
131
164
  }
132
165
  }
133
166
  }));
167
+ const dedupedPreloadAmdModules = [...new Set(preloadAmdModules)];
134
168
  configResources.unshift(await (0, import_utils2.getViewBootstrapConfigurationResource)({
135
169
  id: view.id,
136
170
  url: viewParams?.page?.url,
@@ -145,11 +179,14 @@ async function getHtmlResources(view, viewParams, resourceContext) {
145
179
  },
146
180
  rootComponents,
147
181
  ...isAMD && {requiredModules: requiredAmdModules},
148
- ...isAMD && {preloadModules: preloadAmdModules}
182
+ ...isAMD && {preloadModules: dedupedPreloadAmdModules}
149
183
  }, runtimeEnvironment, runtimeParams));
150
184
  if (!isAMD && hmrEnabled) {
151
185
  configResources.unshift(await (0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
152
186
  }
187
+ for (const preloadUri of preloadModuleUris) {
188
+ moduleResources.unshift((0, import_utils.getModuleResourceByUri)(preloadUri, runtimeEnvironment, {isPreload: true, isSSR}));
189
+ }
153
190
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(import_utils.generateHtmlTag));
154
191
  return {
155
192
  partial: htmlResources.join("\n"),
@@ -37,11 +37,23 @@ async function getHtmlResources(view, viewParams, resourceContext) {
37
37
  moduleRegistry,
38
38
  moduleBundler,
39
39
  resourceRegistry,
40
- viewMetadata
40
+ viewMetadata,
41
+ bundleConfig: {external = {}}
41
42
  } = resourceContext;
42
- const {id: appName, bootstrap: {services} = {services: []}} = view;
43
+ const isExternal = function(rawSpecifier) {
44
+ const {specifier} = (0, import_shared_utils.explodeSpecifier)(rawSpecifier);
45
+ return Object.keys(external).some((e) => specifier === e);
46
+ };
47
+ const {
48
+ id: appName,
49
+ bootstrap: {services, module: bootstrapModule, preloadModules = []} = {
50
+ services: [],
51
+ preloadModules: []
52
+ }
53
+ } = view;
43
54
  const {lwrVersion, format, hmrEnabled, bundle, debug, minify} = runtimeEnvironment;
44
55
  const {customElements} = viewMetadata;
56
+ const defRegistry = bundle ? moduleBundler : moduleRegistry;
45
57
  const version = lwrVersion;
46
58
  const isAMD = format === "amd";
47
59
  const appIdentity = {
@@ -49,7 +61,8 @@ async function getHtmlResources(view, viewParams, resourceContext) {
49
61
  format: runtimeEnvironment.format,
50
62
  resourceType: import_identity.AppResourceEnum.MODULE
51
63
  };
52
- const bootstrapSpecifier = (0, import_identity.getAppSpecifier)(appIdentity);
64
+ const bootstrapSpecifier = bootstrapModule || (0, import_identity.getAppSpecifier)(appIdentity);
65
+ const preloadModuleUris = new Set();
53
66
  const moduleResources = [];
54
67
  const requiredResources = [];
55
68
  const configResources = [];
@@ -60,34 +73,48 @@ async function getHtmlResources(view, viewParams, resourceContext) {
60
73
  const preloadAmdModules = [];
61
74
  const isSSR = view.bootstrap?.ssr;
62
75
  if (isAMD) {
63
- const shimBundle = !bundle ? "lwr-loader-shim.js" : debug || minify === false ? "lwr-loader-shim.bundle.js" : "lwr-loader-shim.bundle.min.js";
76
+ const shimBundle = debug || minify === false ? "lwr-loader-shim.bundle.js" : "lwr-loader-shim.bundle.min.js";
64
77
  const def = await resourceRegistry.getResource({specifier: shimBundle, version}, runtimeEnvironment, runtimeParams);
65
78
  if (!def) {
66
79
  throw Error("Failed to find definition of resource: " + shimBundle);
67
80
  }
68
- if (def.stream) {
69
- def.stream.destroy();
70
- }
71
81
  requiredResources.push(def);
72
82
  const errorShimDef = await resourceRegistry.getResource({specifier: "lwr-error-shim.js", version}, runtimeEnvironment, runtimeParams);
73
83
  if (!errorShimDef) {
74
84
  throw Error("Failed to find definition of resource: lwr-error-shim.js");
75
85
  }
76
86
  requiredResources.push(errorShimDef);
77
- if (!bundle) {
78
- requiredResources.push(await (0, import_utils.getModuleResource)({
79
- specifier: "lwr/loader",
80
- version
81
- }, runtimeEnvironment, {
82
- isSSR
83
- }, moduleRegistry, runtimeParams));
84
- }
85
87
  }
86
88
  const depth = isAMD ? {static: import_shared_utils.GraphDepth.ALL, dynamic: 1} : {static: import_shared_utils.GraphDepth.NONE, dynamic: 1};
87
- const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
89
+ const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
88
90
  const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
89
91
  const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
90
92
  moduleResources.push((0, import_utils.getModuleResourceByUri)(uri, runtimeEnvironment, {isPreload: false, isSSR}));
93
+ for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
94
+ if (!isExternal(depSpecifier)) {
95
+ preloadModuleUris.add(bootstrapModuleGraph.uriMap[depSpecifier]);
96
+ preloadAmdModules.push(depSpecifier);
97
+ }
98
+ }
99
+ for (let i = 0; i < preloadModules.length; i++) {
100
+ const specifier = preloadModules[i];
101
+ if (isExternal(specifier)) {
102
+ import_shared_utils.logger.warn(`"${specifier}" is configured in both bundleConfig.externals and bootstrap.preloadModules. We are treating it as external.`);
103
+ } else {
104
+ const versionedModuleId = await (0, import_shared_utils.getVersionedModuleId)(specifier, moduleRegistry);
105
+ const versionedModuleSpecifier = (0, import_shared_utils.getSpecifier)({
106
+ specifier,
107
+ version: (0, import_shared_utils.normalizeVersionToUri)(versionedModuleId.version)
108
+ });
109
+ const uri2 = bootstrapModuleGraph.uriMap[versionedModuleSpecifier] || await defRegistry.resolveModuleUri(versionedModuleId, runtimeEnvironment, runtimeParams);
110
+ preloadModuleUris.add(uri2);
111
+ if (versionedModuleId.version == import_shared_utils.VERSION_NOT_PROVIDED) {
112
+ preloadAmdModules.push(specifier);
113
+ } else {
114
+ preloadAmdModules.push(versionedModuleSpecifier);
115
+ }
116
+ }
117
+ }
91
118
  if (isAMD) {
92
119
  requiredAmdModules.push(versionedSpecifier);
93
120
  imports[versionedSpecifier] = uri;
@@ -95,7 +122,6 @@ async function getHtmlResources(view, viewParams, resourceContext) {
95
122
  const uri2 = bootstrapModuleGraph.uriMap[staticDep];
96
123
  imports[staticDep] = uri2;
97
124
  if (services && services.length) {
98
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
99
125
  requiredAmdModules.push(staticDep);
100
126
  }
101
127
  }
@@ -111,12 +137,20 @@ async function getHtmlResources(view, viewParams, resourceContext) {
111
137
  let importMetadata = await (0, import_shared_utils.toImportMetadata)(bootstrapModuleGraph, {imports: {}, index: {}}, moduleRegistry, runtimeEnvironment, runtimeParams);
112
138
  const customElementsRecords = [];
113
139
  const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements, isSSR);
114
- await Promise.all(flattenedElements.map(async ({tagName: element, props}) => {
115
- const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabCaseToModuleSpecifer)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
140
+ for (const {tagName: element} of flattenedElements) {
141
+ const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabCaseToModuleSpecifier)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
116
142
  customElementsRecords.push({elementName: element, flatGraph: graph});
117
143
  const specifier = graph.graphs[0].specifier;
118
144
  const uri2 = graph.uriMap[specifier];
119
145
  moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
146
+ if (bundle) {
147
+ for (const depSpecifier of graph.graphs[0].static) {
148
+ if (!isExternal(depSpecifier)) {
149
+ preloadModuleUris.add(graph.uriMap[depSpecifier]);
150
+ preloadAmdModules.push(depSpecifier);
151
+ }
152
+ }
153
+ }
120
154
  rootComponents.push(specifier);
121
155
  imports[specifier] = uri2;
122
156
  if (isAMD) {
@@ -131,7 +165,8 @@ async function getHtmlResources(view, viewParams, resourceContext) {
131
165
  }
132
166
  }
133
167
  importMetadata = await (0, import_shared_utils.toImportMetadata)(graph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
134
- }));
168
+ }
169
+ const dedupedPreloadAmdModules = [...new Set(preloadAmdModules)];
135
170
  configResources.unshift(await (0, import_utils2.getViewBootstrapConfigurationResource)({
136
171
  id: view.id,
137
172
  url: viewParams?.page?.url,
@@ -144,11 +179,14 @@ async function getHtmlResources(view, viewParams, resourceContext) {
144
179
  index: importMetadata?.index,
145
180
  rootComponents,
146
181
  ...isAMD && {requiredModules: requiredAmdModules},
147
- ...isAMD && {preloadModules: preloadAmdModules}
182
+ ...isAMD && {preloadModules: dedupedPreloadAmdModules}
148
183
  }, runtimeEnvironment, runtimeParams));
149
184
  if (!isAMD && hmrEnabled) {
150
185
  configResources.unshift(await (0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
151
186
  }
187
+ for (const preloadUri of preloadModuleUris) {
188
+ moduleResources.unshift((0, import_utils.getModuleResourceByUri)(preloadUri, runtimeEnvironment, {isPreload: true, isSSR}));
189
+ }
152
190
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(import_utils.generateHtmlTag));
153
191
  const mapping = (0, import_shared_utils.getMappingUriPrefix)(runtimeEnvironment, runtimeParams);
154
192
  const endpoints = {
@@ -29,19 +29,15 @@ __export(exports, {
29
29
  generatePageContext: () => generatePageContext,
30
30
  getModuleResource: () => getModuleResource,
31
31
  getModuleResourceByUri: () => getModuleResourceByUri,
32
- getRouteHandler: () => getRouteHandler,
33
32
  isViewResponse: () => isViewResponse,
34
33
  normalizeRenderOptions: () => normalizeRenderOptions,
35
34
  normalizeRenderedResult: () => normalizeRenderedResult,
36
35
  reduceSourceAssetReferences: () => reduceSourceAssetReferences,
37
36
  toJsonFormat: () => toJsonFormat
38
37
  });
39
- var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
40
38
  var import_path = __toModule(require("path"));
41
- var import_path2 = __toModule(require("path"));
42
- var import_shared_utils2 = __toModule(require("@lwrjs/shared-utils"));
39
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
43
40
  var import_identity = __toModule(require("@lwrjs/app-service/identity"));
44
- var import_shared_utils3 = __toModule(require("@lwrjs/shared-utils"));
45
41
  function streamToString(stream) {
46
42
  const chunks = [];
47
43
  return new Promise((resolve, reject) => {
@@ -85,7 +81,7 @@ async function generateInlineTag({specifier, type, content, stream, nonce}) {
85
81
  if (!content && !stream) {
86
82
  throw new Error(`Invalid inline Resource Definition: must have either "content" or "stream": "${specifier}"`);
87
83
  }
88
- const code = content ? content : await streamToString(stream);
84
+ const code = stream ? await streamToString(stream()) : content;
89
85
  return `<${tag}${typeStr}${nonceStr}>${code}</${tag}>`;
90
86
  }
91
87
  async function generateHtmlTag(definition) {
@@ -132,7 +128,7 @@ function normalizeRenderOptions(runtimeEnvironment, overrideRenderOptions, baseR
132
128
  };
133
129
  }
134
130
  function getTitleFromFilePath(filePath) {
135
- return filePath ? (0, import_path2.basename)(filePath, (0, import_path2.extname)(filePath)) : import_shared_utils.DEFAULT_TITLE;
131
+ return filePath ? (0, import_path.basename)(filePath, (0, import_path.extname)(filePath)) : import_shared_utils.DEFAULT_TITLE;
136
132
  }
137
133
  function generatePageContext({requestPath: url}, {id, contentTemplate, properties}) {
138
134
  const title = properties?.title || getTitleFromFilePath(contentTemplate);
@@ -141,19 +137,6 @@ function generatePageContext({requestPath: url}, {id, contentTemplate, propertie
141
137
  function isViewResponse(response) {
142
138
  return response.body !== void 0;
143
139
  }
144
- async function getRouteHandler(path, {cacheDir, rootDir}) {
145
- try {
146
- const fullPath = (0, import_shared_utils.resolveFileExtension)(path);
147
- if (fullPath.endsWith(".ts")) {
148
- path = await (0, import_shared_utils.transpileTs)(path, {rootDir, cacheDir: import_path.default.join(cacheDir, "routeHandlers")});
149
- }
150
- const moduleEntry = await Promise.resolve().then(() => __toModule(require(path)));
151
- return moduleEntry.default || moduleEntry;
152
- } catch (err) {
153
- console.log(err);
154
- throw new Error(`Unable to get routeHandler: ${path}`);
155
- }
156
- }
157
140
  async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEnvironment, runtimeParams, moduleRegistry) {
158
141
  const {viewRecord} = viewDefinition;
159
142
  const {bootstrap, id: appName} = route;
@@ -186,7 +169,7 @@ async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEnvironme
186
169
  });
187
170
  resources.push({
188
171
  type: "application/javascript",
189
- src: (0, import_shared_utils3.getClientBootstrapConfigurationUri)({url: viewRequest.url, id: route.id}, runtimeEnvironment, runtimeParams)
172
+ src: (0, import_shared_utils.getClientBootstrapConfigurationUri)({url: viewRequest.url, id: route.id}, runtimeEnvironment, runtimeParams)
190
173
  });
191
174
  }
192
175
  const mappingUrl = (0, import_shared_utils.getMappingUriPrefix)(runtimeEnvironment, runtimeParams);
@@ -206,7 +189,7 @@ async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEnvironme
206
189
  });
207
190
  viewRecord.assetReferences?.forEach((asset) => {
208
191
  if (asset.override?.uri) {
209
- const type = (0, import_shared_utils2.mimeLookup)(asset.override?.uri);
192
+ const type = (0, import_shared_utils.mimeLookup)(asset.override?.uri);
210
193
  resources.push({type, src: asset.override?.uri});
211
194
  }
212
195
  });
@@ -229,10 +212,10 @@ async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEnvironme
229
212
  }
230
213
  };
231
214
  }
232
- async function getModuleResource(moduleId, runtimeEnvironment, moduleResouceMeta, moduleRegistry, runtimeParams) {
215
+ async function getModuleResource(moduleId, runtimeEnvironment, moduleResouceMeta, defRegistry, runtimeParams) {
233
216
  const {format} = runtimeEnvironment;
234
217
  const {isSSR = false, isPreload = false} = moduleResouceMeta;
235
- const moduleUri = await moduleRegistry.resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams);
218
+ const moduleUri = await defRegistry.resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams);
236
219
  return {
237
220
  src: moduleUri,
238
221
  type: format === "amd" ? "application/javascript" : "module",
@@ -267,7 +250,7 @@ async function createJsonModule(specifier, moduleRegistry, environment, params)
267
250
  version,
268
251
  ownHash,
269
252
  links: {
270
- self: moduleRegistry.resolveModuleUri(moduleIdentifier, environment, params, ownHash)
253
+ self: await moduleRegistry.resolveModuleUri(moduleIdentifier, environment, params, ownHash)
271
254
  }
272
255
  };
273
256
  }
@@ -31,15 +31,13 @@ var import_utils = __toModule(require("./utils.cjs"));
31
31
  var import_path = __toModule(require("path"));
32
32
  var LwrViewHandler = class {
33
33
  constructor(context, globalConfig) {
34
- this.inflightRouteHandlerEvalMap = new Map();
35
- this.routeHandlerFunctionMap = new Map();
36
- this.viewRegistry = context.viewRegistry;
37
34
  this.globalConfig = globalConfig;
35
+ this.routeHandlers = context.routeHandlers;
36
+ this.viewRegistry = context.viewRegistry;
38
37
  this.moduleRegistry = context.moduleRegistry;
39
38
  }
40
39
  async getViewContent(viewRequest, route, runtimeEnvironment, runtimeParams = {}) {
41
- const {routeHandler} = route;
42
- if (routeHandler) {
40
+ if (route.routeHandler) {
43
41
  const response = await this.getRouteHandlerResponse(viewRequest, route, runtimeEnvironment, runtimeParams);
44
42
  if ((0, import_utils.isViewResponse)(response)) {
45
43
  return response;
@@ -55,7 +53,8 @@ var LwrViewHandler = class {
55
53
  body: viewDefinition2.renderedView,
56
54
  metadata: {
57
55
  viewDefinition: viewDefinition2
58
- }
56
+ },
57
+ cache: {ttl: (0, import_shared_utils.shortestTtl)(response.cache?.ttl, viewDefinition2.cache?.ttl)}
59
58
  };
60
59
  }
61
60
  const viewDefinition = await this.getDefaultRouteViewDefinition(viewRequest, route, runtimeEnvironment, runtimeParams);
@@ -63,12 +62,12 @@ var LwrViewHandler = class {
63
62
  body: viewDefinition.renderedView,
64
63
  metadata: {
65
64
  viewDefinition
66
- }
65
+ },
66
+ cache: viewDefinition.cache
67
67
  };
68
68
  }
69
69
  async getViewJson(viewRequest, route, runtimeEnvironment, runtimeParams = {}) {
70
- const {routeHandler} = route;
71
- if (routeHandler) {
70
+ if (route.routeHandler) {
72
71
  const response = await this.getRouteHandlerResponse(viewRequest, route, runtimeEnvironment, runtimeParams);
73
72
  if ((0, import_utils.isViewResponse)(response)) {
74
73
  return response;
@@ -84,9 +83,8 @@ var LwrViewHandler = class {
84
83
  return await (0, import_utils.toJsonFormat)(viewRequest, viewDefinition, route, runtimeEnvironment, runtimeParams, this.moduleRegistry);
85
84
  }
86
85
  async getViewConfiguration(viewRequest, route, runtimeEnvironment, runtimeParams = {}) {
87
- const {routeHandler} = route;
88
86
  let viewDefinition;
89
- if (routeHandler) {
87
+ if (route.routeHandler) {
90
88
  const response = await this.getRouteHandlerResponse(viewRequest, route, runtimeEnvironment, runtimeParams);
91
89
  if ((0, import_utils.isViewResponse)(response)) {
92
90
  return;
@@ -126,28 +124,18 @@ var LwrViewHandler = class {
126
124
  return viewDefinition;
127
125
  }
128
126
  async getRouteHandlerResponse(viewRequest, route, runtimeEnvironment, runtimeParams = {}) {
129
- const {rootDir, assets, contentDir, layoutsDir, cacheDir} = this.globalConfig;
130
- const paths = {rootDir, assets, contentDir, layoutsDir};
131
- const {routeHandler} = route;
132
- if (!routeHandler) {
133
- throw new Error("Route Handler is required for a CustomView");
127
+ if (!route.routeHandler) {
128
+ throw new Error("Route handler is required for a CustomView");
134
129
  }
135
- let routeHandlerFunction = this.routeHandlerFunctionMap.get(routeHandler);
136
- if (!routeHandlerFunction) {
137
- const inflightRouteHandlerPromise = this.inflightRouteHandlerEvalMap.get(routeHandler);
138
- if (inflightRouteHandlerPromise) {
139
- routeHandlerFunction = await inflightRouteHandlerPromise;
140
- } else {
141
- const handlerPromise = (0, import_utils.getRouteHandler)(routeHandler, {cacheDir, rootDir});
142
- this.inflightRouteHandlerEvalMap.set(routeHandler, handlerPromise);
143
- routeHandlerFunction = await handlerPromise;
144
- }
145
- this.routeHandlerFunctionMap.set(routeHandler, routeHandlerFunction);
146
- this.inflightRouteHandlerEvalMap.delete(routeHandler);
130
+ const routeHandler = this.routeHandlers[route.routeHandler];
131
+ if (!routeHandler) {
132
+ throw new Error(`Route handler does not exist for id: ${route.routeHandler}`);
147
133
  }
134
+ const {rootDir, assets, contentDir, layoutsDir} = this.globalConfig;
135
+ const paths = {rootDir, assets, contentDir, layoutsDir};
148
136
  const locale = runtimeParams.locale;
149
137
  const viewApi = this.getBoundApi(route, runtimeEnvironment, runtimeParams);
150
- const response = await routeHandlerFunction({...viewRequest, locale}, {route, viewApi, ...paths});
138
+ const response = await routeHandler({...viewRequest, locale}, {route, viewApi, ...paths});
151
139
  return response;
152
140
  }
153
141
  getBoundApi(route, runtimeEnvironment, runtimeParams) {
@@ -169,7 +157,8 @@ var LwrViewHandler = class {
169
157
  body: viewDefinition.renderedView,
170
158
  metadata: {
171
159
  viewDefinition
172
- }
160
+ },
161
+ cache: viewDefinition.cache
173
162
  };
174
163
  }
175
164
  };
package/build/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { extractMetadataFromHtml, getCacheKeyFromJson, getSpecifier, normalizeResourcePath, createStringBuilder, InflightTasks, } from '@lwrjs/shared-utils';
1
+ import { extractMetadataFromHtml, getCacheKeyFromJson, getSpecifier, normalizeResourcePath, createStringBuilder, shortestTtl, InflightTasks, } from '@lwrjs/shared-utils';
2
2
  import { normalizeRenderOptions, normalizeRenderedResult, reduceSourceAssetReferences } from './utils.js';
3
3
  import { linkLwrResources } from './linkers/link-lwr-resources.js';
4
4
  export { LwrViewHandler } from './view-handler.js';
@@ -295,9 +295,13 @@ export class LwrViewRegistry {
295
295
  importer: importer || renderedView.compiledView.filePath,
296
296
  };
297
297
  const stringBuilder = createStringBuilder(renderedViewContent);
298
+ let pageTtl;
298
299
  for (const viewTransformer of this.viewTransformers) {
299
300
  // eslint-disable-next-line no-await-in-loop
300
- await viewTransformer.link?.(stringBuilder, mergedViewContext, linkedMetadata);
301
+ const linkResults = await viewTransformer.link?.(stringBuilder, mergedViewContext, linkedMetadata);
302
+ // Keep track of the shortest TTL from each view transformer (e.g. lwcSsrViewTranformer)
303
+ const ttl = linkResults && linkResults.cache?.ttl;
304
+ pageTtl = shortestTtl(ttl || undefined, pageTtl);
301
305
  }
302
306
  const linkedAssetContent = stringBuilder.toString();
303
307
  // Link LWR related resources
@@ -312,6 +316,7 @@ export class LwrViewRegistry {
312
316
  resourceRegistry,
313
317
  runtimeEnvironment,
314
318
  runtimeParams,
319
+ bundleConfig: this.globalConfig.bundleConfig,
315
320
  });
316
321
  return {
317
322
  // ...viewDefinition,
@@ -321,6 +326,7 @@ export class LwrViewRegistry {
321
326
  assetReferences: reduceSourceAssetReferences(linkedMetadata.assetReferences),
322
327
  ...viewRecord,
323
328
  },
329
+ cache: { ttl: pageTtl },
324
330
  };
325
331
  }
326
332
  return {
@@ -329,6 +335,7 @@ export class LwrViewRegistry {
329
335
  viewRecord: {
330
336
  assetReferences: reduceSourceAssetReferences(linkedMetadata.assetReferences),
331
337
  },
338
+ cache: { ttl: pageTtl },
332
339
  };
333
340
  }
334
341
  }
@@ -1,4 +1,4 @@
1
- import { ModuleRegistry, RenderedViewMetadata, RenderedViewRecord, ResourceRegistry, RuntimeEnvironment, RuntimeParams, View, ViewParams, ModuleBundler } from '@lwrjs/types';
1
+ import { BundleConfig, ModuleRegistry, RenderedViewMetadata, RenderedViewRecord, ResourceRegistry, RuntimeEnvironment, RuntimeParams, View, ViewParams, ModuleBundler } from '@lwrjs/types';
2
2
  export interface LwrResourcesLinkedContext extends ResourceContext {
3
3
  lwrResourcesId: string;
4
4
  }
@@ -9,6 +9,7 @@ interface ResourceContext {
9
9
  moduleRegistry: ModuleRegistry;
10
10
  moduleBundler: ModuleBundler;
11
11
  resourceRegistry: ResourceRegistry;
12
+ bundleConfig: BundleConfig;
12
13
  }
13
14
  export declare function getHtmlResources(view: View, viewParams: ViewParams, resourceContext: ResourceContext): Promise<{
14
15
  partial: string;