@lwrjs/view-registry 0.9.9-alpha.8 → 0.10.0-alpha.0

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
  };
@@ -39,7 +39,19 @@ async function getHtmlResources(view, viewParams, resourceContext) {
39
39
  resourceRegistry,
40
40
  viewMetadata
41
41
  } = resourceContext;
42
- const {id: appName, bootstrap: {services} = {services: []}} = view;
42
+ const {bundleConfig} = resourceContext;
43
+ const {external = {}} = bundleConfig;
44
+ const isExternal = function(rawSpecifier) {
45
+ const {specifier} = (0, import_shared_utils.explodeSpecifier)(rawSpecifier);
46
+ return Object.keys(external).some((e) => specifier === e);
47
+ };
48
+ const {
49
+ id: appName,
50
+ bootstrap: {services, module: bootstrapModule, preloadModules = []} = {
51
+ services: [],
52
+ preloadModules: []
53
+ }
54
+ } = view;
43
55
  const {lwrVersion, format, hmrEnabled, bundle, debug, minify} = runtimeEnvironment;
44
56
  const {customElements} = viewMetadata;
45
57
  const version = lwrVersion;
@@ -49,7 +61,7 @@ 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);
53
65
  const moduleResources = [];
54
66
  const requiredResources = [];
55
67
  const configResources = [];
@@ -57,16 +69,23 @@ async function getHtmlResources(view, viewParams, resourceContext) {
57
69
  const imports = {};
58
70
  const rootComponents = [];
59
71
  const requiredAmdModules = [];
60
- const preloadAmdModules = [];
72
+ const preloadModulesMeta = new Map();
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";
64
- const def = await resourceRegistry.getResource({specifier: shimBundle, version}, runtimeEnvironment, runtimeParams);
75
+ const shimBundle = debug || minify === false ? "lwr-loader-shim-legacy.bundle.js" : "lwr-loader-shim-legacy.bundle.min.js";
76
+ let def = await resourceRegistry.getResource({specifier: shimBundle, version}, runtimeEnvironment, runtimeParams);
65
77
  if (!def) {
66
- throw Error("Failed to find definition of resource: " + shimBundle);
67
- }
68
- if (def.stream) {
69
- def.stream.destroy();
78
+ let fallbackShimBundle;
79
+ if (shimBundle === "lwr-loader-shim-legacy.bundle.js") {
80
+ fallbackShimBundle = "lwr-loader-shim-legacy.bundle.min.js";
81
+ def = await resourceRegistry.getResource({specifier: fallbackShimBundle, version}, runtimeEnvironment, runtimeParams);
82
+ } else {
83
+ fallbackShimBundle = "lwr-loader-shim-legacy.bundle.js";
84
+ def = await resourceRegistry.getResource({specifier: fallbackShimBundle, version}, runtimeEnvironment, runtimeParams);
85
+ }
86
+ if (!def) {
87
+ throw Error("Failed to find definition of resource: " + shimBundle);
88
+ }
70
89
  }
71
90
  requiredResources.push(def);
72
91
  const errorShimDef = await resourceRegistry.getResource({specifier: "lwr-error-shim.js", version}, runtimeEnvironment, runtimeParams);
@@ -74,21 +93,24 @@ async function getHtmlResources(view, viewParams, resourceContext) {
74
93
  throw Error("Failed to find definition of resource: lwr-error-shim.js");
75
94
  }
76
95
  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
96
  }
87
97
  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);
98
+ const defRegistry = bundle ? moduleBundler : moduleRegistry;
99
+ const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
89
100
  const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
90
101
  const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
91
102
  moduleResources.push((0, import_utils.getModuleResourceByUri)(uri, runtimeEnvironment, {isPreload: false, isSSR}));
103
+ for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
104
+ if (!isExternal(depSpecifier)) {
105
+ const uri2 = bootstrapModuleGraph.uriMap[depSpecifier];
106
+ preloadModulesMeta.set(depSpecifier, uri2);
107
+ }
108
+ }
109
+ if ((0, import_shared_utils.isBundler)(defRegistry)) {
110
+ for (const specifier of preloadModules) {
111
+ await (0, import_utils2.getPreloadModulesMeta)(specifier, preloadModulesMeta, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams);
112
+ }
113
+ }
92
114
  if (isAMD) {
93
115
  requiredAmdModules.push(versionedSpecifier);
94
116
  imports[versionedSpecifier] = uri;
@@ -96,7 +118,6 @@ async function getHtmlResources(view, viewParams, resourceContext) {
96
118
  const uri2 = bootstrapModuleGraph.uriMap[staticDep];
97
119
  imports[staticDep] = uri2;
98
120
  if (services && services.length) {
99
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
100
121
  requiredAmdModules.push(staticDep);
101
122
  }
102
123
  }
@@ -112,15 +133,22 @@ async function getHtmlResources(view, viewParams, resourceContext) {
112
133
  const customElementsRecords = [];
113
134
  const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements, isSSR);
114
135
  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);
136
+ 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
137
  customElementsRecords.push({elementName: element, flatGraph: graph});
117
138
  const specifier = graph.graphs[0].specifier;
118
139
  const uri2 = graph.uriMap[specifier];
119
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
140
+ preloadModulesMeta.set(specifier, uri2);
141
+ if (bundle) {
142
+ for (const depSpecifier of graph.graphs[0].static) {
143
+ if (!isExternal(depSpecifier)) {
144
+ const uri3 = graph.uriMap[depSpecifier];
145
+ preloadModulesMeta.set(depSpecifier, uri3);
146
+ }
147
+ }
148
+ }
120
149
  rootComponents.push(specifier);
121
150
  imports[specifier] = uri2;
122
151
  if (isAMD) {
123
- preloadAmdModules.push(specifier);
124
152
  for (const staticDep of graph.graphs[0].static) {
125
153
  const uri3 = graph.uriMap[staticDep];
126
154
  imports[staticDep] = uri3;
@@ -145,11 +173,14 @@ async function getHtmlResources(view, viewParams, resourceContext) {
145
173
  },
146
174
  rootComponents,
147
175
  ...isAMD && {requiredModules: requiredAmdModules},
148
- ...isAMD && {preloadModules: preloadAmdModules}
176
+ ...isAMD && {preloadModules: Array.from(preloadModulesMeta.keys())}
149
177
  }, runtimeEnvironment, runtimeParams));
150
178
  if (!isAMD && hmrEnabled) {
151
179
  configResources.unshift(await (0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
152
180
  }
181
+ for (const [, preloadUri] of preloadModulesMeta) {
182
+ moduleResources.push((0, import_utils.getModuleResourceByUri)(preloadUri, runtimeEnvironment, {isPreload: true, isSSR}));
183
+ }
153
184
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(import_utils.generateHtmlTag));
154
185
  return {
155
186
  partial: htmlResources.join("\n"),
@@ -25,6 +25,7 @@ var __toModule = (module2) => {
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
27
  flattenCustomElements: () => flattenCustomElements,
28
+ getPreloadModulesMeta: () => getPreloadModulesMeta,
28
29
  getViewBootstrapConfigurationResource: () => getViewBootstrapConfigurationResource,
29
30
  getViewHmrConfigurationResource: () => getViewHmrConfigurationResource
30
31
  });
@@ -120,3 +121,41 @@ function flattenCustomElements(arr, isSSR = false) {
120
121
  flatten(arr);
121
122
  return ret;
122
123
  }
124
+ async function getPreloadModulesMeta(specifier, preloadModulesMeta, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, pending) {
125
+ const {exclude = [], external = {}} = bundleConfig;
126
+ const isExternal = function(rawSpecifier) {
127
+ const {specifier: specifier2} = (0, import_shared_utils.explodeSpecifier)(rawSpecifier);
128
+ return Object.keys(external).includes(specifier2);
129
+ };
130
+ const isExclude = function(specifier2) {
131
+ return exclude.includes(specifier2);
132
+ };
133
+ if (isExternal(specifier)) {
134
+ import_shared_utils.logger.warn(`"${specifier}" is configured in both bundleConfig.externals and bootstrap.preloadModules. We are treating it as external.`);
135
+ } else {
136
+ const versionedModuleId = await (0, import_shared_utils.getVersionedModuleId)(specifier, moduleRegistry);
137
+ const versionedModuleSpecifier = (0, import_shared_utils.getSpecifier)({
138
+ specifier,
139
+ version: (0, import_shared_utils.normalizeVersionToUri)(versionedModuleId.version)
140
+ });
141
+ const uri = await defRegistry.resolveModuleUri(versionedModuleId, runtimeEnvironment, runtimeParams);
142
+ const normalizedSpecifier = versionedModuleId.version === import_shared_utils.VERSION_NOT_PROVIDED ? specifier : versionedModuleSpecifier;
143
+ preloadModulesMeta.set(normalizedSpecifier, uri);
144
+ if (exclude.length) {
145
+ const preloadModuleRecord = await defRegistry.getModuleBundle(versionedModuleId, runtimeEnvironment, runtimeParams);
146
+ const {imports} = preloadModuleRecord.bundleRecord;
147
+ if (imports) {
148
+ if (!pending) {
149
+ pending = new Map();
150
+ }
151
+ for (let i = 0; i < imports.length; i++) {
152
+ const imp = imports[i];
153
+ if (!pending.has(imp.specifier) && isExclude(imp.specifier)) {
154
+ pending.set(imp.specifier, true);
155
+ await getPreloadModulesMeta(imp.specifier, preloadModulesMeta, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, pending);
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+ }
@@ -39,9 +39,22 @@ async function getHtmlResources(view, viewParams, resourceContext) {
39
39
  resourceRegistry,
40
40
  viewMetadata
41
41
  } = resourceContext;
42
- const {id: appName, bootstrap: {services} = {services: []}} = view;
42
+ const {bundleConfig} = resourceContext;
43
+ const {external = {}} = bundleConfig;
44
+ const isExternal = function(rawSpecifier) {
45
+ const {specifier} = (0, import_shared_utils.explodeSpecifier)(rawSpecifier);
46
+ return Object.keys(external).some((e) => specifier === e);
47
+ };
48
+ const {
49
+ id: appName,
50
+ bootstrap: {services, module: bootstrapModule, preloadModules = []} = {
51
+ services: [],
52
+ preloadModules: []
53
+ }
54
+ } = view;
43
55
  const {lwrVersion, format, hmrEnabled, bundle, debug, minify} = runtimeEnvironment;
44
56
  const {customElements} = viewMetadata;
57
+ const defRegistry = bundle ? moduleBundler : moduleRegistry;
45
58
  const version = lwrVersion;
46
59
  const isAMD = format === "amd";
47
60
  const appIdentity = {
@@ -49,7 +62,7 @@ async function getHtmlResources(view, viewParams, resourceContext) {
49
62
  format: runtimeEnvironment.format,
50
63
  resourceType: import_identity.AppResourceEnum.MODULE
51
64
  };
52
- const bootstrapSpecifier = (0, import_identity.getAppSpecifier)(appIdentity);
65
+ const bootstrapSpecifier = bootstrapModule || (0, import_identity.getAppSpecifier)(appIdentity);
53
66
  const moduleResources = [];
54
67
  const requiredResources = [];
55
68
  const configResources = [];
@@ -57,37 +70,37 @@ async function getHtmlResources(view, viewParams, resourceContext) {
57
70
  const imports = {};
58
71
  const rootComponents = [];
59
72
  const requiredAmdModules = [];
60
- const preloadAmdModules = [];
73
+ const preloadModulesMeta = new Map();
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
+ const uri2 = bootstrapModuleGraph.uriMap[depSpecifier];
96
+ preloadModulesMeta.set(depSpecifier, uri2);
97
+ }
98
+ }
99
+ if ((0, import_shared_utils.isBundler)(defRegistry)) {
100
+ for (const specifier of preloadModules) {
101
+ await (0, import_utils2.getPreloadModulesMeta)(specifier, preloadModulesMeta, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams);
102
+ }
103
+ }
91
104
  if (isAMD) {
92
105
  requiredAmdModules.push(versionedSpecifier);
93
106
  imports[versionedSpecifier] = uri;
@@ -95,7 +108,6 @@ async function getHtmlResources(view, viewParams, resourceContext) {
95
108
  const uri2 = bootstrapModuleGraph.uriMap[staticDep];
96
109
  imports[staticDep] = uri2;
97
110
  if (services && services.length) {
98
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
99
111
  requiredAmdModules.push(staticDep);
100
112
  }
101
113
  }
@@ -112,15 +124,22 @@ async function getHtmlResources(view, viewParams, resourceContext) {
112
124
  const customElementsRecords = [];
113
125
  const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements, isSSR);
114
126
  for (const {tagName: element} of flattenedElements) {
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);
127
+ 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
128
  customElementsRecords.push({elementName: element, flatGraph: graph});
117
129
  const specifier = graph.graphs[0].specifier;
118
130
  const uri2 = graph.uriMap[specifier];
119
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri2, runtimeEnvironment, {isPreload: true, isSSR}));
131
+ preloadModulesMeta.set(specifier, uri2);
132
+ if (bundle) {
133
+ for (const depSpecifier of graph.graphs[0].static) {
134
+ if (!isExternal(depSpecifier)) {
135
+ const uri3 = graph.uriMap[depSpecifier];
136
+ preloadModulesMeta.set(depSpecifier, uri3);
137
+ }
138
+ }
139
+ }
120
140
  rootComponents.push(specifier);
121
141
  imports[specifier] = uri2;
122
142
  if (isAMD) {
123
- preloadAmdModules.push(specifier);
124
143
  for (const staticDep of graph.graphs[0].static) {
125
144
  const uri3 = graph.uriMap[staticDep];
126
145
  imports[staticDep] = uri3;
@@ -144,11 +163,14 @@ async function getHtmlResources(view, viewParams, resourceContext) {
144
163
  index: importMetadata?.index,
145
164
  rootComponents,
146
165
  ...isAMD && {requiredModules: requiredAmdModules},
147
- ...isAMD && {preloadModules: preloadAmdModules}
166
+ ...isAMD && {preloadModules: Array.from(preloadModulesMeta.keys())}
148
167
  }, runtimeEnvironment, runtimeParams));
149
168
  if (!isAMD && hmrEnabled) {
150
169
  configResources.unshift(await (0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
151
170
  }
171
+ for (const [, preloadUri] of preloadModulesMeta) {
172
+ moduleResources.push((0, import_utils.getModuleResourceByUri)(preloadUri, runtimeEnvironment, {isPreload: true, isSSR}));
173
+ }
152
174
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(import_utils.generateHtmlTag));
153
175
  const mapping = (0, import_shared_utils.getMappingUriPrefix)(runtimeEnvironment, runtimeParams);
154
176
  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;