@taujs/react 0.1.2 → 0.1.4

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/index.d.ts CHANGED
@@ -56,31 +56,35 @@ type StreamOptions = {
56
56
  /** Whether to use cork/uncork for batched writes (default: true) */
57
57
  useCork?: boolean;
58
58
  };
59
- type HeadContext<T extends Record<string, unknown> = Record<string, unknown>> = {
59
+ type HeadContext<T extends Record<string, unknown> = Record<string, unknown>, R = unknown> = {
60
60
  data: T;
61
61
  meta: Record<string, unknown>;
62
+ routeContext?: R;
62
63
  };
63
64
  type SSRResult = {
64
65
  headContent: string;
65
66
  appHtml: string;
66
67
  aborted: boolean;
67
68
  };
68
- type StreamCallOptions = StreamOptions & {
69
+ type StreamCallOptions<R> = StreamOptions & {
69
70
  logger?: LoggerLike;
71
+ routeContext?: R;
70
72
  };
71
- declare function createRenderer<T extends Record<string, unknown>>({ appComponent, headContent, streamOptions, logger, enableDebug, }: {
73
+ declare function createRenderer<T extends Record<string, unknown> = Record<string, unknown>, R = unknown>({ appComponent, headContent, streamOptions, logger, enableDebug, }: {
72
74
  appComponent: (props: {
73
75
  location: string;
76
+ routeContext?: R;
74
77
  }) => React.ReactElement;
75
- headContent: (ctx: HeadContext<T>) => string;
78
+ headContent: (ctx: HeadContext<T, R>) => string;
76
79
  enableDebug?: boolean;
77
80
  logger?: LoggerLike;
78
81
  streamOptions?: StreamOptions;
79
82
  }): {
80
83
  renderSSR: (initialData: T, location: string, meta?: Record<string, unknown>, signal?: AbortSignal, opts?: {
81
84
  logger?: LoggerLike;
85
+ routeContext?: R;
82
86
  }) => Promise<SSRResult>;
83
- renderStream: (writable: Writable, callbacks: RenderCallbacks<T> | undefined, initialData: T | Promise<T> | (() => Promise<T>), location: string, bootstrapModules?: string, meta?: Record<string, unknown>, cspNonce?: string, signal?: AbortSignal, opts?: StreamCallOptions) => {
87
+ renderStream: (writable: Writable, callbacks: RenderCallbacks<T> | undefined, initialData: T | Promise<T> | (() => Promise<T>), location: string, bootstrapModules?: string, meta?: Record<string, unknown>, cspNonce?: string, signal?: AbortSignal, opts?: StreamCallOptions<R>) => {
84
88
  abort: () => void;
85
89
  done: Promise<void>;
86
90
  };
package/dist/index.js CHANGED
@@ -157,10 +157,13 @@ function hydrateApp({
157
157
  onSuccess
158
158
  }) {
159
159
  const { log, warn, error } = createUILogger(logger, { debugCategory: "ssr", context: { scope: "react-hydration" }, enableDebug });
160
- const mountCSR = (rootEl) => {
160
+ const mountCSR = (rootEl, initialData) => {
161
161
  rootEl.innerHTML = "";
162
+ const store = createSSRStore(initialData);
162
163
  const root = createRoot(rootEl);
163
- root.render(/* @__PURE__ */ jsx2(React2.StrictMode, { children: appComponent }));
164
+ root.render(
165
+ /* @__PURE__ */ jsx2(React2.StrictMode, { children: /* @__PURE__ */ jsx2(SSRStoreProvider, { store, children: appComponent }) })
166
+ );
164
167
  };
165
168
  const startHydration = (rootEl, initialData) => {
166
169
  if (enableDebug) log("Hydration started");
@@ -193,8 +196,9 @@ function hydrateApp({
193
196
  }
194
197
  const data = window[dataKey];
195
198
  if (data === void 0) {
199
+ const data2 = {};
196
200
  if (enableDebug) warn(`No initial SSR data at window["${dataKey}"]. Mounting CSR.`);
197
- mountCSR(rootEl);
201
+ mountCSR(rootEl, data2);
198
202
  return;
199
203
  }
200
204
  startHydration(rootEl, data);
@@ -374,11 +378,12 @@ function createRenderer({
374
378
  let aborted = false;
375
379
  const onAbort = () => aborted = true;
376
380
  signal?.addEventListener("abort", onAbort, { once: true });
381
+ const routeContext = opts?.routeContext;
377
382
  try {
378
383
  log("Starting SSR:", location);
379
- const dynamicHead = headContent({ data: initialData, meta });
384
+ const dynamicHead = headContent({ data: initialData, meta, routeContext });
380
385
  const store = createSSRStore(initialData);
381
- const html = renderToString(/* @__PURE__ */ jsx3(SSRStoreProvider, { store, children: appComponent({ location }) }));
386
+ const html = renderToString(/* @__PURE__ */ jsx3(SSRStoreProvider, { store, children: appComponent({ location, routeContext }) }));
382
387
  if (aborted) {
383
388
  warn("SSR completed after client abort", { location });
384
389
  return { headContent: "", appHtml: "", aborted: true };
@@ -405,6 +410,7 @@ function createRenderer({
405
410
  context: { scope: "react-streaming" },
406
411
  enableDebug
407
412
  });
413
+ const routeContext = opts?.routeContext;
408
414
  const effectiveShellTimeout = opts?.shellTimeoutMs ?? shellTimeoutMs;
409
415
  const effectiveUseCork = opts?.useCork ?? useCork;
410
416
  const controller = createStreamController(writable, { log, warn, error });
@@ -443,7 +449,7 @@ function createRenderer({
443
449
  log("Starting stream:", location);
444
450
  try {
445
451
  const store = createSSRStore(initialData);
446
- const appElement = /* @__PURE__ */ jsx3(SSRStoreProvider, { store, children: appComponent({ location }) });
452
+ const appElement = /* @__PURE__ */ jsx3(SSRStoreProvider, { store, children: appComponent({ location, routeContext }) });
447
453
  const stream = renderToPipeableStream(appElement, {
448
454
  nonce: cspNonce,
449
455
  bootstrapModules: bootstrapModules ? [bootstrapModules] : void 0,
@@ -464,7 +470,7 @@ function createRenderer({
464
470
  });
465
471
  }
466
472
  }
467
- const head = headContent({ data: headData ?? {}, meta });
473
+ const head = headContent({ data: headData ?? {}, meta, routeContext });
468
474
  const canCork = effectiveUseCork && typeof writable.cork === "function" && typeof writable.uncork === "function";
469
475
  if (canCork)
470
476
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/react",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "taujs | τjs",
5
5
  "author": "Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",