@blinkk/root 4.0.0-alpha.8 → 4.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/render.js CHANGED
@@ -17,7 +17,9 @@ import {
17
17
  import {
18
18
  renderJsxToString
19
19
  } from "./chunk-5OPFFJU7.js";
20
- import "./chunk-3Z5TNZEP.js";
20
+ import {
21
+ options
22
+ } from "./chunk-3Z5TNZEP.js";
21
23
  import {
22
24
  getSecurityConfig,
23
25
  setSecurityHeaders
@@ -13071,9 +13073,9 @@ var Router = class {
13071
13073
  return urlPaths;
13072
13074
  }
13073
13075
  };
13074
- function normalizeUrlPath(urlPath, options) {
13076
+ function normalizeUrlPath(urlPath, options2) {
13075
13077
  urlPath = urlPath.replace(/\/+/g, "/");
13076
- if (testPathHasFileExt(urlPath) || options?.trailingSlash === false && urlPath !== "/" && urlPath.endsWith("/")) {
13078
+ if (testPathHasFileExt(urlPath) || options2?.trailingSlash === false && urlPath !== "/" && urlPath.endsWith("/")) {
13077
13079
  urlPath = removeTrailingSlash(urlPath);
13078
13080
  }
13079
13081
  if (urlPath.endsWith("/index")) {
@@ -13082,7 +13084,7 @@ function normalizeUrlPath(urlPath, options) {
13082
13084
  if (!urlPath.startsWith("/")) {
13083
13085
  urlPath = `/${urlPath}`;
13084
13086
  }
13085
- if (options?.trailingSlash && !testPathHasFileExt(urlPath) && !urlPath.endsWith("/")) {
13087
+ if (options2?.trailingSlash && !testPathHasFileExt(urlPath) && !urlPath.endsWith("/")) {
13086
13088
  urlPath = `${urlPath}/`;
13087
13089
  }
13088
13090
  return urlPath;
@@ -13142,10 +13144,10 @@ var Renderer = class {
13142
13144
  assetMap;
13143
13145
  elementGraph;
13144
13146
  router;
13145
- constructor(rootConfig, options) {
13147
+ constructor(rootConfig, options2) {
13146
13148
  this.rootConfig = rootConfig;
13147
- this.assetMap = options.assetMap;
13148
- this.elementGraph = options.elementGraph;
13149
+ this.assetMap = options2.assetMap;
13150
+ this.elementGraph = options2.elementGraph;
13149
13151
  this.router = new Router(rootConfig);
13150
13152
  }
13151
13153
  /** Returns a route from the router. */
@@ -13187,12 +13189,12 @@ var Renderer = class {
13187
13189
  };
13188
13190
  await nextRouteHandler();
13189
13191
  }
13190
- async renderComponent(Component, props, options) {
13191
- const { currentPath, route, routeParams, nonce } = options;
13192
- const locale = options.locale;
13192
+ async renderComponent(Component, props, options2) {
13193
+ const { currentPath, route, routeParams, nonce } = options2;
13194
+ const locale = options2.locale;
13193
13195
  const translations = {
13194
13196
  ...getTranslations(locale),
13195
- ...options.translations || {}
13197
+ ...options2.translations || {}
13196
13198
  };
13197
13199
  const ctx = {
13198
13200
  currentPath,
@@ -13212,27 +13214,40 @@ var Renderer = class {
13212
13214
  styleDeps: []
13213
13215
  };
13214
13216
  const vdom = /* @__PURE__ */ jsx4(ASSET_MAP_CONTEXT.Provider, { value: this.assetMap, children: /* @__PURE__ */ jsx4(REQUEST_CONTEXT.Provider, { value: ctx, children: /* @__PURE__ */ jsx4(I18N_CONTEXT.Provider, { value: { locale, translations }, children: /* @__PURE__ */ jsx4(HTML_CONTEXT.Provider, { value: htmlContext, children: /* @__PURE__ */ jsx4(Component, { ...props }) }) }) }) });
13215
- const renderJsxFn = await this.getJsxRenderFn({ mode: options.renderMode });
13217
+ const renderJsxFn = await this.getJsxRenderFn({ mode: options2.renderMode });
13218
+ const injectNonce = (vnode) => {
13219
+ if (!vnode) {
13220
+ return;
13221
+ }
13222
+ if (vnode.type === "script" || vnode.type === "style") {
13223
+ vnode.props.nonce = nonce;
13224
+ }
13225
+ if (vnode.type === "link" && vnode.props.rel === "stylesheet") {
13226
+ vnode.props.nonce = nonce;
13227
+ }
13228
+ };
13216
13229
  const preactHook = preactOptions.vnode;
13230
+ const rootJsxHook = options.vnode;
13217
13231
  let mainHtml;
13218
13232
  try {
13219
13233
  preactOptions.vnode = (vnode) => {
13220
- if (vnode && vnode.type === "script") {
13221
- vnode.props.nonce = nonce;
13222
- }
13223
- if (vnode && vnode.type === "style") {
13224
- vnode.props.nonce = nonce;
13225
- }
13226
- if (vnode && vnode.type === "link" && vnode.props.rel === "stylesheet") {
13227
- vnode.props.nonce = nonce;
13228
- }
13234
+ injectNonce(vnode);
13229
13235
  if (preactHook) {
13230
13236
  preactHook(vnode);
13231
13237
  }
13232
13238
  };
13239
+ if (options !== preactOptions) {
13240
+ options.vnode = (vnode) => {
13241
+ injectNonce(vnode);
13242
+ if (rootJsxHook) {
13243
+ rootJsxHook(vnode);
13244
+ }
13245
+ };
13246
+ }
13233
13247
  mainHtml = renderJsxFn(vdom);
13234
13248
  } finally {
13235
13249
  preactOptions.vnode = preactHook;
13250
+ options.vnode = rootJsxHook;
13236
13251
  }
13237
13252
  const jsDeps = /* @__PURE__ */ new Set();
13238
13253
  const cssDeps = /* @__PURE__ */ new Set();
@@ -13292,13 +13307,13 @@ var Renderer = class {
13292
13307
  ...preloadTags,
13293
13308
  ...scriptTags
13294
13309
  ],
13295
- renderMode: options.renderMode
13310
+ renderMode: options2.renderMode
13296
13311
  });
13297
13312
  return { html };
13298
13313
  }
13299
13314
  /** SSG renders a route. */
13300
- async renderRoute(route, options) {
13301
- const routeParams = options.routeParams;
13315
+ async renderRoute(route, options2) {
13316
+ const routeParams = options2.routeParams;
13302
13317
  if (route.locale) {
13303
13318
  routeParams.$locale = route.locale;
13304
13319
  }
@@ -13343,10 +13358,10 @@ var Renderer = class {
13343
13358
  });
13344
13359
  }
13345
13360
  /** Handles the SSR rendering of a route. */
13346
- async handleRoute(req, res, next, route, options) {
13347
- const defaultStatusCode = options?.defaultStatusCode || 200;
13348
- const routeParams = options?.routeParams || {};
13349
- const nextRouteHandler = options?.nextRouteHandler;
13361
+ async handleRoute(req, res, next, route, options2) {
13362
+ const defaultStatusCode = options2?.defaultStatusCode || 200;
13363
+ const routeParams = options2?.routeParams || {};
13364
+ const nextRouteHandler = options2?.nextRouteHandler;
13350
13365
  const fallbackLocales = route.isDefaultLocale ? getFallbackLocales(req) : [route.locale];
13351
13366
  const getPreferredLocale = (availableLocales) => {
13352
13367
  const lowerLocales = availableLocales.map((l) => l.toLowerCase());
@@ -13357,8 +13372,8 @@ var Renderer = class {
13357
13372
  }
13358
13373
  return req.rootConfig?.i18n?.defaultLocale || "en";
13359
13374
  };
13360
- const render404 = async (options2) => {
13361
- if (options2?.nextRoute && nextRouteHandler) {
13375
+ const render404 = async (options3) => {
13376
+ if (options3?.nextRoute && nextRouteHandler) {
13362
13377
  await nextRouteHandler();
13363
13378
  return;
13364
13379
  }
@@ -13367,7 +13382,7 @@ var Renderer = class {
13367
13382
  }
13368
13383
  next();
13369
13384
  };
13370
- const render = async (props2, options2) => {
13385
+ const render = async (props2, options3) => {
13371
13386
  if (!route.module.default) {
13372
13387
  console.error(`no default component exported in route: ${route.src}`);
13373
13388
  render404();
@@ -13376,8 +13391,8 @@ var Renderer = class {
13376
13391
  const securityConfig = getSecurityConfig(this.rootConfig);
13377
13392
  const cspEnabled = !!securityConfig.contentSecurityPolicy;
13378
13393
  const currentPath = req.path;
13379
- const locale = options2?.locale || route.locale;
13380
- const translations = options2?.translations;
13394
+ const locale = options3?.locale || route.locale;
13395
+ const translations = options3?.translations;
13381
13396
  const nonce = cspEnabled ? this.generateNonce() : void 0;
13382
13397
  const output = await this.renderComponent(route.module.default, props2, {
13383
13398
  currentPath,
@@ -13386,7 +13401,7 @@ var Renderer = class {
13386
13401
  locale,
13387
13402
  translations,
13388
13403
  nonce,
13389
- renderMode: options2?.renderMode
13404
+ renderMode: options3?.renderMode
13390
13405
  });
13391
13406
  let html = await transformHtml(output.html, this.rootConfig);
13392
13407
  if (req.viteServer) {
@@ -13398,7 +13413,7 @@ var Renderer = class {
13398
13413
  );
13399
13414
  }
13400
13415
  }
13401
- let statusCode = options2?.statusCode || defaultStatusCode;
13416
+ let statusCode = options3?.statusCode || defaultStatusCode;
13402
13417
  if (route.src === "routes/404.tsx") {
13403
13418
  statusCode = 404;
13404
13419
  } else if (route.src === "routes/401.tsx") {
@@ -13561,8 +13576,8 @@ var Renderer = class {
13561
13576
  async getJsxRenderFn(renderOptions) {
13562
13577
  const mode = renderOptions?.mode ?? this.rootConfig.jsxRenderer?.mode;
13563
13578
  if (mode) {
13564
- const options = { ...this.getJsxRenderOptions(), mode };
13565
- return (vnode) => renderJsxToString(vnode, options);
13579
+ const options2 = { ...this.getJsxRenderOptions(), mode };
13580
+ return (vnode) => renderJsxToString(vnode, options2);
13566
13581
  }
13567
13582
  const { renderToString } = await import("preact-render-to-string");
13568
13583
  if (!renderToString) {
@@ -13577,8 +13592,8 @@ var Renderer = class {
13577
13592
  * `preact-render-to-string` depending if the `jsxRenderer` config is set up
13578
13593
  * in `root.config.ts`.
13579
13594
  */
13580
- async renderJsx(vnode, options) {
13581
- const render = await this.getJsxRenderFn(options);
13595
+ async renderJsx(vnode, options2) {
13596
+ const render = await this.getJsxRenderFn(options2);
13582
13597
  return render(vnode);
13583
13598
  }
13584
13599
  getConfiguredStyleEntries() {
@@ -13586,14 +13601,14 @@ var Renderer = class {
13586
13601
  const basePath = this.rootConfig.base || "/";
13587
13602
  return styleEntries.map((entry) => entry.trim()).filter((entry) => entry).map((entry) => normalizeStyleEntry(entry, basePath));
13588
13603
  }
13589
- async renderHtml(html, options) {
13590
- const htmlAttrs = options?.htmlAttrs || {};
13591
- const headAttrs = options?.headAttrs || {};
13592
- const bodyAttrs = options?.bodyAttrs || {};
13604
+ async renderHtml(html, options2) {
13605
+ const htmlAttrs = options2?.htmlAttrs || {};
13606
+ const headAttrs = options2?.headAttrs || {};
13607
+ const bodyAttrs = options2?.bodyAttrs || {};
13593
13608
  const page = /* @__PURE__ */ jsxs4("html", { ...htmlAttrs, children: [
13594
13609
  /* @__PURE__ */ jsxs4("head", { ...headAttrs, children: [
13595
13610
  /* @__PURE__ */ jsx4("meta", { charSet: "utf-8" }),
13596
- options?.headComponents
13611
+ options2?.headComponents
13597
13612
  ] }),
13598
13613
  /* @__PURE__ */ jsx4(
13599
13614
  "body",
@@ -13603,7 +13618,7 @@ var Renderer = class {
13603
13618
  }
13604
13619
  )
13605
13620
  ] });
13606
- const content = await this.renderJsx(page, { mode: options?.renderMode });
13621
+ const content = await this.renderJsx(page, { mode: options2?.renderMode });
13607
13622
  return `<!doctype html>
13608
13623
  ${content}`;
13609
13624
  }
@@ -13613,10 +13628,10 @@ ${content}`;
13613
13628
  }
13614
13629
  return str;
13615
13630
  }
13616
- async render404(options) {
13631
+ async render404(options2) {
13617
13632
  const basePath = this.rootConfig.base || "/";
13618
13633
  const notFoundPath = normalizeUrlPath(`${basePath}/404`);
13619
- const currentPath = options?.currentPath || notFoundPath;
13634
+ const currentPath = options2?.currentPath || notFoundPath;
13620
13635
  const [route, routeParams] = this.router.get(notFoundPath);
13621
13636
  if (route && route.src === "routes/404.tsx" && route.module.default) {
13622
13637
  const Component = route.module.default;
@@ -13651,8 +13666,8 @@ ${content}`;
13651
13666
  });
13652
13667
  return { html };
13653
13668
  }
13654
- async renderError(err, options) {
13655
- const currentPath = options?.currentPath || "/500";
13669
+ async renderError(err, options2) {
13670
+ const currentPath = options2?.currentPath || "/500";
13656
13671
  const [route, routeParams] = this.router.get("/500");
13657
13672
  if (route && route.src === "routes/500.tsx" && route.module.default) {
13658
13673
  const Component = route.module.default;