@krate/runtime 0.5.0-rc.16 → 0.5.0-rc.17

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/server.js CHANGED
@@ -7,7 +7,7 @@ import { sealBoundArgs } from './action-seal.js';
7
7
  import { accessDeniedStatus, isDynamicUsageError, isNotFoundSignal, redirectTarget } from './request-context.js';
8
8
  import { sha256Hex } from './sha256.js';
9
9
  import { currentRenderScope, markErrorCaught } from './cache-scope.js';
10
- import { encodeProp } from './flight-props.js';
10
+ import { encodeProp, settleThenables } from './flight-props.js';
11
11
  export { getActionContext, headers, cookies, redirect } from './action-context.js';
12
12
  export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
13
13
  export { after } from './after.js';
@@ -677,16 +677,27 @@ const flightIsJSX = (v) => isElement(v) || "__raw" in v;
677
677
  // actions become references the client chunk turns back into callable
678
678
  // proxies; anything JSON and the tag encoding cannot carry faithfully throws
679
679
  // with the offending prop path.
680
- function serializeClientProp(v, path, component) {
680
+ function serializeClientProp(v, path, component, pkg) {
681
681
  if (v === undefined)
682
682
  return undefined;
683
683
  return encodeProp(v, path, {
684
684
  isAction: flightIsAction,
685
685
  sealBound: sealBoundArgs,
686
686
  isJSX: flightIsJSX,
687
- fail: (p, what, hint) => clientPropError(p, component, what, hint),
687
+ fail: (p, what, hint) => pkg && what === "a function" ? packageFunctionPropError(p, component, pkg) : clientPropError(p, component, what, hint),
688
688
  });
689
689
  }
690
+ // packageFunctionPropError explains a function handed to a component of a
691
+ // React package without "use client": Krate renders such components with
692
+ // Preact, like client components, so the function (often a component of the
693
+ // app, as in an MDX `components` map) cannot reach it.
694
+ function packageFunctionPropError(path, component, pkg) {
695
+ return new Error(`Prop \`${path}\` passed to <${component}> from the package "${pkg}" is a function. The package has no "use client", ` +
696
+ `but Krate renders the components of React packages in server components with Preact, like client components, ` +
697
+ `so their props must be serializable and cannot hold functions or components of your app. ` +
698
+ `Copy <${component}> into your project (it then renders as a server component with the functions you pass), ` +
699
+ `or render it from a "use client" component of your own that imports those functions itself.`);
700
+ }
690
701
  function clientPropError(path, component, what, hint) {
691
702
  return new Error(`Prop \`${path}\` passed to client component <${component}> is ${what}, which cannot be sent from a server ` +
692
703
  `component to the client. ` +
@@ -751,7 +762,9 @@ async function clientProps(props, ctx, tree) {
751
762
  for (const [k, v] of Object.entries(props)) {
752
763
  if (k === "key" || k === "children")
753
764
  continue;
754
- const sv = isSlotValue(v) ? await slotProp(v, ctx, tree) : serializeClientProp(v, k, ctx.component);
765
+ if (!isSlotValue(v))
766
+ await settleThenables(v);
767
+ const sv = isSlotValue(v) ? await slotProp(v, ctx, tree) : serializeClientProp(v, k, ctx.component, ctx.pkg);
755
768
  if (sv !== undefined)
756
769
  out[k] = sv;
757
770
  }
@@ -910,16 +923,16 @@ function warnUncloneable(component, tree) {
910
923
  console.warn(`[krate] <${component} asChild> received a child rendered to HTML, so it cannot merge its props and handlers ` +
911
924
  `into it. Pass a single element or a component that renders one (not raw HTML) as the child.`);
912
925
  }
913
- function wrapClientNamespace(moduleId, exportName, src, ns, render, hydrate) {
926
+ function wrapClientNamespace(moduleId, exportName, src, ns, render, hydrate, pkg) {
914
927
  const out = {};
915
928
  for (const key of Object.keys(ns)) {
916
929
  const v = ns[key];
917
930
  const isComponentName = /^[A-Z]/.test(key);
918
931
  if (isComponentName && typeof v === "function") {
919
- out[key] = clientBoundary(moduleId, `${exportName}.${key}`, src, v, render, hydrate);
932
+ out[key] = clientBoundary(moduleId, `${exportName}.${key}`, src, v, render, hydrate, false, pkg);
920
933
  }
921
934
  else if (isComponentName && v && typeof v === "object" && !Array.isArray(v)) {
922
- out[key] = wrapClientNamespace(moduleId, `${exportName}.${key}`, src, v, render, hydrate);
935
+ out[key] = wrapClientNamespace(moduleId, `${exportName}.${key}`, src, v, render, hydrate, pkg);
923
936
  }
924
937
  else {
925
938
  out[key] = v;
@@ -944,11 +957,12 @@ function wrapClientNamespace(moduleId, exportName, src, ns, render, hydrate) {
944
957
  * always travel as a tree, with a server component at the top of a child run
945
958
  * to the element it renders, so the component's Slot can clone it. A
946
959
  * namespace export (Radix's Dialog) is wrapped member by member. With `flight` the children always travel as a
947
- * tree (a real-React island rebuilds them as elements, not HTML).
960
+ * tree (a real-React island rebuilds them as elements, not HTML). pkg names
961
+ * the package specifier of a package component (no "use client"), for errors.
948
962
  */
949
- export function clientBoundary(moduleId, exportName, src, impl, render, hydrate = true, flight = false) {
963
+ export function clientBoundary(moduleId, exportName, src, impl, render, hydrate = true, flight = false, pkg) {
950
964
  if (impl && typeof impl === "object" && !Array.isArray(impl)) {
951
- return wrapClientNamespace(moduleId, exportName, src, impl, render, hydrate);
965
+ return wrapClientNamespace(moduleId, exportName, src, impl, render, hydrate, pkg);
952
966
  }
953
967
  if (typeof impl !== "function")
954
968
  return impl;
@@ -968,7 +982,7 @@ export function clientBoundary(moduleId, exportName, src, impl, render, hydrate
968
982
  // Taken before the first await, so islands number in render order.
969
983
  const ids = islandIdPrefix(moduleId, exportName, key);
970
984
  const slots = [];
971
- const slotCtx = { hydrate: false, component: name, slots, vals };
985
+ const slotCtx = { hydrate: false, component: name, slots, vals, pkg };
972
986
  const data = await clientProps(rest, slotCtx, flight);
973
987
  const hasChildren = children != null && !(Array.isArray(children) && children.length === 0);
974
988
  let hydrates = hydrate || slotCtx.hydrate;
package/dist/signal.js CHANGED
@@ -1216,7 +1216,10 @@ export function createRoot(fn) {
1216
1216
  }
1217
1217
  }
1218
1218
  export function onMount(fn) {
1219
- mountQueue.push({ fn, owner: context[context.length - 1], boundary: ownerHooks ? ownerHooks.current() : null });
1219
+ // The route scope is captured now: by the time the queue flushes, the
1220
+ // hydration code of a kept layout has left its scope, and the cleanups the
1221
+ // callback registers must still survive navigations that keep the layout.
1222
+ mountQueue.push({ fn, owner: context[context.length - 1], boundary: ownerHooks ? ownerHooks.current() : null, scope: krateCurrentScope() });
1220
1223
  if (!mountScheduled) {
1221
1224
  mountScheduled = true;
1222
1225
  queueMicrotask(flushMounts);
@@ -1225,10 +1228,17 @@ export function onMount(fn) {
1225
1228
  function flushMounts() {
1226
1229
  mountScheduled = false;
1227
1230
  const queue = mountQueue.splice(0);
1228
- for (const { fn, owner, boundary } of queue) {
1231
+ for (const { fn, owner, boundary, scope } of queue) {
1229
1232
  const run = () => {
1230
1233
  if (!owner) {
1231
- fn();
1234
+ const prev = routeScope;
1235
+ routeScope = scope;
1236
+ try {
1237
+ fn();
1238
+ }
1239
+ finally {
1240
+ routeScope = prev;
1241
+ }
1232
1242
  return;
1233
1243
  }
1234
1244
  // The owner unmounted before its mount effects ran: skip them, as React
package/dist/slots.js CHANGED
@@ -11,6 +11,7 @@
11
11
  // branches keep working across toggles.
12
12
  import { krateRefsEnter, krateRefsLeave } from './refs.js';
13
13
  import { krateMountFields } from './events.js';
14
+ import { krateKeepForExit, krateRestoreKept } from './dom.js';
14
15
  import { kratePortalsEnter, kratePortalsIn, kratePortalsLeave } from './portal.js';
15
16
  export * from './portal.js';
16
17
  // Branches swapped out of the document. Hydration lookups (findSlot) search
@@ -83,11 +84,17 @@ export function krateCondSwap(start, show, tpl) {
83
84
  if (!st.end || st.end.parentNode !== parent)
84
85
  st.end = findCondEnd(start);
85
86
  const end = st.end;
87
+ // Elements of the branch coming back that are still running their exit
88
+ // return to their places first, so they are not swapped out again.
89
+ if (st.other)
90
+ krateRestoreKept(st.other);
86
91
  const out = document.createDocumentFragment();
87
92
  let n = start.nextSibling;
88
93
  while (n && n !== end) {
89
94
  const next = n.nextSibling;
90
- out.appendChild(n);
95
+ // An element kept for its exit animation stays until it has finished.
96
+ if (n.nodeType !== 1 || !krateKeepForExit(n, out))
97
+ out.appendChild(n);
91
98
  n = next;
92
99
  }
93
100
  // React order: refs of the outgoing branch detach before the incoming
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krate/runtime",
3
- "version": "0.5.0-rc.16",
3
+ "version": "0.5.0-rc.17",
4
4
  "description": "Client-side runtime for Krate - signals, hydration, SPA router, resources, and context.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",