@lwrjs/core 0.11.0-alpha.13 → 0.11.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.
- package/build/cjs/middleware/asset-middleware.cjs +11 -4
- package/build/cjs/middleware/bundle-middleware.cjs +12 -10
- package/build/cjs/middleware/locale-middleware.cjs +3 -1
- package/build/cjs/middleware/mapping-middleware.cjs +7 -3
- package/build/cjs/middleware/module-middleware.cjs +11 -8
- package/build/cjs/middleware/resource-middleware.cjs +2 -7
- package/build/cjs/middleware/view-middleware.cjs +35 -7
- package/build/cjs/tools/server-warmup.cjs +2 -2
- package/build/cjs/tools/static-generation.cjs +10 -7
- package/build/es/middleware/asset-middleware.js +12 -4
- package/build/es/middleware/bundle-middleware.js +15 -7
- package/build/es/middleware/locale-middleware.js +1 -1
- package/build/es/middleware/mapping-middleware.js +8 -3
- package/build/es/middleware/module-middleware.js +13 -5
- package/build/es/middleware/resource-middleware.js +2 -2
- package/build/es/middleware/view-middleware.js +46 -7
- package/build/es/tools/server-warmup.js +2 -2
- package/build/es/tools/static-generation.d.ts +2 -2
- package/build/es/tools/static-generation.js +12 -8
- package/package.json +30 -30
|
@@ -27,17 +27,17 @@ __export(exports, {
|
|
|
27
27
|
assetMiddleware: () => assetMiddleware
|
|
28
28
|
});
|
|
29
29
|
var import_path = __toModule(require("path"));
|
|
30
|
+
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
31
|
+
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
30
32
|
var import_identity = __toModule(require("./utils/identity.cjs"));
|
|
31
33
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
32
|
-
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
33
34
|
function createAssetMiddleware(context) {
|
|
34
35
|
const {
|
|
35
|
-
appConfig: {i18n},
|
|
36
36
|
assetRegistry,
|
|
37
37
|
runtimeEnvironment: {basePath}
|
|
38
38
|
} = context;
|
|
39
39
|
return async (req, res, next) => {
|
|
40
|
-
const {runtimeEnvironment} = req.getRuntimeContext(context.runtimeEnvironment
|
|
40
|
+
const {runtimeEnvironment} = req.getRuntimeContext(context.runtimeEnvironment);
|
|
41
41
|
const {assetId, immutable, signature} = (0, import_identity.getAssetIdentity)(req);
|
|
42
42
|
if (basePath && !assetId.specifier.startsWith(basePath)) {
|
|
43
43
|
assetId.specifier = import_path.default.join(basePath, assetId.specifier);
|
|
@@ -52,7 +52,14 @@ function createAssetMiddleware(context) {
|
|
|
52
52
|
res.sendStatus(302);
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
|
-
const asset = await
|
|
55
|
+
const asset = await (0, import_instrumentation.getTracer)().trace({
|
|
56
|
+
name: import_instrumentation.RequestHandlerSpan.GetAsset,
|
|
57
|
+
attributes: {
|
|
58
|
+
specifier: assetId.specifier
|
|
59
|
+
}
|
|
60
|
+
}, () => {
|
|
61
|
+
return assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
|
|
62
|
+
});
|
|
56
63
|
if (req.isSiteGeneration()) {
|
|
57
64
|
res.setSiteGenerationMetadata({asset});
|
|
58
65
|
}
|
|
@@ -28,6 +28,7 @@ __export(exports, {
|
|
|
28
28
|
});
|
|
29
29
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
30
30
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
31
|
+
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
31
32
|
var import_request = __toModule(require("./utils/request.cjs"));
|
|
32
33
|
var import_identity = __toModule(require("./utils/identity.cjs"));
|
|
33
34
|
var import_metadata = __toModule(require("./utils/metadata.cjs"));
|
|
@@ -36,7 +37,6 @@ var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
|
36
37
|
function createBundleMiddleware(context) {
|
|
37
38
|
const {
|
|
38
39
|
appConfig,
|
|
39
|
-
appConfig: {i18n},
|
|
40
40
|
moduleRegistry,
|
|
41
41
|
moduleBundler,
|
|
42
42
|
runtimeEnvironment: defaultRuntimeEnvironment
|
|
@@ -53,7 +53,7 @@ function createBundleMiddleware(context) {
|
|
|
53
53
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
|
-
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
56
|
+
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
57
57
|
const importer = req.query.importer ? await (0, import_request.getRequestImporter)(req, moduleRegistry, runtimeParams) : void 0;
|
|
58
58
|
const {moduleId, signature} = (0, import_identity.getModuleIdentity)(req, importer);
|
|
59
59
|
if (moduleId.importer || !signature) {
|
|
@@ -61,7 +61,14 @@ function createBundleMiddleware(context) {
|
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
const sourceMapUrl = req.path.replace("/bundle/", "/sourcemaps/bundle/");
|
|
64
|
-
const bundleDefinition = await
|
|
64
|
+
const bundleDefinition = await (0, import_instrumentation.getTracer)().trace({
|
|
65
|
+
name: import_instrumentation.RequestHandlerSpan.GetBundle,
|
|
66
|
+
attributes: {
|
|
67
|
+
specifier: moduleId.specifier
|
|
68
|
+
}
|
|
69
|
+
}, () => {
|
|
70
|
+
return moduleBundler.getModuleBundle(moduleId, {...runtimeEnvironment, bundle: true, sourceMapUrl}, runtimeParams);
|
|
71
|
+
});
|
|
65
72
|
if (req.isSiteGeneration()) {
|
|
66
73
|
res.setSiteGenerationMetadata({
|
|
67
74
|
moduleDefinition: bundleDefinition,
|
|
@@ -75,19 +82,14 @@ function createBundleMiddleware(context) {
|
|
|
75
82
|
};
|
|
76
83
|
}
|
|
77
84
|
function createSourceMapMiddleware(context) {
|
|
78
|
-
const {
|
|
79
|
-
appConfig,
|
|
80
|
-
appConfig: {i18n},
|
|
81
|
-
moduleBundler,
|
|
82
|
-
runtimeEnvironment: defaultRuntimeEnvironment
|
|
83
|
-
} = context;
|
|
85
|
+
const {appConfig, moduleBundler, runtimeEnvironment: defaultRuntimeEnvironment} = context;
|
|
84
86
|
return async (req, res) => {
|
|
85
87
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
86
88
|
res.status(400);
|
|
87
89
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
88
90
|
return;
|
|
89
91
|
}
|
|
90
|
-
const {runtimeEnvironment} = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
92
|
+
const {runtimeEnvironment} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
91
93
|
const {moduleId, signature} = (0, import_identity.getModuleIdentity)(req);
|
|
92
94
|
const bundleDef = await moduleBundler.getModuleBundle(moduleId, runtimeEnvironment);
|
|
93
95
|
if (signature !== import_shared_utils.LATEST_SIGNATURE) {
|
|
@@ -11,7 +11,9 @@ __export(exports, {
|
|
|
11
11
|
localeMiddleware: () => localeMiddleware
|
|
12
12
|
});
|
|
13
13
|
function localeMiddleware(app, context) {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
i18n: {defaultLocale}
|
|
16
|
+
} = context.runtimeEnvironment;
|
|
15
17
|
app.use(async (req, _res, next) => {
|
|
16
18
|
const langs = req.acceptsLanguages();
|
|
17
19
|
req.locale = langs?.length > 0 && langs[0] != "*" ? langs[0] : defaultLocale;
|
|
@@ -28,12 +28,12 @@ __export(exports, {
|
|
|
28
28
|
});
|
|
29
29
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
30
30
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
31
|
+
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
31
32
|
var import_identity = __toModule(require("./utils/identity.cjs"));
|
|
32
33
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
33
34
|
function createMappingMiddleware(context) {
|
|
34
35
|
const {
|
|
35
36
|
appConfig,
|
|
36
|
-
appConfig: {i18n},
|
|
37
37
|
moduleBundler,
|
|
38
38
|
moduleRegistry,
|
|
39
39
|
runtimeEnvironment: defaultRuntimeEnvironment
|
|
@@ -44,9 +44,13 @@ function createMappingMiddleware(context) {
|
|
|
44
44
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
|
-
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
47
|
+
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
48
48
|
const {moduleIds} = (0, import_identity.getMappingIdentity)(req);
|
|
49
|
-
const importMetadata = await (0,
|
|
49
|
+
const importMetadata = await (0, import_instrumentation.getTracer)().trace({
|
|
50
|
+
name: import_instrumentation.RequestHandlerSpan.GetMapping
|
|
51
|
+
}, () => {
|
|
52
|
+
return (0, import_shared_utils.getImportMetadataMappings)(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler);
|
|
53
|
+
});
|
|
50
54
|
if (req.isSiteGeneration()) {
|
|
51
55
|
res.setSiteGenerationMetadata({importMetadata});
|
|
52
56
|
}
|
|
@@ -28,17 +28,13 @@ __export(exports, {
|
|
|
28
28
|
});
|
|
29
29
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
30
30
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
31
|
+
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
31
32
|
var import_request = __toModule(require("./utils/request.cjs"));
|
|
32
33
|
var import_identity = __toModule(require("./utils/identity.cjs"));
|
|
33
34
|
var import_unsigned_module_redirect = __toModule(require("./redirects/unsigned-module-redirect.cjs"));
|
|
34
35
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
35
36
|
function createModuleMiddleware(context) {
|
|
36
|
-
const {
|
|
37
|
-
appConfig,
|
|
38
|
-
appConfig: {i18n},
|
|
39
|
-
moduleRegistry,
|
|
40
|
-
runtimeEnvironment: defaultRuntimeEnvironment
|
|
41
|
-
} = context;
|
|
37
|
+
const {appConfig, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment} = context;
|
|
42
38
|
const unsignedRedirect = (0, import_unsigned_module_redirect.createUnsignedModuleRedirect)(moduleRegistry);
|
|
43
39
|
return async (req, res) => {
|
|
44
40
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
@@ -51,14 +47,21 @@ function createModuleMiddleware(context) {
|
|
|
51
47
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
52
48
|
return;
|
|
53
49
|
}
|
|
54
|
-
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
50
|
+
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
55
51
|
const importer = req.query.importer ? await (0, import_request.getRequestImporter)(req, moduleRegistry, runtimeParams) : void 0;
|
|
56
52
|
const {moduleId, signature} = (0, import_identity.getModuleIdentity)(req, importer);
|
|
57
53
|
if (moduleId.importer || !signature) {
|
|
58
54
|
await unsignedRedirect(req, res, moduleId, runtimeEnvironment, runtimeParams);
|
|
59
55
|
return;
|
|
60
56
|
}
|
|
61
|
-
const moduleDefinition = await
|
|
57
|
+
const moduleDefinition = await (0, import_instrumentation.getTracer)().trace({
|
|
58
|
+
name: import_instrumentation.RequestHandlerSpan.GetModule,
|
|
59
|
+
attributes: {
|
|
60
|
+
specifier: moduleId.specifier
|
|
61
|
+
}
|
|
62
|
+
}, () => {
|
|
63
|
+
return moduleRegistry.getLinkedModule(moduleId, {...runtimeEnvironment, bundle: false}, runtimeParams);
|
|
64
|
+
});
|
|
62
65
|
const {ownHash, linkedSource} = moduleDefinition;
|
|
63
66
|
if (signature !== import_shared_utils.LATEST_SIGNATURE && ownHash !== signature) {
|
|
64
67
|
res.status(404).send(import_diagnostics.descriptions.UNRESOLVABLE.SIGNED_MODULE(moduleId.specifier, signature).message);
|
|
@@ -30,18 +30,13 @@ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
|
30
30
|
var import_identity = __toModule(require("./utils/identity.cjs"));
|
|
31
31
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
32
32
|
function createResourceMiddleware(context) {
|
|
33
|
-
const {
|
|
34
|
-
appConfig,
|
|
35
|
-
appConfig: {i18n},
|
|
36
|
-
resourceRegistry,
|
|
37
|
-
runtimeEnvironment: defaultRuntimeEnvironment
|
|
38
|
-
} = context;
|
|
33
|
+
const {appConfig, resourceRegistry, runtimeEnvironment: defaultRuntimeEnvironment} = context;
|
|
39
34
|
return async (req, res) => {
|
|
40
35
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
41
36
|
res.status(400).send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
42
37
|
return;
|
|
43
38
|
}
|
|
44
|
-
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
39
|
+
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
45
40
|
const {resourceId} = (0, import_identity.getResourceIdentity)(req);
|
|
46
41
|
const resource = await resourceRegistry.getResource(resourceId, runtimeEnvironment, runtimeParams);
|
|
47
42
|
if (req.isSiteGeneration()) {
|
|
@@ -29,12 +29,13 @@ __export(exports, {
|
|
|
29
29
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
30
30
|
var import_router = __toModule(require("@lwrjs/router"));
|
|
31
31
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
32
|
+
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
32
33
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
33
34
|
var import_view_registry = __toModule(require("@lwrjs/view-registry"));
|
|
34
35
|
function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
35
36
|
const errorRoute = errorRoutes.find((route2) => route2.status === 500);
|
|
36
37
|
const appConfig = context.appConfig;
|
|
37
|
-
const {environment: environmentConfig
|
|
38
|
+
const {environment: environmentConfig} = appConfig;
|
|
38
39
|
return async (req, res) => {
|
|
39
40
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
40
41
|
res.status(400);
|
|
@@ -46,7 +47,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
46
47
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
47
48
|
return;
|
|
48
49
|
}
|
|
49
|
-
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(context.runtimeEnvironment
|
|
50
|
+
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(context.runtimeEnvironment);
|
|
50
51
|
if (!runtimeParams.environment && environmentConfig?.default) {
|
|
51
52
|
runtimeParams.environment = environmentConfig.default;
|
|
52
53
|
}
|
|
@@ -60,7 +61,14 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
60
61
|
let viewResponse;
|
|
61
62
|
let resolvedRoute;
|
|
62
63
|
try {
|
|
63
|
-
viewResponse = await
|
|
64
|
+
viewResponse = await (0, import_instrumentation.getTracer)().trace({
|
|
65
|
+
name: import_instrumentation.RequestHandlerSpan.GetView,
|
|
66
|
+
attributes: {
|
|
67
|
+
view: route.id
|
|
68
|
+
}
|
|
69
|
+
}, () => {
|
|
70
|
+
return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
71
|
+
});
|
|
64
72
|
resolvedRoute = route;
|
|
65
73
|
} catch (err) {
|
|
66
74
|
if (!errorRoute) {
|
|
@@ -68,7 +76,14 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
68
76
|
}
|
|
69
77
|
import_diagnostics.logger.error(`[view-middleware] Route Error ${req.originalUrl}`);
|
|
70
78
|
import_diagnostics.logger.error(err);
|
|
71
|
-
viewResponse = await
|
|
79
|
+
viewResponse = await (0, import_instrumentation.getTracer)().trace({
|
|
80
|
+
name: import_instrumentation.RequestHandlerSpan.GetView,
|
|
81
|
+
attributes: {
|
|
82
|
+
view: errorRoute.id
|
|
83
|
+
}
|
|
84
|
+
}, () => {
|
|
85
|
+
return resolve.call(viewHandler, viewRequest, errorRoute, runtimeEnvironment, runtimeParams);
|
|
86
|
+
});
|
|
72
87
|
resolvedRoute = errorRoute;
|
|
73
88
|
}
|
|
74
89
|
if (req.isSiteGeneration()) {
|
|
@@ -89,7 +104,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
89
104
|
}
|
|
90
105
|
function createConfigMiddleware(routes, context, viewHandler) {
|
|
91
106
|
return async (req, res) => {
|
|
92
|
-
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(context.runtimeEnvironment
|
|
107
|
+
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(context.runtimeEnvironment);
|
|
93
108
|
const {appId, encodedViewPath} = req.params;
|
|
94
109
|
const route = routes.find((route2) => route2.id === appId);
|
|
95
110
|
if (!route) {
|
|
@@ -143,15 +158,28 @@ function viewMiddleware(app, context) {
|
|
|
143
158
|
subRoutes.routes.forEach((subRoute) => subRoute.uri !== route.path && paths.push(`${prefix}${subRoute.uri}`));
|
|
144
159
|
}
|
|
145
160
|
if (i18n.uriPattern === "path-prefix") {
|
|
146
|
-
const
|
|
161
|
+
const defaultLocalePaths = paths.map((routePath) => `/${i18n.defaultLocale}${routePath}`);
|
|
162
|
+
const supportedStr = i18n.locales.map((l) => l.id).filter((id) => id !== i18n.defaultLocale).join("|");
|
|
147
163
|
paths.forEach((routePath) => {
|
|
148
164
|
const localizedPath = `/:locale(${supportedStr})${routePath}`;
|
|
149
|
-
import_diagnostics.logger.debug(`[view-middleware] Add localized path ${localizedPath}`);
|
|
150
165
|
paths.push(localizedPath);
|
|
151
166
|
});
|
|
167
|
+
addDefaultLocaleRedirects(i18n.defaultLocale, defaultLocalePaths, app);
|
|
152
168
|
}
|
|
169
|
+
import_diagnostics.logger.debug(`[view-middleware] Add view paths ${paths}`);
|
|
153
170
|
app.get(paths, (0, import_error_handling.handleErrors)(createViewMiddleware(route, errorRoutes, context, viewHandler)));
|
|
154
171
|
}
|
|
155
172
|
app.get((0, import_shared_utils.getClientBootstrapConfigurationRoutes)(), (0, import_error_handling.handleErrors)(createConfigMiddleware(routes, context, viewHandler)));
|
|
156
173
|
app.get("/" + app.getRegexWildcard(), (0, import_error_handling.handleErrors)(createNotFoundMiddleware(errorRoutes, context, viewHandler)));
|
|
157
174
|
}
|
|
175
|
+
function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
|
|
176
|
+
import_diagnostics.logger.debug(`[view-middleware] Add default localized paths ${defaultLocalePaths}`);
|
|
177
|
+
app.get(defaultLocalePaths, (req, res) => {
|
|
178
|
+
let modifiedPath = req.originalUrl.replace(`/${defaultLocale}`, "");
|
|
179
|
+
if (modifiedPath.indexOf("/") !== 0) {
|
|
180
|
+
modifiedPath = `/${modifiedPath}`;
|
|
181
|
+
}
|
|
182
|
+
res.setHeader("Location", modifiedPath);
|
|
183
|
+
return res.sendStatus(301);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
@@ -36,10 +36,10 @@ async function warmupServer(config, internalRequestKey) {
|
|
|
36
36
|
import_diagnostics.logger.setOptions({dedupe: new Set([import_diagnostics.WARN])});
|
|
37
37
|
}
|
|
38
38
|
import_diagnostics.logger.info("[Server Warmup] starting");
|
|
39
|
-
const {basePath,
|
|
39
|
+
const {basePath, port, routes, staticSiteGenerator} = config;
|
|
40
40
|
staticSiteGenerator.outputDir = import_dir.skipDirCreation;
|
|
41
41
|
const urlRewriteMap = new Map();
|
|
42
42
|
const runtimeEnvironment = (0, import_config.getRuntimeEnvironment)(config);
|
|
43
|
-
await new import_static_generation.default().generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, new import_network_dispatcher.default(port, internalRequestKey), staticSiteGenerator.outputDir,
|
|
43
|
+
await new import_static_generation.default().generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, new import_network_dispatcher.default(port, internalRequestKey), staticSiteGenerator.outputDir, urlRewriteMap);
|
|
44
44
|
import_diagnostics.logger.info("[Server Warmup] complete");
|
|
45
45
|
}
|
|
@@ -43,7 +43,7 @@ var SiteGenerator = class {
|
|
|
43
43
|
if (!import_diagnostics.logger.currentLevel || import_diagnostics.logger.currentLevel == import_diagnostics.WARN || import_diagnostics.logger.currentLevel == import_diagnostics.INFO) {
|
|
44
44
|
import_diagnostics.logger.setOptions({dedupe: new Set([import_diagnostics.WARN])});
|
|
45
45
|
}
|
|
46
|
-
const {assets, basePath,
|
|
46
|
+
const {assets, basePath, rootDir, routes, staticSiteGenerator} = config;
|
|
47
47
|
if (!staticSiteGenerator.outputDir) {
|
|
48
48
|
staticSiteGenerator.outputDir = "site";
|
|
49
49
|
}
|
|
@@ -57,26 +57,29 @@ var SiteGenerator = class {
|
|
|
57
57
|
const urlRewriteMap = new Map();
|
|
58
58
|
const runtimeEnvironment = (0, import_config.getRuntimeEnvironment)(config);
|
|
59
59
|
import_diagnostics.logger.info(`[SSG] Building routes (this may take some time to complete)`);
|
|
60
|
-
await this.generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir,
|
|
60
|
+
await this.generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, urlRewriteMap);
|
|
61
61
|
this.writeNetlifyRedirectConfig(outputDir, urlRewriteMap);
|
|
62
62
|
await this.copyAssets(assets, outputDir, config);
|
|
63
63
|
const endTime = import_perf_hooks.performance.now();
|
|
64
64
|
const timeDiff = (endTime - startTime) / 1e3;
|
|
65
65
|
import_diagnostics.logger.info(`[SSG] Static Site Generation complete in ${Math.round(timeDiff)} seconds`);
|
|
66
66
|
}
|
|
67
|
-
async generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir,
|
|
67
|
+
async generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, urlRewriteMap = new Map()) {
|
|
68
68
|
const generateUrl = this.createGenerateURLFunction(dispatcher);
|
|
69
69
|
const {skipBaseDocumentGeneration = false} = staticSiteGenerator;
|
|
70
|
+
const i18n = runtimeEnvironment.i18n;
|
|
70
71
|
if (!i18n.uriPattern) {
|
|
71
|
-
await this.generateRoutesForLocale(routes, outputDir, {id: i18n.defaultLocale}, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment,
|
|
72
|
+
await this.generateRoutesForLocale(routes, outputDir, {id: i18n.defaultLocale}, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, basePath, generateUrl, staticSiteGenerator, dispatcher);
|
|
72
73
|
} else {
|
|
73
|
-
|
|
74
|
+
const sortedLocales = (0, import_shared_utils.sortLocalesByFallback)(i18n);
|
|
75
|
+
for (const locale of sortedLocales) {
|
|
74
76
|
import_diagnostics.logger.debug(`[SSG] Generate routes of locale: ${locale.id}`);
|
|
75
|
-
await this.generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment,
|
|
77
|
+
await this.generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, basePath, generateUrl, staticSiteGenerator, dispatcher);
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
80
|
}
|
|
79
|
-
async generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment,
|
|
81
|
+
async generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, basePath, generateUrl, staticSiteGenerator, dispatcher) {
|
|
82
|
+
const i18n = runtimeEnvironment.i18n;
|
|
80
83
|
for (const route of routes) {
|
|
81
84
|
const siteConfig = this.createSiteConfig(outputDir, locale.id, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n);
|
|
82
85
|
const localizedPath = (0, import_shared_utils.getViewUri)(route.path, basePath, locale.id, i18n);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
+
import { DiagnosticsError } from '@lwrjs/diagnostics';
|
|
3
|
+
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
2
4
|
import { getAssetIdentity } from './utils/identity.js';
|
|
3
5
|
import { handleErrors } from './utils/error-handling.js';
|
|
4
|
-
import { DiagnosticsError } from '@lwrjs/diagnostics';
|
|
5
6
|
function createAssetMiddleware(context) {
|
|
6
|
-
const {
|
|
7
|
+
const { assetRegistry, runtimeEnvironment: { basePath }, } = context;
|
|
7
8
|
return async (req, res, next) => {
|
|
8
|
-
const { runtimeEnvironment } = req.getRuntimeContext(context.runtimeEnvironment
|
|
9
|
+
const { runtimeEnvironment } = req.getRuntimeContext(context.runtimeEnvironment);
|
|
9
10
|
const { assetId, immutable, signature } = getAssetIdentity(req);
|
|
10
11
|
if (basePath && !assetId.specifier.startsWith(basePath)) {
|
|
11
12
|
assetId.specifier = path.join(basePath, assetId.specifier);
|
|
@@ -21,7 +22,14 @@ function createAssetMiddleware(context) {
|
|
|
21
22
|
res.sendStatus(302);
|
|
22
23
|
return;
|
|
23
24
|
}
|
|
24
|
-
const asset = await
|
|
25
|
+
const asset = await getTracer().trace({
|
|
26
|
+
name: RequestHandlerSpan.GetAsset,
|
|
27
|
+
attributes: {
|
|
28
|
+
specifier: assetId.specifier,
|
|
29
|
+
},
|
|
30
|
+
}, () => {
|
|
31
|
+
return assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
|
|
32
|
+
});
|
|
25
33
|
if (req.isSiteGeneration()) {
|
|
26
34
|
res.setSiteGenerationMetadata({ asset });
|
|
27
35
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { LATEST_SIGNATURE } from '@lwrjs/shared-utils';
|
|
2
2
|
import { descriptions } from '@lwrjs/diagnostics';
|
|
3
|
+
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
3
4
|
import { getRequestImporter } from './utils/request.js';
|
|
4
5
|
import { getModuleIdentity } from './utils/identity.js';
|
|
5
6
|
import { normalizeResolvedUris } from './utils/metadata.js';
|
|
6
7
|
import { createUnsignedBundleRedirect } from './redirects/unsigned-module-redirect.js';
|
|
7
8
|
import { handleErrors } from './utils/error-handling.js';
|
|
8
9
|
function createBundleMiddleware(context) {
|
|
9
|
-
const { appConfig,
|
|
10
|
+
const { appConfig, moduleRegistry, moduleBundler, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
10
11
|
const unsignedBundleRedirect = createUnsignedBundleRedirect(moduleBundler);
|
|
11
12
|
return async (req, res) => {
|
|
12
13
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
@@ -19,7 +20,7 @@ function createBundleMiddleware(context) {
|
|
|
19
20
|
res.send(descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
20
21
|
return;
|
|
21
22
|
}
|
|
22
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
23
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
23
24
|
const importer = req.query.importer
|
|
24
25
|
? await getRequestImporter(req, moduleRegistry, runtimeParams)
|
|
25
26
|
: undefined;
|
|
@@ -29,9 +30,16 @@ function createBundleMiddleware(context) {
|
|
|
29
30
|
return;
|
|
30
31
|
}
|
|
31
32
|
const sourceMapUrl = req.path.replace('/bundle/', '/sourcemaps/bundle/');
|
|
32
|
-
const bundleDefinition = await
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const bundleDefinition = await getTracer().trace({
|
|
34
|
+
name: RequestHandlerSpan.GetBundle,
|
|
35
|
+
attributes: {
|
|
36
|
+
specifier: moduleId.specifier,
|
|
37
|
+
},
|
|
38
|
+
}, () => {
|
|
39
|
+
return moduleBundler.getModuleBundle(moduleId,
|
|
40
|
+
// bundle must be `true` to properly resolve bundles in amd
|
|
41
|
+
{ ...runtimeEnvironment, bundle: true, sourceMapUrl }, runtimeParams);
|
|
42
|
+
});
|
|
35
43
|
if (req.isSiteGeneration()) {
|
|
36
44
|
res.setSiteGenerationMetadata({
|
|
37
45
|
moduleDefinition: bundleDefinition,
|
|
@@ -45,14 +53,14 @@ function createBundleMiddleware(context) {
|
|
|
45
53
|
};
|
|
46
54
|
}
|
|
47
55
|
function createSourceMapMiddleware(context) {
|
|
48
|
-
const { appConfig,
|
|
56
|
+
const { appConfig, moduleBundler, runtimeEnvironment: defaultRuntimeEnvironment } = context;
|
|
49
57
|
return async (req, res) => {
|
|
50
58
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
51
59
|
res.status(400);
|
|
52
60
|
res.send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
53
61
|
return;
|
|
54
62
|
}
|
|
55
|
-
const { runtimeEnvironment } = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
63
|
+
const { runtimeEnvironment } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
56
64
|
const { moduleId, signature } = getModuleIdentity(req);
|
|
57
65
|
const bundleDef = await moduleBundler.getModuleBundle(moduleId, runtimeEnvironment);
|
|
58
66
|
if (signature !== LATEST_SIGNATURE) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function localeMiddleware(app, context) {
|
|
2
|
-
const { defaultLocale } = context.runtimeEnvironment;
|
|
2
|
+
const { i18n: { defaultLocale }, } = context.runtimeEnvironment;
|
|
3
3
|
app.use(async (req, _res, next) => {
|
|
4
4
|
const langs = req.acceptsLanguages();
|
|
5
5
|
req.locale = langs?.length > 0 && langs[0] != '*' ? langs[0] : defaultLocale;
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import { getImportMetadataMappings } from '@lwrjs/shared-utils';
|
|
2
2
|
import { descriptions } from '@lwrjs/diagnostics';
|
|
3
|
+
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
3
4
|
import { getMappingIdentity } from './utils/identity.js';
|
|
4
5
|
import { handleErrors } from './utils/error-handling.js';
|
|
5
6
|
function createMappingMiddleware(context) {
|
|
6
|
-
const { appConfig,
|
|
7
|
+
const { appConfig, moduleBundler, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
7
8
|
return async (req, res) => {
|
|
8
9
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
9
10
|
res.status(400);
|
|
10
11
|
res.send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
11
12
|
return;
|
|
12
13
|
}
|
|
13
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
14
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
14
15
|
const { moduleIds } = getMappingIdentity(req);
|
|
15
|
-
const importMetadata = await
|
|
16
|
+
const importMetadata = await getTracer().trace({
|
|
17
|
+
name: RequestHandlerSpan.GetMapping,
|
|
18
|
+
}, () => {
|
|
19
|
+
return getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler);
|
|
20
|
+
});
|
|
16
21
|
if (req.isSiteGeneration()) {
|
|
17
22
|
res.setSiteGenerationMetadata({ importMetadata });
|
|
18
23
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { descriptions } from '@lwrjs/diagnostics';
|
|
2
2
|
import { LATEST_SIGNATURE, serializeModuleToJson } from '@lwrjs/shared-utils';
|
|
3
|
+
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
3
4
|
import { getRequestImporter } from './utils/request.js';
|
|
4
5
|
import { getModuleIdentity } from './utils/identity.js';
|
|
5
6
|
import { createUnsignedModuleRedirect } from './redirects/unsigned-module-redirect.js';
|
|
6
7
|
import { handleErrors } from './utils/error-handling.js';
|
|
7
8
|
function createModuleMiddleware(context) {
|
|
8
|
-
const { appConfig,
|
|
9
|
+
const { appConfig, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment } = context;
|
|
9
10
|
const unsignedRedirect = createUnsignedModuleRedirect(moduleRegistry);
|
|
10
11
|
return async (req, res) => {
|
|
11
12
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
@@ -18,7 +19,7 @@ function createModuleMiddleware(context) {
|
|
|
18
19
|
res.send(descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
19
20
|
return;
|
|
20
21
|
}
|
|
21
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
22
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
22
23
|
const importer = req.query.importer
|
|
23
24
|
? await getRequestImporter(req, moduleRegistry, runtimeParams)
|
|
24
25
|
: undefined;
|
|
@@ -27,9 +28,16 @@ function createModuleMiddleware(context) {
|
|
|
27
28
|
await unsignedRedirect(req, res, moduleId, runtimeEnvironment, runtimeParams);
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
|
-
const moduleDefinition = await
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const moduleDefinition = await getTracer().trace({
|
|
32
|
+
name: RequestHandlerSpan.GetModule,
|
|
33
|
+
attributes: {
|
|
34
|
+
specifier: moduleId.specifier,
|
|
35
|
+
},
|
|
36
|
+
}, () => {
|
|
37
|
+
return moduleRegistry.getLinkedModule(moduleId,
|
|
38
|
+
// bundle must be `false` to resolve amd modules in `prod-compat` mode
|
|
39
|
+
{ ...runtimeEnvironment, bundle: false }, runtimeParams);
|
|
40
|
+
});
|
|
33
41
|
const { ownHash, linkedSource } = moduleDefinition;
|
|
34
42
|
// validate the requested instances exists
|
|
35
43
|
if (signature !== LATEST_SIGNATURE && ownHash !== signature) {
|
|
@@ -2,13 +2,13 @@ import { descriptions } from '@lwrjs/diagnostics';
|
|
|
2
2
|
import { getResourceIdentity } from './utils/identity.js';
|
|
3
3
|
import { handleErrors } from './utils/error-handling.js';
|
|
4
4
|
function createResourceMiddleware(context) {
|
|
5
|
-
const { appConfig,
|
|
5
|
+
const { appConfig, resourceRegistry, runtimeEnvironment: defaultRuntimeEnvironment } = context;
|
|
6
6
|
return async (req, res) => {
|
|
7
7
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
8
8
|
res.status(400).send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
9
9
|
return;
|
|
10
10
|
}
|
|
11
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment
|
|
11
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
12
12
|
const { resourceId } = getResourceIdentity(req);
|
|
13
13
|
const resource = await resourceRegistry.getResource(resourceId, runtimeEnvironment, runtimeParams);
|
|
14
14
|
if (req.isSiteGeneration()) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { descriptions, logger } from '@lwrjs/diagnostics';
|
|
2
2
|
import { getClientRoutes } from '@lwrjs/router';
|
|
3
3
|
import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, shortestTtl, } from '@lwrjs/shared-utils';
|
|
4
|
+
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
4
5
|
import { handleErrors } from './utils/error-handling.js';
|
|
5
6
|
import { LwrViewHandler } from '@lwrjs/view-registry';
|
|
6
7
|
function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
7
8
|
const errorRoute = errorRoutes.find((route) => route.status === 500);
|
|
8
9
|
const appConfig = context.appConfig;
|
|
9
|
-
const { environment: environmentConfig
|
|
10
|
+
const { environment: environmentConfig } = appConfig;
|
|
10
11
|
return async (req, res) => {
|
|
11
12
|
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
12
13
|
res.status(400);
|
|
@@ -18,7 +19,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
18
19
|
res.send(descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
19
20
|
return;
|
|
20
21
|
}
|
|
21
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(context.runtimeEnvironment
|
|
22
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(context.runtimeEnvironment);
|
|
22
23
|
// set the default environment if an environment is configured in the app config
|
|
23
24
|
if (!runtimeParams.environment && environmentConfig?.default) {
|
|
24
25
|
runtimeParams.environment = environmentConfig.default;
|
|
@@ -33,7 +34,14 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
33
34
|
let viewResponse;
|
|
34
35
|
let resolvedRoute;
|
|
35
36
|
try {
|
|
36
|
-
viewResponse = await
|
|
37
|
+
viewResponse = await getTracer().trace({
|
|
38
|
+
name: RequestHandlerSpan.GetView,
|
|
39
|
+
attributes: {
|
|
40
|
+
view: route.id,
|
|
41
|
+
},
|
|
42
|
+
}, () => {
|
|
43
|
+
return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
44
|
+
});
|
|
37
45
|
resolvedRoute = route;
|
|
38
46
|
}
|
|
39
47
|
catch (err) {
|
|
@@ -43,7 +51,14 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
43
51
|
// Log Unexpected Routing Errors
|
|
44
52
|
logger.error(`[view-middleware] Route Error ${req.originalUrl}`);
|
|
45
53
|
logger.error(err);
|
|
46
|
-
viewResponse = await
|
|
54
|
+
viewResponse = await getTracer().trace({
|
|
55
|
+
name: RequestHandlerSpan.GetView,
|
|
56
|
+
attributes: {
|
|
57
|
+
view: errorRoute.id,
|
|
58
|
+
},
|
|
59
|
+
}, () => {
|
|
60
|
+
return resolve.call(viewHandler, viewRequest, errorRoute, runtimeEnvironment, runtimeParams);
|
|
61
|
+
});
|
|
47
62
|
resolvedRoute = errorRoute;
|
|
48
63
|
}
|
|
49
64
|
if (req.isSiteGeneration()) {
|
|
@@ -67,7 +82,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
67
82
|
}
|
|
68
83
|
function createConfigMiddleware(routes, context, viewHandler) {
|
|
69
84
|
return async (req, res) => {
|
|
70
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(context.runtimeEnvironment
|
|
85
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(context.runtimeEnvironment);
|
|
71
86
|
const { appId, encodedViewPath } = req.params;
|
|
72
87
|
// match the app id to a route
|
|
73
88
|
const route = routes.find((route) => route.id === appId);
|
|
@@ -126,13 +141,20 @@ export function viewMiddleware(app, context) {
|
|
|
126
141
|
}
|
|
127
142
|
// Add localized routes
|
|
128
143
|
if (i18n.uriPattern === 'path-prefix') {
|
|
129
|
-
|
|
144
|
+
// Add a redirect handler for the default locale for each route /en-US/home -> 301 /home
|
|
145
|
+
const defaultLocalePaths = paths.map((routePath) => `/${i18n.defaultLocale}${routePath}`);
|
|
146
|
+
// Map all supported locale ids into an array. Filter out the default one. It will be handled separately
|
|
147
|
+
const supportedStr = i18n.locales
|
|
148
|
+
.map((l) => l.id)
|
|
149
|
+
.filter((id) => id !== i18n.defaultLocale)
|
|
150
|
+
.join('|');
|
|
130
151
|
paths.forEach((routePath) => {
|
|
131
152
|
const localizedPath = `/:locale(${supportedStr})${routePath}`;
|
|
132
|
-
logger.debug(`[view-middleware] Add localized path ${localizedPath}`);
|
|
133
153
|
paths.push(localizedPath);
|
|
134
154
|
});
|
|
155
|
+
addDefaultLocaleRedirects(i18n.defaultLocale, defaultLocalePaths, app);
|
|
135
156
|
}
|
|
157
|
+
logger.debug(`[view-middleware] Add view paths ${paths}`);
|
|
136
158
|
app.get(paths, handleErrors(createViewMiddleware(route, errorRoutes, context, viewHandler)));
|
|
137
159
|
}
|
|
138
160
|
// create and attach middleware for bootstrap configurations
|
|
@@ -141,4 +163,21 @@ export function viewMiddleware(app, context) {
|
|
|
141
163
|
// NOTE: a greedy `all` is used over `use` due to the way middleware is mounted in the server abstraction
|
|
142
164
|
app.get('/' + app.getRegexWildcard(), handleErrors(createNotFoundMiddleware(errorRoutes, context, viewHandler)));
|
|
143
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Adds a 301 redirect if attempting to route with default locale as the path prefix
|
|
168
|
+
*/
|
|
169
|
+
function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
|
|
170
|
+
logger.debug(`[view-middleware] Add default localized paths ${defaultLocalePaths}`);
|
|
171
|
+
app.get(defaultLocalePaths, (req, res) => {
|
|
172
|
+
// Get the original URL path and split it into segments
|
|
173
|
+
let modifiedPath = req.originalUrl.replace(`/${defaultLocale}`, '');
|
|
174
|
+
// Make sure there is a leading slash. Redirect to '' causes an infinite loop.
|
|
175
|
+
if (modifiedPath.indexOf('/') !== 0) {
|
|
176
|
+
modifiedPath = `/${modifiedPath}`;
|
|
177
|
+
}
|
|
178
|
+
// Perform a 301 redirect to the modified URL
|
|
179
|
+
res.setHeader('Location', modifiedPath);
|
|
180
|
+
return res.sendStatus(301);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
144
183
|
//# sourceMappingURL=view-middleware.js.map
|
|
@@ -9,12 +9,12 @@ export async function warmupServer(config, internalRequestKey) {
|
|
|
9
9
|
logger.setOptions({ dedupe: new Set([WARN]) });
|
|
10
10
|
}
|
|
11
11
|
logger.info('[Server Warmup] starting');
|
|
12
|
-
const { basePath,
|
|
12
|
+
const { basePath, port, routes, staticSiteGenerator } = config;
|
|
13
13
|
staticSiteGenerator.outputDir = skipDirCreation;
|
|
14
14
|
const urlRewriteMap = new Map();
|
|
15
15
|
const runtimeEnvironment = getRuntimeEnvironment(config);
|
|
16
16
|
// For each locale, generate all the modules
|
|
17
|
-
await new SiteGenerator().generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, new NetworkDispatcher(port, internalRequestKey), staticSiteGenerator.outputDir,
|
|
17
|
+
await new SiteGenerator().generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, new NetworkDispatcher(port, internalRequestKey), staticSiteGenerator.outputDir, urlRewriteMap);
|
|
18
18
|
logger.info('[Server Warmup] complete');
|
|
19
19
|
}
|
|
20
20
|
//# sourceMappingURL=server-warmup.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ImportMetadata, LwrDispatcher, StaticSiteGenerator, LwrRoute, NormalizedLwrGlobalConfig, RuntimeEnvironment
|
|
1
|
+
import type { ImportMetadata, LwrDispatcher, StaticSiteGenerator, LwrRoute, NormalizedLwrGlobalConfig, RuntimeEnvironment } from '@lwrjs/types';
|
|
2
2
|
import type { ResourceContextOpts, SiteConfig, ViewImportMetadata } from './types.js';
|
|
3
3
|
export default class SiteGenerator {
|
|
4
4
|
/**
|
|
@@ -14,7 +14,7 @@ export default class SiteGenerator {
|
|
|
14
14
|
/**
|
|
15
15
|
* Crawl all view routes for a site
|
|
16
16
|
*/
|
|
17
|
-
generateRoutes(runtimeEnvironment: RuntimeEnvironment, staticSiteGenerator: StaticSiteGenerator, routes: LwrRoute[], basePath: string, dispatcher: LwrDispatcher, outputDir: string,
|
|
17
|
+
generateRoutes(runtimeEnvironment: RuntimeEnvironment, staticSiteGenerator: StaticSiteGenerator, routes: LwrRoute[], basePath: string, dispatcher: LwrDispatcher, outputDir: string, urlRewriteMap?: Map<string, string>): Promise<void>;
|
|
18
18
|
/**
|
|
19
19
|
* Generate all routes for a given locale
|
|
20
20
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { performance } from 'perf_hooks';
|
|
2
2
|
import { logger, WARN, INFO } from '@lwrjs/diagnostics';
|
|
3
|
-
import { getSpecifier, getFeatureFlags, hashContent, isSelfUrl, getModuleUriPrefix, getMappingUriPrefix, isExternalUrl, mimeLookup, getViewUri, } from '@lwrjs/shared-utils';
|
|
3
|
+
import { getSpecifier, getFeatureFlags, hashContent, isSelfUrl, getModuleUriPrefix, getMappingUriPrefix, isExternalUrl, mimeLookup, getViewUri, sortLocalesByFallback, } from '@lwrjs/shared-utils';
|
|
4
4
|
import { SiteMetadataImpl } from '@lwrjs/static/site-metadata';
|
|
5
5
|
import { join, dirname, extname, normalize } from 'path';
|
|
6
6
|
import fs from 'fs-extra';
|
|
@@ -24,7 +24,7 @@ export default class SiteGenerator {
|
|
|
24
24
|
if (!logger.currentLevel || logger.currentLevel == WARN || logger.currentLevel == INFO) {
|
|
25
25
|
logger.setOptions({ dedupe: new Set([WARN]) });
|
|
26
26
|
}
|
|
27
|
-
const { assets, basePath,
|
|
27
|
+
const { assets, basePath, rootDir, routes, staticSiteGenerator } = config;
|
|
28
28
|
if (!staticSiteGenerator.outputDir) {
|
|
29
29
|
staticSiteGenerator.outputDir = 'site';
|
|
30
30
|
}
|
|
@@ -40,7 +40,7 @@ export default class SiteGenerator {
|
|
|
40
40
|
const runtimeEnvironment = getRuntimeEnvironment(config);
|
|
41
41
|
// For each locale, generate all the modules
|
|
42
42
|
logger.info(`[SSG] Building routes (this may take some time to complete)`);
|
|
43
|
-
await this.generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir,
|
|
43
|
+
await this.generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, urlRewriteMap);
|
|
44
44
|
// Write redirect files
|
|
45
45
|
this.writeNetlifyRedirectConfig(outputDir, urlRewriteMap);
|
|
46
46
|
// Copy over assets
|
|
@@ -52,28 +52,32 @@ export default class SiteGenerator {
|
|
|
52
52
|
/**
|
|
53
53
|
* Crawl all view routes for a site
|
|
54
54
|
*/
|
|
55
|
-
async generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir,
|
|
55
|
+
async generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, urlRewriteMap = new Map()) {
|
|
56
56
|
const generateUrl = this.createGenerateURLFunction(dispatcher);
|
|
57
57
|
// Note: generateUrl can consume a lot of memory so we need to do this sequentially
|
|
58
58
|
const { skipBaseDocumentGeneration = false } = staticSiteGenerator;
|
|
59
|
+
const i18n = runtimeEnvironment.i18n;
|
|
59
60
|
// If there is not a know URL pattern it is expected the app is accounting with the locale in the routes
|
|
60
61
|
if (!i18n.uriPattern) {
|
|
61
62
|
// eslint-disable-next-line no-await-in-loop
|
|
62
|
-
await this.generateRoutesForLocale(routes, outputDir, { id: i18n.defaultLocale }, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment,
|
|
63
|
+
await this.generateRoutesForLocale(routes, outputDir, { id: i18n.defaultLocale }, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, basePath, generateUrl, staticSiteGenerator, dispatcher);
|
|
63
64
|
}
|
|
64
65
|
else {
|
|
66
|
+
// Run through locales in fallback sorted order
|
|
67
|
+
const sortedLocales = sortLocalesByFallback(i18n);
|
|
65
68
|
// Generate all the routes
|
|
66
|
-
for (const locale of
|
|
69
|
+
for (const locale of sortedLocales) {
|
|
67
70
|
logger.debug(`[SSG] Generate routes of locale: ${locale.id}`);
|
|
68
71
|
// eslint-disable-next-line no-await-in-loop
|
|
69
|
-
await this.generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment,
|
|
72
|
+
await this.generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, basePath, generateUrl, staticSiteGenerator, dispatcher);
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
75
|
}
|
|
73
76
|
/**
|
|
74
77
|
* Generate all routes for a given locale
|
|
75
78
|
*/
|
|
76
|
-
async generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment,
|
|
79
|
+
async generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, basePath, generateUrl, staticSiteGenerator, dispatcher) {
|
|
80
|
+
const i18n = runtimeEnvironment.i18n;
|
|
77
81
|
for (const route of routes) {
|
|
78
82
|
const siteConfig = this.createSiteConfig(outputDir, locale.id, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n);
|
|
79
83
|
const localizedPath = getViewUri(route.path, basePath, locale.id, i18n);
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.11.0-alpha.
|
|
7
|
+
"version": "0.11.0-alpha.15",
|
|
8
8
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -39,33 +39,33 @@
|
|
|
39
39
|
"build": "tsc -b"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@lwrjs/app-service": "0.11.0-alpha.
|
|
43
|
-
"@lwrjs/asset-registry": "0.11.0-alpha.
|
|
44
|
-
"@lwrjs/asset-transformer": "0.11.0-alpha.
|
|
45
|
-
"@lwrjs/base-view-provider": "0.11.0-alpha.
|
|
46
|
-
"@lwrjs/base-view-transformer": "0.11.0-alpha.
|
|
47
|
-
"@lwrjs/client-modules": "0.11.0-alpha.
|
|
48
|
-
"@lwrjs/config": "0.11.0-alpha.
|
|
49
|
-
"@lwrjs/diagnostics": "0.11.0-alpha.
|
|
50
|
-
"@lwrjs/esbuild": "0.11.0-alpha.
|
|
51
|
-
"@lwrjs/fs-asset-provider": "0.11.0-alpha.
|
|
52
|
-
"@lwrjs/fs-watch": "0.11.0-alpha.
|
|
53
|
-
"@lwrjs/html-view-provider": "0.11.0-alpha.
|
|
54
|
-
"@lwrjs/instrumentation": "0.11.0-alpha.
|
|
55
|
-
"@lwrjs/loader": "0.11.0-alpha.
|
|
56
|
-
"@lwrjs/lwc-module-provider": "0.11.0-alpha.
|
|
57
|
-
"@lwrjs/markdown-view-provider": "0.11.0-alpha.
|
|
58
|
-
"@lwrjs/module-bundler": "0.11.0-alpha.
|
|
59
|
-
"@lwrjs/module-registry": "0.11.0-alpha.
|
|
60
|
-
"@lwrjs/npm-module-provider": "0.11.0-alpha.
|
|
61
|
-
"@lwrjs/nunjucks-view-provider": "0.11.0-alpha.
|
|
62
|
-
"@lwrjs/o11y": "0.11.0-alpha.
|
|
63
|
-
"@lwrjs/resource-registry": "0.11.0-alpha.
|
|
64
|
-
"@lwrjs/router": "0.11.0-alpha.
|
|
65
|
-
"@lwrjs/server": "0.11.0-alpha.
|
|
66
|
-
"@lwrjs/shared-utils": "0.11.0-alpha.
|
|
67
|
-
"@lwrjs/static": "0.11.0-alpha.
|
|
68
|
-
"@lwrjs/view-registry": "0.11.0-alpha.
|
|
42
|
+
"@lwrjs/app-service": "0.11.0-alpha.15",
|
|
43
|
+
"@lwrjs/asset-registry": "0.11.0-alpha.15",
|
|
44
|
+
"@lwrjs/asset-transformer": "0.11.0-alpha.15",
|
|
45
|
+
"@lwrjs/base-view-provider": "0.11.0-alpha.15",
|
|
46
|
+
"@lwrjs/base-view-transformer": "0.11.0-alpha.15",
|
|
47
|
+
"@lwrjs/client-modules": "0.11.0-alpha.15",
|
|
48
|
+
"@lwrjs/config": "0.11.0-alpha.15",
|
|
49
|
+
"@lwrjs/diagnostics": "0.11.0-alpha.15",
|
|
50
|
+
"@lwrjs/esbuild": "0.11.0-alpha.15",
|
|
51
|
+
"@lwrjs/fs-asset-provider": "0.11.0-alpha.15",
|
|
52
|
+
"@lwrjs/fs-watch": "0.11.0-alpha.15",
|
|
53
|
+
"@lwrjs/html-view-provider": "0.11.0-alpha.15",
|
|
54
|
+
"@lwrjs/instrumentation": "0.11.0-alpha.15",
|
|
55
|
+
"@lwrjs/loader": "0.11.0-alpha.15",
|
|
56
|
+
"@lwrjs/lwc-module-provider": "0.11.0-alpha.15",
|
|
57
|
+
"@lwrjs/markdown-view-provider": "0.11.0-alpha.15",
|
|
58
|
+
"@lwrjs/module-bundler": "0.11.0-alpha.15",
|
|
59
|
+
"@lwrjs/module-registry": "0.11.0-alpha.15",
|
|
60
|
+
"@lwrjs/npm-module-provider": "0.11.0-alpha.15",
|
|
61
|
+
"@lwrjs/nunjucks-view-provider": "0.11.0-alpha.15",
|
|
62
|
+
"@lwrjs/o11y": "0.11.0-alpha.15",
|
|
63
|
+
"@lwrjs/resource-registry": "0.11.0-alpha.15",
|
|
64
|
+
"@lwrjs/router": "0.11.0-alpha.15",
|
|
65
|
+
"@lwrjs/server": "0.11.0-alpha.15",
|
|
66
|
+
"@lwrjs/shared-utils": "0.11.0-alpha.15",
|
|
67
|
+
"@lwrjs/static": "0.11.0-alpha.15",
|
|
68
|
+
"@lwrjs/view-registry": "0.11.0-alpha.15",
|
|
69
69
|
"chokidar": "^3.5.3",
|
|
70
70
|
"esbuild": "^0.9.7",
|
|
71
71
|
"fs-extra": "^11.1.1",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"ws": "^8.8.1"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@lwrjs/types": "0.11.0-alpha.
|
|
78
|
+
"@lwrjs/types": "0.11.0-alpha.15",
|
|
79
79
|
"@types/ws": "^8.5.3"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
@@ -87,5 +87,5 @@
|
|
|
87
87
|
"volta": {
|
|
88
88
|
"extends": "../../../package.json"
|
|
89
89
|
},
|
|
90
|
-
"gitHead": "
|
|
90
|
+
"gitHead": "3c6c6d17b2f57a5b67d32e40a1e574abb7d9afea"
|
|
91
91
|
}
|