@blinkk/root 2.0.10 → 2.1.1

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/dist/node.js CHANGED
@@ -7,8 +7,8 @@ import {
7
7
  loadPackageJson,
8
8
  loadRootConfig,
9
9
  viteSsrLoadModule
10
- } from "./chunk-HTBDTUFE.js";
11
- import "./chunk-MA6MSEP4.js";
10
+ } from "./chunk-KKBUZU3B.js";
11
+ import "./chunk-V2WNR3EX.js";
12
12
  export {
13
13
  bundleRootConfig,
14
14
  createViteServer,
package/dist/render.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { J as Renderer } from './types-Mr1zFAT1.js';
1
+ export { J as Renderer } from './types-mA-Pt2Qw.js';
2
2
  import 'express';
3
3
  import 'preact';
4
4
  import 'vite';
package/dist/render.js CHANGED
@@ -13095,6 +13095,10 @@ var Renderer = class {
13095
13095
  this.elementGraph = options.elementGraph;
13096
13096
  this.router = new Router(rootConfig);
13097
13097
  }
13098
+ /** Returns a route from the router. */
13099
+ getRoute(url) {
13100
+ return this.router.get(url);
13101
+ }
13098
13102
  async handle(req, res, next) {
13099
13103
  let url = req.path;
13100
13104
  if (url.includes("%")) {
@@ -13105,7 +13109,7 @@ var Renderer = class {
13105
13109
  }
13106
13110
  const matches = this.router.matchAll(url);
13107
13111
  let matchIndex = 0;
13108
- const processNextMatch = async () => {
13112
+ const nextRouteHandler = async () => {
13109
13113
  const match = matches[matchIndex++];
13110
13114
  if (!match) {
13111
13115
  next();
@@ -13115,143 +13119,12 @@ var Renderer = class {
13115
13119
  if (route.locale) {
13116
13120
  routeParams.$locale = route.locale;
13117
13121
  }
13118
- const fallbackLocales = route.isDefaultLocale ? getFallbackLocales(req) : [route.locale];
13119
- const getPreferredLocale = (availableLocales) => {
13120
- const lowerLocales = availableLocales.map((l) => l.toLowerCase());
13121
- for (const fallbackLocale of fallbackLocales) {
13122
- if (lowerLocales.includes(fallbackLocale.toLowerCase())) {
13123
- return fallbackLocale;
13124
- }
13125
- }
13126
- return req.rootConfig?.i18n?.defaultLocale || "en";
13127
- };
13128
- const render404 = async (options) => {
13129
- if (options?.nextRoute) {
13130
- await processNextMatch();
13131
- return;
13132
- }
13133
- next();
13134
- };
13135
- const render = async (props2, options) => {
13136
- if (!route.module.default) {
13137
- console.error(`no default component exported in route: ${route.src}`);
13138
- render404();
13139
- return;
13140
- }
13141
- const securityConfig = this.getSecurityConfig();
13142
- const cspEnabled = !!securityConfig.contentSecurityPolicy;
13143
- const currentPath = req.path;
13144
- const locale = options?.locale || route.locale;
13145
- const translations = options?.translations;
13146
- const nonce = cspEnabled ? this.generateNonce() : void 0;
13147
- const output = await this.renderComponent(route.module.default, props2, {
13148
- currentPath,
13149
- route,
13150
- routeParams,
13151
- locale,
13152
- translations,
13153
- nonce
13154
- });
13155
- let html = output.html;
13156
- if (this.rootConfig.prettyHtml) {
13157
- html = await htmlPretty(html, this.rootConfig.prettyHtmlOptions);
13158
- } else if (this.rootConfig.minifyHtml !== false) {
13159
- html = await htmlMinify(html, this.rootConfig.minifyHtmlOptions);
13160
- }
13161
- if (req.viteServer) {
13162
- html = await req.viteServer.transformIndexHtml(currentPath, html);
13163
- if (nonce) {
13164
- html = html.replace(
13165
- '<script type="module" src="/@vite/client"></script>',
13166
- `<script type="module" src="/@vite/client" nonce="${nonce}"></script>`
13167
- );
13168
- }
13169
- }
13170
- let statusCode = 200;
13171
- if (route.src === "routes/404.tsx") {
13172
- statusCode = 404;
13173
- } else if (route.src === "routes/500.tsx") {
13174
- statusCode = 500;
13175
- }
13176
- req.hooks.trigger("preRender");
13177
- const plugins = this.rootConfig.plugins || [];
13178
- for (const plugin of plugins) {
13179
- if (plugin.hooks?.preRender) {
13180
- const newHtml = await plugin.hooks.preRender(html);
13181
- if (newHtml && typeof newHtml === "string") {
13182
- html = newHtml;
13183
- }
13184
- }
13185
- }
13186
- res.status(statusCode);
13187
- res.set({ "Content-Type": "text/html" });
13188
- this.setSecurityHeaders(res, {
13189
- securityConfig,
13190
- nonce
13191
- });
13192
- res.end(html);
13193
- };
13194
- if (route.module.getStaticContent) {
13195
- let props2;
13196
- if (route.module.getStaticProps) {
13197
- props2 = await route.module.getStaticProps({
13198
- rootConfig: this.rootConfig,
13199
- params: routeParams
13200
- });
13201
- if (props2?.notFound) {
13202
- return render404();
13203
- }
13204
- } else {
13205
- props2 = { rootConfig: this.rootConfig, params: routeParams };
13206
- }
13207
- const result = await route.module.getStaticContent(props2);
13208
- let body;
13209
- let contentType;
13210
- if (typeof result === "string" || Buffer.isBuffer(result)) {
13211
- body = result;
13212
- } else if (result && typeof result === "object") {
13213
- body = result.body;
13214
- contentType = result.contentType;
13215
- } else {
13216
- body = "";
13217
- }
13218
- res.status(200);
13219
- const ext = path2.extname(route.routePath);
13220
- res.set({
13221
- "Content-Type": contentType || guessContentType(ext)
13222
- });
13223
- res.end(body);
13224
- return;
13225
- }
13226
- if (route.module.handle) {
13227
- const handlerContext = {
13228
- route,
13229
- params: routeParams,
13230
- i18nFallbackLocales: fallbackLocales,
13231
- getPreferredLocale,
13232
- render,
13233
- render404
13234
- };
13235
- req.handlerContext = handlerContext;
13236
- return route.module.handle(req, res, next);
13237
- }
13238
- let props = {};
13239
- if (route.module.getStaticProps) {
13240
- const propsData = await route.module.getStaticProps({
13241
- rootConfig: this.rootConfig,
13242
- params: routeParams
13243
- });
13244
- if (propsData.notFound) {
13245
- return render404();
13246
- }
13247
- if (propsData.props) {
13248
- props = propsData.props;
13249
- }
13250
- }
13251
- await render(props);
13252
- return;
13122
+ await this.handleRoute(req, res, next, route, {
13123
+ routeParams,
13124
+ nextRouteHandler
13125
+ });
13253
13126
  };
13254
- await processNextMatch();
13127
+ await nextRouteHandler();
13255
13128
  }
13256
13129
  async renderComponent(Component, props, options) {
13257
13130
  const { currentPath, route, routeParams, nonce } = options;
@@ -13394,6 +13267,149 @@ var Renderer = class {
13394
13267
  translations
13395
13268
  });
13396
13269
  }
13270
+ /** Handles the SSR rendering of a route. */
13271
+ async handleRoute(req, res, next, route, options) {
13272
+ const defaultStatusCode = options?.defaultStatusCode || 200;
13273
+ const routeParams = options?.routeParams || {};
13274
+ const nextRouteHandler = options?.nextRouteHandler;
13275
+ const fallbackLocales = route.isDefaultLocale ? getFallbackLocales(req) : [route.locale];
13276
+ const getPreferredLocale = (availableLocales) => {
13277
+ const lowerLocales = availableLocales.map((l) => l.toLowerCase());
13278
+ for (const fallbackLocale of fallbackLocales) {
13279
+ if (lowerLocales.includes(fallbackLocale.toLowerCase())) {
13280
+ return fallbackLocale;
13281
+ }
13282
+ }
13283
+ return req.rootConfig?.i18n?.defaultLocale || "en";
13284
+ };
13285
+ const render404 = async (options2) => {
13286
+ if (options2?.nextRoute && nextRouteHandler) {
13287
+ await nextRouteHandler();
13288
+ return;
13289
+ }
13290
+ next();
13291
+ };
13292
+ const render = async (props2, options2) => {
13293
+ if (!route.module.default) {
13294
+ console.error(`no default component exported in route: ${route.src}`);
13295
+ render404();
13296
+ return;
13297
+ }
13298
+ const securityConfig = this.getSecurityConfig();
13299
+ const cspEnabled = !!securityConfig.contentSecurityPolicy;
13300
+ const currentPath = req.path;
13301
+ const locale = options2?.locale || route.locale;
13302
+ const translations = options2?.translations;
13303
+ const nonce = cspEnabled ? this.generateNonce() : void 0;
13304
+ const output = await this.renderComponent(route.module.default, props2, {
13305
+ currentPath,
13306
+ route,
13307
+ routeParams,
13308
+ locale,
13309
+ translations,
13310
+ nonce
13311
+ });
13312
+ let html = output.html;
13313
+ if (this.rootConfig.prettyHtml) {
13314
+ html = await htmlPretty(html, this.rootConfig.prettyHtmlOptions);
13315
+ } else if (this.rootConfig.minifyHtml !== false) {
13316
+ html = await htmlMinify(html, this.rootConfig.minifyHtmlOptions);
13317
+ }
13318
+ if (req.viteServer) {
13319
+ html = await req.viteServer.transformIndexHtml(currentPath, html);
13320
+ if (nonce) {
13321
+ html = html.replace(
13322
+ '<script type="module" src="/@vite/client"></script>',
13323
+ `<script type="module" src="/@vite/client" nonce="${nonce}"></script>`
13324
+ );
13325
+ }
13326
+ }
13327
+ let statusCode = defaultStatusCode;
13328
+ if (route.src === "routes/404.tsx") {
13329
+ statusCode = 404;
13330
+ } else if (route.src === "routes/401.tsx") {
13331
+ statusCode = 401;
13332
+ } else if (route.src === "routes/500.tsx") {
13333
+ statusCode = 500;
13334
+ }
13335
+ req.hooks.trigger("preRender");
13336
+ const plugins = this.rootConfig.plugins || [];
13337
+ for (const plugin of plugins) {
13338
+ if (plugin.hooks?.preRender) {
13339
+ const newHtml = await plugin.hooks.preRender(html);
13340
+ if (newHtml && typeof newHtml === "string") {
13341
+ html = newHtml;
13342
+ }
13343
+ }
13344
+ }
13345
+ res.status(statusCode);
13346
+ res.set({ "Content-Type": "text/html" });
13347
+ this.setSecurityHeaders(res, {
13348
+ securityConfig,
13349
+ nonce
13350
+ });
13351
+ res.end(html);
13352
+ };
13353
+ if (route.module.getStaticContent) {
13354
+ let props2;
13355
+ if (route.module.getStaticProps) {
13356
+ props2 = await route.module.getStaticProps({
13357
+ rootConfig: this.rootConfig,
13358
+ params: routeParams
13359
+ });
13360
+ if (props2?.notFound) {
13361
+ return render404();
13362
+ }
13363
+ } else {
13364
+ props2 = { rootConfig: this.rootConfig, params: routeParams };
13365
+ }
13366
+ const result = await route.module.getStaticContent(props2);
13367
+ let body;
13368
+ let contentType;
13369
+ if (typeof result === "string" || Buffer.isBuffer(result)) {
13370
+ body = result;
13371
+ } else if (result && typeof result === "object") {
13372
+ body = result.body;
13373
+ contentType = result.contentType;
13374
+ } else {
13375
+ body = "";
13376
+ }
13377
+ res.status(defaultStatusCode);
13378
+ const ext = path2.extname(route.routePath);
13379
+ res.set({
13380
+ "Content-Type": contentType || guessContentType(ext)
13381
+ });
13382
+ res.end(body);
13383
+ return;
13384
+ }
13385
+ if (route.module.handle) {
13386
+ const handlerContext = {
13387
+ route,
13388
+ params: routeParams,
13389
+ i18nFallbackLocales: fallbackLocales,
13390
+ getPreferredLocale,
13391
+ render,
13392
+ render404
13393
+ };
13394
+ req.handlerContext = handlerContext;
13395
+ return route.module.handle(req, res, next);
13396
+ }
13397
+ let props = {};
13398
+ if (route.module.getStaticProps) {
13399
+ const propsData = await route.module.getStaticProps({
13400
+ rootConfig: this.rootConfig,
13401
+ params: routeParams
13402
+ });
13403
+ if (propsData.notFound) {
13404
+ return render404();
13405
+ }
13406
+ if (propsData.props) {
13407
+ props = propsData.props;
13408
+ }
13409
+ }
13410
+ await render(props);
13411
+ return;
13412
+ }
13397
13413
  /**
13398
13414
  * Constructs and returns the project's sitemap.
13399
13415
  *