@krate/runtime 0.5.0-rc.15 → 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.
Files changed (58) hide show
  1. package/NOTICE +10 -0
  2. package/README.md +92 -1
  3. package/dist/actions.js +10 -5
  4. package/dist/cache-scope.d.ts +10 -2
  5. package/dist/cache-scope.js +26 -4
  6. package/dist/context.js +3 -0
  7. package/dist/dev/app.js +6 -2
  8. package/dist/dev/modal.js +9 -9
  9. package/dist/dev/panel.js +3 -1
  10. package/dist/dev/sourcemap.d.ts +5 -0
  11. package/dist/dev/sourcemap.js +36 -2
  12. package/dist/dev/util.d.ts +6 -0
  13. package/dist/dev/util.js +1 -0
  14. package/dist/dom.d.ts +26 -0
  15. package/dist/dom.js +78 -5
  16. package/dist/flight-props.d.ts +8 -0
  17. package/dist/flight-props.js +44 -0
  18. package/dist/hints.d.ts +12 -0
  19. package/dist/hints.js +32 -2
  20. package/dist/index.d.ts +4 -0
  21. package/dist/island-swap.js +46 -8
  22. package/dist/krate-core.js +1 -1
  23. package/dist/krate-dev.js +14 -14
  24. package/dist/krate-f-actions.js +1 -1
  25. package/dist/krate-f-list.js +1 -1
  26. package/dist/krate-f-navigation.js +1 -1
  27. package/dist/krate-f-router.js +1 -1
  28. package/dist/krate-features.json +32 -1
  29. package/dist/krate-hydrate-lite.js +5 -5
  30. package/dist/krate-hydrate.js +5 -5
  31. package/dist/krate-island-swap.js +1 -1
  32. package/dist/krate-runtime.js +5 -5
  33. package/dist/list.js +8 -3
  34. package/dist/memsize.d.ts +14 -0
  35. package/dist/memsize.js +71 -0
  36. package/dist/metadata.js +20 -5
  37. package/dist/middleware-cookies.d.ts +4 -0
  38. package/dist/middleware-cookies.js +67 -0
  39. package/dist/navigation-react.js +2 -1
  40. package/dist/navigation.d.ts +7 -0
  41. package/dist/navigation.js +26 -1
  42. package/dist/react-compat.js +9 -0
  43. package/dist/react.d.ts +1 -1
  44. package/dist/react.js +5 -0
  45. package/dist/router.js +55 -9
  46. package/dist/segmented-lru.d.ts +14 -1
  47. package/dist/segmented-lru.js +90 -21
  48. package/dist/server-handler.d.ts +1 -0
  49. package/dist/server-handler.js +100 -6
  50. package/dist/server-renderer.js +4 -1
  51. package/dist/server.d.ts +6 -3
  52. package/dist/server.js +78 -38
  53. package/dist/shared-isr.js +2 -1
  54. package/dist/signal.js +13 -3
  55. package/dist/slots.js +8 -1
  56. package/dist/ssr-core.d.ts +10 -2
  57. package/dist/ssr-core.js +19 -6
  58. package/package.json +3 -2
@@ -1,4 +1,5 @@
1
1
  // The ISR page cache on a shared cache adapter (see cache-adapter.ts).
2
+ import { cacheByteBudgets } from "./memsize.js";
2
3
  import { MemoryIsrStore, variantKey } from './ssr-core.js';
3
4
  import { installCacheAdapter, loadCacheAdapter, tagName } from './cache-adapter.js';
4
5
  const ISR_PREFIX = 'isr:';
@@ -11,7 +12,7 @@ export class SharedIsrStore {
11
12
  constructor(guard, ttlSeconds = 7 * 24 * 3600, maxMemory = 512) {
12
13
  this.guard = guard;
13
14
  this.ttlSeconds = ttlSeconds;
14
- this.memory = new MemoryIsrStore(maxMemory);
15
+ this.memory = new MemoryIsrStore(maxMemory, cacheByteBudgets().pages);
15
16
  }
16
17
  async get(key) {
17
18
  const r = await this.guard.call((a) => a.get(ISR_PREFIX + key));
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
@@ -80,11 +80,14 @@ export interface QueryTracker {
80
80
  }
81
81
  export declare function newQueryTracker(): QueryTracker;
82
82
  export declare function trackedQuery(query: Record<string, string> | undefined, t: QueryTracker): Record<string, string>;
83
+ export declare function cacheEntryBytes(e: CacheEntry): number;
83
84
  export declare class ISRCache {
84
85
  private cache;
85
86
  private byTag;
86
87
  private revalidationIntervals;
87
- constructor(maxSize?: number);
88
+ constructor(maxSize?: number, maxBytes?: number);
89
+ /** The approximate memory the cached pages take, in bytes. */
90
+ get bytes(): number;
88
91
  setRevalidation(route: string, seconds: number): void;
89
92
  get(key: string): CacheEntry | undefined;
90
93
  set(key: string, entry: CacheEntry): void;
@@ -116,6 +119,11 @@ export interface SSRCoreOptions {
116
119
  loadRuntimeRenderer(relPath: string): (props: string | Record<string, any>) => string | Promise<string>;
117
120
  /** ISR/region cache entry bound (default 512). */
118
121
  maxCacheSize?: number;
122
+ /**
123
+ * Memory budget of the in-memory render caches in bytes (default 64 MiB):
124
+ * rendered pages get half, PPR regions a quarter (see cacheByteBudgets).
125
+ */
126
+ cacheBytes?: number;
119
127
  /** Per-render timeout in ms (default 5000, 0 disables). */
120
128
  timeoutMs?: number;
121
129
  ppr?: boolean;
@@ -151,7 +159,7 @@ export interface IsrStore {
151
159
  /** The in-process IsrStore: a bounded map with a tag index. */
152
160
  export declare class MemoryIsrStore implements IsrStore {
153
161
  readonly cache: ISRCache;
154
- constructor(maxSize?: number);
162
+ constructor(maxSize?: number, maxBytes?: number);
155
163
  get(key: string): CacheEntry | undefined;
156
164
  set(key: string, entry: CacheEntry): void;
157
165
  deleteRoute(route: string): number;
package/dist/ssr-core.js CHANGED
@@ -9,6 +9,7 @@ import { sha256Hex as portableSha256Hex } from "./sha256.js";
9
9
  import { isNotFoundSignal, isDynamicUsageError, dynamicUsageError, dynamicUsageReason, accessDeniedStatus, parseCookieHeader, redirectTarget, redirectStatus, thenable, thenableRecord, } from "./request-context.js";
10
10
  import { HEAD_CLOSE, HEAD_OPEN, takeHead } from "./head-tags.js";
11
11
  import { metadataHeadTags } from "./metadata.js";
12
+ import { approxBytes, cacheByteBudgets } from "./memsize.js";
12
13
  import { SegmentedLRU } from "./segmented-lru.js";
13
14
  import { cacheRuntime, routeDataTag, withCacheScope, } from "./cache-scope.js";
14
15
  // ISR Cache (variant-aware, SWR, persisted)
@@ -54,11 +55,20 @@ export function trackedQuery(query, t) {
54
55
  },
55
56
  });
56
57
  }
58
+ // cacheEntryBytes is the approximate size of a cached page: its HTML plus
59
+ // the small records kept with it.
60
+ export function cacheEntryBytes(e) {
61
+ return e.html.length + approxBytes({ params: e.params, query: e.query, tags: e.tags }) + 64;
62
+ }
57
63
  export class ISRCache {
58
- constructor(maxSize = 512) {
64
+ constructor(maxSize = 512, maxBytes = 0) {
59
65
  this.byTag = new Map();
60
66
  this.revalidationIntervals = new Map(); // route -> seconds
61
- this.cache = new SegmentedLRU(maxSize);
67
+ this.cache = new SegmentedLRU(maxSize, maxBytes > 0 ? { maxBytes, sizeOf: cacheEntryBytes } : undefined);
68
+ }
69
+ /** The approximate memory the cached pages take, in bytes. */
70
+ get bytes() {
71
+ return this.cache.bytes;
62
72
  }
63
73
  setRevalidation(route, seconds) {
64
74
  this.revalidationIntervals.set(route, seconds);
@@ -185,8 +195,8 @@ export class ISRCache {
185
195
  }
186
196
  /** The in-process IsrStore: a bounded map with a tag index. */
187
197
  export class MemoryIsrStore {
188
- constructor(maxSize = 512) {
189
- this.cache = new ISRCache(maxSize);
198
+ constructor(maxSize = 512, maxBytes = 0) {
199
+ this.cache = new ISRCache(maxSize, maxBytes);
190
200
  }
191
201
  get(key) {
192
202
  return this.cache.get(key);
@@ -295,7 +305,7 @@ export function createSSRCore(opts) {
295
305
  }
296
306
  // ISR cache size and the render timeout are configurable via the compiler
297
307
  // (ssr.maxCacheSize / ssr.timeout), which exports them to the sidecar env.
298
- const isrStore = opts.isrStore || new MemoryIsrStore(opts.maxCacheSize && opts.maxCacheSize > 0 ? opts.maxCacheSize : 512);
308
+ const isrStore = opts.isrStore || new MemoryIsrStore(opts.maxCacheSize && opts.maxCacheSize > 0 ? opts.maxCacheSize : 512, cacheByteBudgets(opts.cacheBytes).pages);
299
309
  // pageIntervals remembers each ISR page's revalidate window (seconds).
300
310
  const pageIntervals = new Map();
301
311
  // entryStale: the entry is older than its page window, or than the smaller
@@ -638,7 +648,10 @@ export function createSSRCore(opts) {
638
648
  // Bounded like the page cache, and with the same segmented eviction: a
639
649
  // client minting params variants cannot push out the regions hit twice.
640
650
  const REGION_CACHE_MAX = opts.maxCacheSize && opts.maxCacheSize > 0 ? opts.maxCacheSize : 512;
641
- const regionCache = new SegmentedLRU(REGION_CACHE_MAX);
651
+ const regionCache = new SegmentedLRU(REGION_CACHE_MAX, {
652
+ maxBytes: cacheByteBudgets(opts.cacheBytes).regions,
653
+ sizeOf: (e) => e.html.length + e.route.length + e.paramsKey.length + approxBytes(e.tags) + 64,
654
+ });
642
655
  const inFlightRegionRevalidations = new Map();
643
656
  function paramsKeyOf(params) {
644
657
  return variantKey({ route: "", params });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krate/runtime",
3
- "version": "0.5.0-rc.15",
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",
@@ -125,7 +125,8 @@
125
125
  },
126
126
  "files": [
127
127
  "dist",
128
- "!dist/**/*.test.*"
128
+ "!dist/**/*.test.*",
129
+ "NOTICE"
129
130
  ],
130
131
  "devDependencies": {
131
132
  "@types/node": "^26.2.0",