@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10

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.
Files changed (219) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +496 -91
  22. package/dist/dom.d.ts +4 -0
  23. package/dist/dom.js +62 -16
  24. package/dist/effects.js +43 -10
  25. package/dist/events.d.ts +2 -0
  26. package/dist/events.js +18 -0
  27. package/dist/flight-props.d.ts +7 -0
  28. package/dist/flight-props.js +105 -0
  29. package/dist/handler-html.js +4 -4
  30. package/dist/handler-routing.d.ts +2 -23
  31. package/dist/handler-routing.js +9 -190
  32. package/dist/handler-segments.d.ts +6 -0
  33. package/dist/handler-segments.js +18 -4
  34. package/dist/head-tags.d.ts +9 -0
  35. package/dist/head-tags.js +93 -6
  36. package/dist/highlight-bind.js +1 -1
  37. package/dist/highlight.d.ts +3 -2
  38. package/dist/highlight.js +17 -12
  39. package/dist/hints.d.ts +25 -2
  40. package/dist/hints.js +147 -74
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +3 -0
  43. package/dist/island-swap.d.ts +47 -0
  44. package/dist/island-swap.js +635 -0
  45. package/dist/jsx-dev-runtime.d.ts +1 -1
  46. package/dist/jsx-types.d.ts +99 -3
  47. package/dist/krate-core.js +1 -1
  48. package/dist/krate-dev.js +407 -80
  49. package/dist/krate-f-actions.js +1 -1
  50. package/dist/krate-f-classes.js +1 -1
  51. package/dist/krate-f-highlight.js +1 -1
  52. package/dist/krate-f-list.js +1 -1
  53. package/dist/krate-f-navigation.js +1 -1
  54. package/dist/krate-f-router-lite.js +1 -1
  55. package/dist/krate-f-router.js +1 -1
  56. package/dist/krate-features.json +455 -0
  57. package/dist/krate-hydrate-lite.js +5 -5
  58. package/dist/krate-hydrate.js +5 -5
  59. package/dist/krate-island-swap.js +1 -0
  60. package/dist/krate-runtime.js +5 -5
  61. package/dist/krate-suspense.js +1 -1
  62. package/dist/list.d.ts +14 -4
  63. package/dist/list.js +30 -23
  64. package/dist/metadata.js +5 -2
  65. package/dist/navigation-react.d.ts +13 -0
  66. package/dist/navigation-react.js +86 -0
  67. package/dist/navigation.d.ts +19 -0
  68. package/dist/navigation.js +35 -0
  69. package/dist/node-server.js +3 -3
  70. package/dist/og.js +1 -1
  71. package/dist/platform.d.ts +52 -0
  72. package/dist/platform.js +57 -0
  73. package/dist/react-compat.d.ts +39 -0
  74. package/dist/react-compat.js +359 -0
  75. package/dist/react-server.d.ts +63 -0
  76. package/dist/react-server.js +125 -0
  77. package/dist/region-wire.d.ts +10 -0
  78. package/dist/region-wire.js +154 -0
  79. package/dist/request-context.js +1 -1
  80. package/dist/request-extras.d.ts +4 -0
  81. package/dist/request-extras.js +51 -0
  82. package/dist/resp.js +1 -1
  83. package/dist/route-source.d.ts +39 -0
  84. package/dist/route-source.js +532 -0
  85. package/dist/router-lite.js +3 -9
  86. package/dist/router.d.ts +1 -1
  87. package/dist/router.js +675 -100
  88. package/dist/server-handler.d.ts +69 -1
  89. package/dist/server-handler.js +515 -96
  90. package/dist/server-renderer.js +16 -7
  91. package/dist/server.d.ts +73 -16
  92. package/dist/server.js +619 -102
  93. package/dist/sha256.js +9 -9
  94. package/dist/signal.js +47 -20
  95. package/dist/site-config.d.ts +15 -0
  96. package/dist/site-config.js +28 -0
  97. package/dist/slot.d.ts +12 -0
  98. package/dist/slot.js +23 -0
  99. package/dist/ssr-core.d.ts +2 -0
  100. package/dist/ssr-core.js +12 -4
  101. package/dist/suspense.js +34 -2
  102. package/dist/testing.d.ts +62 -0
  103. package/dist/testing.js +90 -0
  104. package/dist/use-id.js +1 -1
  105. package/package.json +28 -3
  106. package/dist/action-context.test.d.ts +0 -1
  107. package/dist/action-context.test.js +0 -72
  108. package/dist/actions.test.d.ts +0 -1
  109. package/dist/actions.test.js +0 -477
  110. package/dist/cache-adapter.test.d.ts +0 -1
  111. package/dist/cache-adapter.test.js +0 -244
  112. package/dist/cache.test.d.ts +0 -1
  113. package/dist/cache.test.js +0 -233
  114. package/dist/class-component.test.d.ts +0 -1
  115. package/dist/class-component.test.js +0 -462
  116. package/dist/class-helpers.test.d.ts +0 -1
  117. package/dist/class-helpers.test.js +0 -169
  118. package/dist/context.test.d.ts +0 -1
  119. package/dist/context.test.js +0 -57
  120. package/dist/derived.test.d.ts +0 -1
  121. package/dist/derived.test.js +0 -140
  122. package/dist/dev-overlay.test.d.ts +0 -1
  123. package/dist/dev-overlay.test.js +0 -152
  124. package/dist/discrete.test.d.ts +0 -1
  125. package/dist/discrete.test.js +0 -100
  126. package/dist/dom.test.d.ts +0 -1
  127. package/dist/dom.test.js +0 -31
  128. package/dist/effect-errors.test.d.ts +0 -1
  129. package/dist/effect-errors.test.js +0 -163
  130. package/dist/effects.test.d.ts +0 -1
  131. package/dist/effects.test.js +0 -485
  132. package/dist/events.test.d.ts +0 -1
  133. package/dist/events.test.js +0 -447
  134. package/dist/handler-html.test.d.ts +0 -1
  135. package/dist/handler-html.test.js +0 -256
  136. package/dist/handler-routing.test.d.ts +0 -1
  137. package/dist/handler-routing.test.js +0 -99
  138. package/dist/handler-segments.test.d.ts +0 -1
  139. package/dist/handler-segments.test.js +0 -217
  140. package/dist/head-tags.test.d.ts +0 -1
  141. package/dist/head-tags.test.js +0 -131
  142. package/dist/highlight-bind.test.d.ts +0 -1
  143. package/dist/highlight-bind.test.js +0 -124
  144. package/dist/highlight.test.d.ts +0 -1
  145. package/dist/highlight.test.js +0 -56
  146. package/dist/lanes.test.d.ts +0 -1
  147. package/dist/lanes.test.js +0 -59
  148. package/dist/list.test.d.ts +0 -1
  149. package/dist/list.test.js +0 -344
  150. package/dist/metadata.test.d.ts +0 -1
  151. package/dist/metadata.test.js +0 -167
  152. package/dist/navigation.test.d.ts +0 -1
  153. package/dist/navigation.test.js +0 -130
  154. package/dist/node-server.test.d.ts +0 -1
  155. package/dist/node-server.test.js +0 -492
  156. package/dist/optimistic-lanes.test.d.ts +0 -1
  157. package/dist/optimistic-lanes.test.js +0 -45
  158. package/dist/patterns.test.d.ts +0 -1
  159. package/dist/patterns.test.js +0 -98
  160. package/dist/portal.test.d.ts +0 -1
  161. package/dist/portal.test.js +0 -99
  162. package/dist/react-effects.test.d.ts +0 -1
  163. package/dist/react-effects.test.js +0 -96
  164. package/dist/react-suspense.test.d.ts +0 -1
  165. package/dist/react-suspense.test.js +0 -194
  166. package/dist/react.test.d.ts +0 -1
  167. package/dist/react.test.js +0 -60
  168. package/dist/react19.test.d.ts +0 -1
  169. package/dist/react19.test.js +0 -176
  170. package/dist/reactivity-core.test.d.ts +0 -1
  171. package/dist/reactivity-core.test.js +0 -152
  172. package/dist/reconcile.test.d.ts +0 -1
  173. package/dist/reconcile.test.js +0 -110
  174. package/dist/refs.test.d.ts +0 -1
  175. package/dist/refs.test.js +0 -257
  176. package/dist/router-layout.test.d.ts +0 -1
  177. package/dist/router-layout.test.js +0 -249
  178. package/dist/router-nav.test.d.ts +0 -1
  179. package/dist/router-nav.test.js +0 -464
  180. package/dist/router-slots.test.d.ts +0 -1
  181. package/dist/router-slots.test.js +0 -144
  182. package/dist/router-speculation.test.d.ts +0 -1
  183. package/dist/router-speculation.test.js +0 -25
  184. package/dist/router-styles.test.d.ts +0 -1
  185. package/dist/router-styles.test.js +0 -98
  186. package/dist/router.test.d.ts +0 -1
  187. package/dist/router.test.js +0 -36
  188. package/dist/server-async.test.d.ts +0 -1
  189. package/dist/server-async.test.js +0 -216
  190. package/dist/server-handler.test.d.ts +0 -1
  191. package/dist/server-handler.test.js +0 -535
  192. package/dist/server-highlight.test.d.ts +0 -1
  193. package/dist/server-highlight.test.js +0 -32
  194. package/dist/server-renderer.test.d.ts +0 -1
  195. package/dist/server-renderer.test.js +0 -266
  196. package/dist/server.test.d.ts +0 -1
  197. package/dist/server.test.js +0 -88
  198. package/dist/sha256.test.d.ts +0 -1
  199. package/dist/sha256.test.js +0 -13
  200. package/dist/signal-lanes.test.d.ts +0 -1
  201. package/dist/signal-lanes.test.js +0 -32
  202. package/dist/signal.test.d.ts +0 -1
  203. package/dist/signal.test.js +0 -68
  204. package/dist/slots.test.d.ts +0 -1
  205. package/dist/slots.test.js +0 -169
  206. package/dist/snapshots.test.d.ts +0 -1
  207. package/dist/snapshots.test.js +0 -208
  208. package/dist/ssr-core-behavior.test.d.ts +0 -1
  209. package/dist/ssr-core-behavior.test.js +0 -620
  210. package/dist/ssr-core.test.d.ts +0 -1
  211. package/dist/ssr-core.test.js +0 -324
  212. package/dist/static-memory.test.d.ts +0 -1
  213. package/dist/static-memory.test.js +0 -26
  214. package/dist/storage.test.d.ts +0 -1
  215. package/dist/storage.test.js +0 -46
  216. package/dist/suspense-actions.test.d.ts +0 -1
  217. package/dist/suspense-actions.test.js +0 -72
  218. package/dist/suspense.test.d.ts +0 -1
  219. package/dist/suspense.test.js +0 -518
package/dist/sha256.js CHANGED
@@ -1,14 +1,14 @@
1
1
  // Portable synchronous SHA-256 (no node:crypto, no Web Crypto) so the render
2
2
  // core can compute the same error digest on Node, Workers and edge hosts.
3
3
  const K = new Uint32Array([
4
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
5
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
6
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
7
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
8
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
9
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
10
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
11
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
4
+ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
5
+ 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
6
+ 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
7
+ 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
8
+ 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
9
+ 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
10
+ 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
11
+ 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
12
12
  ]);
13
13
  export function sha256Hex(input) {
14
14
  const msg = new TextEncoder().encode(input);
@@ -21,7 +21,7 @@ export function sha256Hex(input) {
21
21
  view.setUint32(total - 8, Math.floor(bitLen / 0x100000000), false);
22
22
  view.setUint32(total - 4, bitLen >>> 0, false);
23
23
  const h = new Uint32Array([
24
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
24
+ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
25
25
  ]);
26
26
  const w = new Uint32Array(64);
27
27
  const rotr = (x, n) => (x >>> n) | (x << (32 - n));
package/dist/signal.js CHANGED
@@ -80,7 +80,6 @@ export function krateTagOwner(owner, scope) {
80
80
  // subscriber re-runs into a single flush so N writes in a loop (or one write
81
81
  // fanning out) produce one pass instead of N synchronous effect runs.
82
82
  const pending = [];
83
- let flushScheduled = false;
84
83
  function newEffectState(fn) {
85
84
  const scope = krateCurrentScope();
86
85
  const effect = { fn, cleanups: [], disposed: false, sources: new Set(), running: false, rerun: false, queued: false, owner: currentOwner, boundaryOwner: ownerHooks ? ownerHooks.current() : null, scope };
@@ -106,7 +105,7 @@ export function krateDisposeOwner(owner) {
106
105
  ownerEffects.delete(owner);
107
106
  if (effects) {
108
107
  for (const effect of effects) {
109
- disposeEffect(effect);
108
+ disposeEffectQuietly(effect);
110
109
  allEffects.delete(effect);
111
110
  }
112
111
  }
@@ -341,6 +340,31 @@ function drainAfterQueue() {
341
340
  export function sameValue(a, b) {
342
341
  return Object.is(a, b);
343
342
  }
343
+ /**
344
+ * Runs every cleanup in order even when one throws, so a failing cleanup
345
+ * cannot strand the listeners and timers registered after it; the first error
346
+ * is rethrown once all have run, the others are logged.
347
+ */
348
+ function runCleanups(list) {
349
+ let failed = false;
350
+ let first;
351
+ for (const cleanup of list.splice(0)) {
352
+ try {
353
+ cleanup();
354
+ }
355
+ catch (err) {
356
+ if (!failed) {
357
+ failed = true;
358
+ first = err;
359
+ }
360
+ else if (typeof console !== 'undefined') {
361
+ console.error(err);
362
+ }
363
+ }
364
+ }
365
+ if (failed)
366
+ throw first;
367
+ }
344
368
  function disposeEffect(effect) {
345
369
  if (!effect.disposed) {
346
370
  effect.disposed = true;
@@ -352,10 +376,17 @@ function disposeEffect(effect) {
352
376
  set.delete(effect);
353
377
  }
354
378
  effect.sources.clear();
355
- for (const cleanup of effect.cleanups) {
356
- cleanup();
357
- }
358
- effect.cleanups.length = 0;
379
+ runCleanups(effect.cleanups);
380
+ }
381
+ }
382
+ /** Teardown disposal: a failing cleanup is logged and never stops the sweep. */
383
+ function disposeEffectQuietly(effect) {
384
+ try {
385
+ disposeEffect(effect);
386
+ }
387
+ catch (err) {
388
+ if (typeof console !== 'undefined')
389
+ console.error(err);
359
390
  }
360
391
  }
361
392
  /**
@@ -393,13 +424,15 @@ function disposeMatching(gone, pageTeardown) {
393
424
  try {
394
425
  hook(gone);
395
426
  }
396
- catch {
427
+ catch (err) {
397
428
  // Unmount errors must not prevent the rest of the teardown.
429
+ if (typeof console !== 'undefined')
430
+ console.error(err);
398
431
  }
399
432
  }
400
433
  for (const effect of Array.from(allEffects)) {
401
434
  if (gone(effect.scope)) {
402
- disposeEffect(effect);
435
+ disposeEffectQuietly(effect);
403
436
  allEffects.delete(effect);
404
437
  }
405
438
  }
@@ -447,12 +480,14 @@ export function disposeAll() {
447
480
  try {
448
481
  hook(null);
449
482
  }
450
- catch {
483
+ catch (err) {
451
484
  // Unmount errors must not prevent the rest of the teardown.
485
+ if (typeof console !== 'undefined')
486
+ console.error(err);
452
487
  }
453
488
  }
454
489
  for (const effect of allEffects) {
455
- disposeEffect(effect);
490
+ disposeEffectQuietly(effect);
456
491
  }
457
492
  allEffects.clear();
458
493
  for (const cleanup of rootCleanups) {
@@ -488,7 +523,6 @@ export function disposeAll() {
488
523
  }
489
524
  pending.length = 0;
490
525
  afterQueue.length = 0;
491
- flushScheduled = false;
492
526
  flushMode = 0;
493
527
  flushToken++;
494
528
  syncLane = false;
@@ -825,7 +859,6 @@ function scheduleFlush(reactWrite = false) {
825
859
  const micro = !later || discrete || committing > 0 || renderDepth > 0 || typeof setTimeout !== 'function';
826
860
  if (flushMode === 1 || (!micro && flushMode === 2))
827
861
  return;
828
- flushScheduled = true;
829
862
  const token = ++flushToken;
830
863
  const run = () => {
831
864
  if (token === flushToken)
@@ -851,7 +884,6 @@ function flushEffects() {
851
884
  }
852
885
  }
853
886
  function flushEffectsInner() {
854
- flushScheduled = false;
855
887
  flushMode = 0;
856
888
  flushToken++;
857
889
  // One effect throwing must not strand the effects queued behind it: their
@@ -915,7 +947,6 @@ function flushEffectsInner() {
915
947
  if (pending.length === 0) {
916
948
  // The cascade is done: a flush the passes scheduled has nothing to do.
917
949
  flushMode = 0;
918
- flushScheduled = false;
919
950
  flushToken++;
920
951
  }
921
952
  if (pending.length === 0) {
@@ -973,7 +1004,6 @@ function abortUpdateLoop() {
973
1004
  const looping = pending.splice(0);
974
1005
  for (const effect of looping)
975
1006
  effect.queued = false;
976
- flushScheduled = false;
977
1007
  flushMode = 0;
978
1008
  flushToken++;
979
1009
  const culprits = looping.length > 0 ? looping : lastPassEffects;
@@ -1053,9 +1083,7 @@ function runEffect(effect) {
1053
1083
  snapshotEpoch = null;
1054
1084
  try {
1055
1085
  // Run cleanups from previous execution
1056
- for (const cleanup of effect.cleanups.splice(0)) {
1057
- cleanup();
1058
- }
1086
+ runCleanups(effect.cleanups);
1059
1087
  // Re-track from scratch so a dependency the last run did not read stops
1060
1088
  // re-running the effect.
1061
1089
  for (const set of effect.sources) {
@@ -1234,8 +1262,7 @@ export function createMemo(fn) {
1234
1262
  const node = newEffectState(() => { });
1235
1263
  allEffects.add(node);
1236
1264
  const recompute = () => {
1237
- for (const cleanup of node.cleanups.splice(0))
1238
- cleanup();
1265
+ runCleanups(node.cleanups);
1239
1266
  for (const source of node.sources)
1240
1267
  source.delete(node);
1241
1268
  node.sources.clear();
@@ -0,0 +1,15 @@
1
+ /** Site config injected by the compiler. */
2
+ export interface KrateRuntimeConfig {
3
+ basePath?: string;
4
+ viewTransitions?: boolean | 'auto';
5
+ /** Milliseconds a prefetched document stays fresh (default 30000). */
6
+ prefetchTTL?: number;
7
+ /** Default link prefetch: "viewport" also prefetches links as they scroll into view. */
8
+ prefetch?: 'intent' | 'viewport';
9
+ /** Set by `krate dev`: enables router dev warnings. */
10
+ dev?: boolean;
11
+ }
12
+ /** Id of the JSON data block the compiler writes. */
13
+ export declare const CFG_BLOCK_ID = "__krate_cfg__";
14
+ /** The site config, read once from the head's JSON block when the global is not set. */
15
+ export declare function siteConfig(): KrateRuntimeConfig;
@@ -0,0 +1,28 @@
1
+ // The site runtime config (`__KRATE_CFG__`). The compiler writes it into the
2
+ // page head as a JSON data block rather than an inline script, so a strict
3
+ // `script-src 'self'` policy needs no allowance for it. Server bundles set the
4
+ // global directly. The block is only present when something differs from the
5
+ // defaults (a base path, View Transitions off, dev mode).
6
+ /** Id of the JSON data block the compiler writes. */
7
+ export const CFG_BLOCK_ID = '__krate_cfg__';
8
+ /** The site config, read once from the head's JSON block when the global is not set. */
9
+ export function siteConfig() {
10
+ const g = globalThis;
11
+ if (g.__KRATE_CFG__)
12
+ return g.__KRATE_CFG__;
13
+ const doc = g.document;
14
+ const el = doc && typeof doc.getElementById === 'function' ? doc.getElementById(CFG_BLOCK_ID) : null;
15
+ if (el) {
16
+ try {
17
+ const parsed = JSON.parse(el.textContent || '{}');
18
+ if (parsed && typeof parsed === 'object') {
19
+ g.__KRATE_CFG__ = parsed;
20
+ return parsed;
21
+ }
22
+ }
23
+ catch {
24
+ // a malformed block falls back to the defaults
25
+ }
26
+ }
27
+ return {};
28
+ }
package/dist/slot.d.ts CHANGED
@@ -5,4 +5,16 @@ export declare const Root: typeof KrateSlot;
5
5
  export declare function Slottable(props: {
6
6
  children?: unknown;
7
7
  }): unknown;
8
+ type SlotProps = Record<string, unknown> & {
9
+ children?: unknown;
10
+ };
11
+ /**
12
+ * Radix's `createSlot(ownerName)` (react-slot >= 1.2): a Slot that merges its
13
+ * props and `ref` onto its single child. `ownerName` only labels the component.
14
+ */
15
+ export declare function createSlot(ownerName: string): (props: SlotProps) => Node | null;
16
+ /** Radix's `createSlottable(ownerName)`: a `Slottable` marker for one owner. */
17
+ export declare function createSlottable(ownerName: string): (props: {
18
+ children?: unknown;
19
+ }) => unknown;
8
20
  export default Slot;
package/dist/slot.js CHANGED
@@ -8,4 +8,27 @@ export const Root = KrateSlot;
8
8
  export function Slottable(props) {
9
9
  return props?.children;
10
10
  }
11
+ /**
12
+ * Radix's `createSlot(ownerName)` (react-slot >= 1.2): a Slot that merges its
13
+ * props and `ref` onto its single child. `ownerName` only labels the component.
14
+ */
15
+ export function createSlot(ownerName) {
16
+ const OwnerSlot = (props) => {
17
+ const node = KrateSlot(props);
18
+ const ref = props?.ref;
19
+ if (node && typeof ref === 'function')
20
+ ref(node);
21
+ else if (node && ref && typeof ref === 'object')
22
+ ref.current = node;
23
+ return node;
24
+ };
25
+ OwnerSlot.displayName = ownerName + '.Slot';
26
+ return OwnerSlot;
27
+ }
28
+ /** Radix's `createSlottable(ownerName)`: a `Slottable` marker for one owner. */
29
+ export function createSlottable(ownerName) {
30
+ const OwnerSlottable = (props) => props?.children;
31
+ OwnerSlottable.displayName = ownerName + '.Slottable';
32
+ return OwnerSlottable;
33
+ }
11
34
  export default Slot;
@@ -63,6 +63,8 @@ export interface RenderResponse {
63
63
  digest?: string;
64
64
  stack?: string;
65
65
  noStore?: boolean;
66
+ /** An error boundary caught a render error; html is its fallback and the answer is a 500. */
67
+ errorCaught?: boolean;
66
68
  }
67
69
  export declare function variantKey(req: {
68
70
  route: string;
package/dist/ssr-core.js CHANGED
@@ -30,7 +30,7 @@ export function newQueryTracker() {
30
30
  return { keys: new Set(), all: false };
31
31
  }
32
32
  export function trackedQuery(query, t) {
33
- const target = { ...(query || {}) };
33
+ const target = { ...query };
34
34
  const note = (prop) => {
35
35
  if (typeof prop === "string" && prop !== "then" && prop !== "toJSON" && prop !== "constructor")
36
36
  t.keys.add(prop);
@@ -369,8 +369,8 @@ export function createSSRCore(opts) {
369
369
  method: req.method || "GET",
370
370
  headers: h,
371
371
  cookies: parseCookieHeader(h.get("cookie")),
372
- params: { ...(req.params || {}) },
373
- searchParams: tracker ? trackedQuery(req.query, tracker) : { ...(req.query || {}) },
372
+ params: { ...req.params },
373
+ searchParams: tracker ? trackedQuery(req.query, tracker) : { ...req.query },
374
374
  };
375
375
  }
376
376
  function withRequest(req, fn, tracker) {
@@ -449,6 +449,12 @@ export function createSSRCore(opts) {
449
449
  status: 200,
450
450
  noStore: scope.noStore || invalidated,
451
451
  };
452
+ if (scope.errorCaught) {
453
+ // An error boundary rendered its fallback: the page is an error
454
+ // response, so it is never cached (scope.noStore keeps it out of ISR).
455
+ response.errorCaught = true;
456
+ response.noStore = true;
457
+ }
452
458
  // An uncacheable read (no-store) keeps the render out of the ISR cache, and
453
459
  // a revalidation that landed mid-render means the HTML may be stale.
454
460
  if (page.mode === "isr" && !scope.noStore && !invalidated) {
@@ -585,7 +591,7 @@ export function createSSRCore(opts) {
585
591
  // defines globalThis.__krate_render(propsJSON)) with the build-time baked props,
586
592
  // so nothing is re-derived at request time.
587
593
  function regionProps(region, req) {
588
- const props = { ...(region.props || {}) };
594
+ const props = { ...region.props };
589
595
  if (req.params && Object.keys(req.params).length > 0)
590
596
  props.params = req.params;
591
597
  if (req.query && Object.keys(req.query).length > 0)
@@ -865,6 +871,8 @@ export function createSSRCore(opts) {
865
871
  frame.stack = result.stack;
866
872
  if (result.noStore)
867
873
  frame.noStore = true;
874
+ if (result.errorCaught)
875
+ frame.errorCaught = true;
868
876
  if (page.serverComponents && frame.status >= 500)
869
877
  frame.fatal = true;
870
878
  // The page's <Head> children (title, meta, link, script) render in place;
package/dist/suspense.js CHANGED
@@ -23,7 +23,7 @@
23
23
  * content keeps showing it when a transition update suspends it, and the
24
24
  * transition stays pending until that work and any async action settle.
25
25
  */
26
- import { createSignal, createEffect, onCleanup, krateGetOwner, krateSetOwner } from './signal.js';
26
+ import { createSignal, createEffect, onCleanup, krateGetOwner, krateSetOwner, krateOnDispose, krateCurrentScope } from './signal.js';
27
27
  const tracked = new WeakSet();
28
28
  export function isThenable(value) {
29
29
  return (value != null &&
@@ -369,6 +369,9 @@ class BoundaryController {
369
369
  this.pending = new Set();
370
370
  this.holds = 0;
371
371
  this.scheduled = false;
372
+ /** Route scope the boundary belongs to, and whether its page is gone. */
373
+ this.route = krateCurrentScope();
374
+ this.dead = false;
372
375
  this.throttle = null;
373
376
  this.id = id;
374
377
  this.anchor = anchor;
@@ -420,6 +423,10 @@ class BoundaryController {
420
423
  });
421
424
  }
422
425
  update() {
426
+ // A boundary of a page that was navigated away from must not touch the DOM
427
+ // or the slot state of the page that replaced it.
428
+ if (this.dead)
429
+ return;
423
430
  const want = this.pending.size === 0 && this.holds === 0;
424
431
  if (want === this.content)
425
432
  return;
@@ -609,6 +616,23 @@ export function registerBoundary(boundary) {
609
616
  export function resetBoundaries() {
610
617
  boundaries.clear();
611
618
  }
619
+ // Suspended component runs and compiled boundaries are retried when their
620
+ // promise settles, which can be after the user left the page: a retry would
621
+ // mount the effects of a page that is gone. A page teardown (soft navigation)
622
+ // marks the ones it removed dead.
623
+ const liveRuns = new Set();
624
+ krateOnDispose((gone) => {
625
+ for (const run of liveRuns) {
626
+ if (gone === null || gone(run.route))
627
+ run.dead = true;
628
+ }
629
+ for (const [id, b] of Array.from(boundaries)) {
630
+ if (b instanceof BoundaryController && (gone === null || gone(b.route))) {
631
+ b.dead = true;
632
+ boundaries.delete(id);
633
+ }
634
+ }
635
+ });
612
636
  /**
613
637
  * Suspends `boundary` on `t`. Inside a transition an already revealed
614
638
  * boundary keeps its content and the transition waits instead; with no
@@ -634,10 +658,15 @@ export function currentBoundary() {
634
658
  */
635
659
  export function __krateBoundary(id, fn) {
636
660
  const boundary = id ? getBoundary(id) : null;
637
- const state = { boundary, scope: { list: [], i: 0 }, disposers: [] };
661
+ const state = { boundary, scope: { list: [], i: 0 }, disposers: [], route: krateCurrentScope(), dead: false };
638
662
  const errOwner = errorOwner();
639
663
  boundary?.hold();
640
664
  const attempt = () => {
665
+ // The page was left while the promise was pending: do not mount it now.
666
+ if (state.dead) {
667
+ liveRuns.delete(state);
668
+ return;
669
+ }
641
670
  state.scope.i = 0;
642
671
  const prev = currentRun;
643
672
  currentRun = state;
@@ -649,6 +678,7 @@ export function __krateBoundary(id, fn) {
649
678
  withReadScope(state.scope, fn);
650
679
  currentRun = prev;
651
680
  krateSetOwner(owner);
681
+ liveRuns.delete(state);
652
682
  boundary?.release();
653
683
  }
654
684
  catch (error) {
@@ -657,9 +687,11 @@ export function __krateBoundary(id, fn) {
657
687
  for (const dispose of state.disposers.splice(0))
658
688
  dispose();
659
689
  if (isSuspenseSignal(error)) {
690
+ liveRuns.add(state);
660
691
  error.thenable.then(attempt, attempt);
661
692
  return;
662
693
  }
694
+ liveRuns.delete(state);
663
695
  boundary?.release();
664
696
  reportSuspenseError(error, boundary, errOwner);
665
697
  }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Test helpers, exported from `@krate/runtime/testing`. They call an API route
3
+ * module or a server action in-process, without a running server. Node only
4
+ * (it uses AsyncLocalStorage); import it from your test files, never from app
5
+ * code.
6
+ */
7
+ import type { KrateRequest } from "./after.js";
8
+ export type RouteMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
9
+ export type RouteHandler = (request: KrateRequest, context: {
10
+ params: Record<string, string>;
11
+ }) => Response | Promise<Response>;
12
+ /** An imported `src/api/*.ts` module: named method exports and/or a default Web handler. */
13
+ export type RouteModule = Partial<Record<RouteMethod, RouteHandler>> & {
14
+ default?: unknown;
15
+ };
16
+ export interface CallRouteOptions {
17
+ /** Dynamic segments of the route (`[id]` -> `{ id: "7" }`). */
18
+ params?: Record<string, string>;
19
+ headers?: HeadersInit;
20
+ /** A JSON body; sets `content-type: application/json`. */
21
+ json?: unknown;
22
+ /** A raw body (string, FormData, ...). Ignored when `json` is set. */
23
+ body?: BodyInit | null;
24
+ /** The site base path, reported by request.nextUrl.basePath. */
25
+ basePath?: string;
26
+ }
27
+ /**
28
+ * Calls a route module the way the server does: the named export for `method`
29
+ * (HEAD falls back to GET without a body, OPTIONS answers `Allow`), else a
30
+ * default Web-style handler, else a 405 with `Allow`. `target` is a path
31
+ * (`"/api/users/7?x=1"`), an absolute URL, or a ready `Request`.
32
+ */
33
+ export declare function callRoute(mod: RouteModule, method: RouteMethod, target: string | Request, options?: CallRouteOptions): Promise<Response>;
34
+ export interface ActionCallOptions {
35
+ /** Request headers the action sees through `headers()`. */
36
+ headers?: HeadersInit;
37
+ /** Cookies the request carried; the action reads them with `cookies()`. */
38
+ cookies?: Record<string, string>;
39
+ /** The page URL path the action was submitted from. */
40
+ url?: string;
41
+ /** The action id; only visible through `getActionContext()`. */
42
+ actionId?: string;
43
+ }
44
+ export interface SetCookieCall {
45
+ name: string;
46
+ /** Empty string for a delete. */
47
+ value: string;
48
+ options?: Record<string, unknown>;
49
+ deleted: boolean;
50
+ }
51
+ export interface ActionCallResult<T> {
52
+ /** What the action returned. A thrown error (including `redirect()`) rejects instead. */
53
+ result: T;
54
+ /** Every `cookies().set()` / `delete()` call, in order. */
55
+ setCookies: SetCookieCall[];
56
+ }
57
+ /**
58
+ * Runs a server action function with the request context a real call has, so
59
+ * `cookies()`, `headers()` and `getActionContext()` from `@krate/runtime/server`
60
+ * work. The action is the plain exported function you imported from the module.
61
+ */
62
+ export declare function runAction<T>(action: () => T | Promise<T>, options?: ActionCallOptions): Promise<ActionCallResult<T>>;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Test helpers, exported from `@krate/runtime/testing`. They call an API route
3
+ * module or a server action in-process, without a running server. Node only
4
+ * (it uses AsyncLocalStorage); import it from your test files, never from app
5
+ * code.
6
+ */
7
+ import { AsyncLocalStorage } from "node:async_hooks";
8
+ import { attachCookies } from "./request-extras.js";
9
+ const METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
10
+ /**
11
+ * Calls a route module the way the server does: the named export for `method`
12
+ * (HEAD falls back to GET without a body, OPTIONS answers `Allow`), else a
13
+ * default Web-style handler, else a 405 with `Allow`. `target` is a path
14
+ * (`"/api/users/7?x=1"`), an absolute URL, or a ready `Request`.
15
+ */
16
+ export async function callRoute(mod, method, target, options = {}) {
17
+ // The server hands routes a Request with request.cookies and request.nextUrl.
18
+ const request = attachCookies(toRequest(method, target, options), options.basePath ?? "");
19
+ const context = { params: options.params ?? {} };
20
+ const named = (m) => (typeof mod[m] === "function" ? mod[m] : undefined);
21
+ const fallback = typeof mod.default === "function" ? mod.default : undefined;
22
+ if (fallback && fallback.length >= 2) {
23
+ throw new TypeError("callRoute: a default export with a (req, res) signature is the legacy Node style and is not supported; export named GET/POST handlers instead");
24
+ }
25
+ const direct = named(method);
26
+ if (direct)
27
+ return direct(request, context);
28
+ if (method === "HEAD" && named("GET")) {
29
+ const res = await named("GET")(request, context);
30
+ return new Response(null, { status: res.status, statusText: res.statusText, headers: res.headers });
31
+ }
32
+ if (fallback && !METHODS.some((m) => named(m)))
33
+ return (await fallback(request, context));
34
+ const allow = METHODS.filter((m) => named(m) || (m === "HEAD" && named("GET")));
35
+ if (!allow.includes("OPTIONS"))
36
+ allow.push("OPTIONS");
37
+ if (method === "OPTIONS")
38
+ return new Response(null, { status: 204, headers: { Allow: allow.join(", ") } });
39
+ return new Response("Method Not Allowed", { status: 405, headers: { Allow: allow.join(", ") } });
40
+ }
41
+ function toRequest(method, target, options) {
42
+ if (typeof target !== "string")
43
+ return target;
44
+ const url = /^https?:\/\//i.test(target) ? target : "http://localhost" + (target.startsWith("/") ? target : "/" + target);
45
+ const headers = new Headers(options.headers);
46
+ let body = options.body;
47
+ if (options.json !== undefined) {
48
+ body = JSON.stringify(options.json);
49
+ if (!headers.has("content-type"))
50
+ headers.set("content-type", "application/json");
51
+ }
52
+ const hasBody = method !== "GET" && method !== "HEAD" && body !== undefined && body !== null;
53
+ return new Request(url, { method, headers, body: hasBody ? body : undefined });
54
+ }
55
+ const ACTION_STORE = Symbol.for("krate.action.store");
56
+ const als = new AsyncLocalStorage();
57
+ /**
58
+ * Runs a server action function with the request context a real call has, so
59
+ * `cookies()`, `headers()` and `getActionContext()` from `@krate/runtime/server`
60
+ * work. The action is the plain exported function you imported from the module.
61
+ */
62
+ export async function runAction(action, options = {}) {
63
+ const jar = new Map(Object.entries(options.cookies ?? {}));
64
+ const setCookies = [];
65
+ const cookies = {
66
+ get: (name) => jar.get(name),
67
+ getAll: () => Array.from(jar, ([name, value]) => ({ name, value })),
68
+ has: (name) => jar.has(name),
69
+ set: (name, value, opts) => {
70
+ setCookies.push({ name, value: String(value), options: opts, deleted: false });
71
+ jar.set(name, String(value));
72
+ },
73
+ delete: (name, opts) => {
74
+ setCookies.push({ name, value: "", options: opts, deleted: true });
75
+ jar.delete(name);
76
+ },
77
+ };
78
+ const context = {
79
+ actionId: options.actionId ?? "test-action",
80
+ url: options.url ?? "/",
81
+ method: "POST",
82
+ headers: new Headers(options.headers),
83
+ cookies,
84
+ };
85
+ // Published once and left in place: concurrent runAction calls share it, and
86
+ // each call's context lives in its own async scope.
87
+ globalThis[ACTION_STORE] = als;
88
+ const result = await als.run(context, async () => action());
89
+ return { result, setCookies };
90
+ }
package/dist/use-id.js CHANGED
@@ -11,7 +11,7 @@ const encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
11
11
  */
12
12
  function idSafe(s) {
13
13
  return s.replace(/[^A-Za-z0-9_.:$-]/gu, (c) => {
14
- const bytes = encoder ? Array.from(encoder.encode(c)) : [c.charCodeAt(0) & 0xff];
14
+ const bytes = encoder ? Array.from(encoder.encode(c)) : [c.charCodeAt(0) & 0xFF];
15
15
  return bytes.map((b) => '%' + b.toString(16).toUpperCase().padStart(2, '0')).join('');
16
16
  });
17
17
  }