@krate/runtime 0.5.0-rc.14 → 0.5.0-rc.16
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/NOTICE +10 -0
- package/dist/action-context.js +5 -1
- package/dist/cache-scope.d.ts +10 -2
- package/dist/cache-scope.js +26 -4
- package/dist/dev/api.d.ts +93 -0
- package/dist/dev/api.js +2 -0
- package/dist/dev/app.d.ts +165 -0
- package/dist/dev/app.js +1204 -0
- package/dist/dev/commands.d.ts +49 -0
- package/dist/dev/commands.js +114 -0
- package/dist/dev/highlight.d.ts +4 -0
- package/dist/dev/highlight.js +126 -0
- package/dist/dev/icons.d.ts +4 -0
- package/dist/dev/icons.js +57 -0
- package/dist/dev/inspect.d.ts +55 -0
- package/dist/dev/inspect.js +256 -0
- package/dist/dev/inspector.d.ts +39 -0
- package/dist/dev/inspector.js +348 -0
- package/dist/dev/issues.d.ts +83 -0
- package/dist/dev/issues.js +166 -0
- package/dist/dev/modal.d.ts +16 -0
- package/dist/dev/modal.js +836 -0
- package/dist/dev/native.d.ts +53 -0
- package/dist/dev/native.js +95 -0
- package/dist/dev/palette.d.ts +9 -0
- package/dist/dev/palette.js +250 -0
- package/dist/dev/panel.d.ts +31 -0
- package/dist/dev/panel.js +848 -0
- package/dist/dev/requests.d.ts +36 -0
- package/dist/dev/requests.js +150 -0
- package/dist/dev/settings.d.ts +32 -0
- package/dist/dev/settings.js +78 -0
- package/dist/dev/sourcemap.d.ts +22 -0
- package/dist/dev/sourcemap.js +148 -0
- package/dist/dev/stack.d.ts +38 -0
- package/dist/dev/stack.js +115 -0
- package/dist/dev/styles.d.ts +1 -0
- package/dist/dev/styles.js +424 -0
- package/dist/dev/util.d.ts +63 -0
- package/dist/dev/util.js +190 -0
- package/dist/dev-overlay.d.ts +9 -83
- package/dist/dev-overlay.js +17 -1038
- package/dist/island-swap.js +53 -9
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +441 -871
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -1
- package/dist/krate-runtime.js +5 -5
- package/dist/list.js +23 -1
- package/dist/memsize.d.ts +14 -0
- package/dist/memsize.js +71 -0
- package/dist/metadata.js +23 -12
- package/dist/request-context.d.ts +10 -0
- package/dist/request-context.js +24 -0
- package/dist/segmented-lru.d.ts +14 -1
- package/dist/segmented-lru.js +90 -21
- package/dist/server-handler.d.ts +3 -0
- package/dist/server-handler.js +4 -2
- package/dist/server-renderer.js +10 -3
- package/dist/server.d.ts +3 -1
- package/dist/server.js +55 -29
- package/dist/shared-isr.js +2 -1
- package/dist/signal.d.ts +8 -0
- package/dist/signal.js +4 -0
- package/dist/ssr-core.d.ts +13 -2
- package/dist/ssr-core.js +63 -11
- package/package.json +3 -2
package/dist/server.js
CHANGED
|
@@ -4,7 +4,7 @@ import { ATTR_ALIASES } from './htmlspec-attributes.js';
|
|
|
4
4
|
import { withBase } from './base-path.js';
|
|
5
5
|
import { highlight as highlightCode, trimSpace } from './highlight.js';
|
|
6
6
|
import { sealBoundArgs } from './action-seal.js';
|
|
7
|
-
import { accessDeniedStatus, isNotFoundSignal, redirectTarget } from './request-context.js';
|
|
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
10
|
import { encodeProp } from './flight-props.js';
|
|
@@ -101,14 +101,19 @@ function base64url(s) {
|
|
|
101
101
|
export function Fragment(_props) {
|
|
102
102
|
return _props.children ?? [];
|
|
103
103
|
}
|
|
104
|
-
export function jsx(type, props,
|
|
104
|
+
export function jsx(type, props, key) {
|
|
105
105
|
const { children, ...rest } = props ?? {};
|
|
106
106
|
const childArray = Array.isArray(children)
|
|
107
107
|
? children.flat(Infinity)
|
|
108
108
|
: children != null
|
|
109
109
|
? [children]
|
|
110
110
|
: [];
|
|
111
|
-
|
|
111
|
+
const el = { type, props: rest, children: childArray };
|
|
112
|
+
// The automatic runtime passes the key apart; createElement leaves it in props.
|
|
113
|
+
const k = key ?? rest.key;
|
|
114
|
+
if (k != null)
|
|
115
|
+
el.key = String(k);
|
|
116
|
+
return el;
|
|
112
117
|
}
|
|
113
118
|
export const jsxs = jsx;
|
|
114
119
|
// HTML Rendering
|
|
@@ -217,7 +222,11 @@ function normalizeNode(node) {
|
|
|
217
222
|
return node;
|
|
218
223
|
const { children, ref: _ref, ...rest } = node.props ?? {};
|
|
219
224
|
const kids = Array.isArray(children) ? children.flat(Infinity) : children != null ? [children] : [];
|
|
220
|
-
|
|
225
|
+
const el = { type: node.type, props: rest, children: kids };
|
|
226
|
+
const k = node.key;
|
|
227
|
+
if (k != null)
|
|
228
|
+
el.key = String(k);
|
|
229
|
+
return el;
|
|
221
230
|
}
|
|
222
231
|
function foreignContext(type) {
|
|
223
232
|
if (type.$$typeof === R_PROVIDER)
|
|
@@ -324,7 +333,7 @@ export class ServerErrorBoundary {
|
|
|
324
333
|
this.props = props;
|
|
325
334
|
}
|
|
326
335
|
static getDerivedStateFromError(error) {
|
|
327
|
-
if (isNotFoundSignal(error) || accessDeniedStatus(error) || redirectTarget(error))
|
|
336
|
+
if (isNotFoundSignal(error) || accessDeniedStatus(error) || redirectTarget(error) || isDynamicUsageError(error))
|
|
328
337
|
throw error;
|
|
329
338
|
return { error: describeBoundaryError(error) };
|
|
330
339
|
}
|
|
@@ -515,7 +524,7 @@ async function expandComponent(node, vals) {
|
|
|
515
524
|
return { out: consumerChild(callProps)(vals?.has(ctx) ? vals.get(ctx) : ctx._currentValue), vals };
|
|
516
525
|
}
|
|
517
526
|
// A client boundary also gets the context values in scope (slotValues).
|
|
518
|
-
let out = fn.__krateClient ? fn(callProps, vals, RENDER_CALL) : fn(callProps);
|
|
527
|
+
let out = fn.__krateClient ? fn(callProps, vals, RENDER_CALL, node.key) : fn(callProps);
|
|
519
528
|
if (isThenable(out))
|
|
520
529
|
out = await out;
|
|
521
530
|
if (fn.__krateForeignProvider) {
|
|
@@ -551,7 +560,7 @@ function renderElement(el) {
|
|
|
551
560
|
throw asyncInSyncRender("A lazy component");
|
|
552
561
|
throw e;
|
|
553
562
|
}
|
|
554
|
-
return renderElement({ type: fn
|
|
563
|
+
return renderElement({ ...el, type: fn });
|
|
555
564
|
}
|
|
556
565
|
if (typeof type === "function") {
|
|
557
566
|
if (isClassComponent(type)) {
|
|
@@ -563,7 +572,9 @@ function renderElement(el) {
|
|
|
563
572
|
return renderNode(classFallback(type, instance, error));
|
|
564
573
|
}
|
|
565
574
|
}
|
|
566
|
-
const result = type.__krateClient
|
|
575
|
+
const result = type.__krateClient
|
|
576
|
+
? type({ ...props, children }, undefined, RENDER_CALL, el.key)
|
|
577
|
+
: type({ ...props, children });
|
|
567
578
|
if (isThenable(result))
|
|
568
579
|
throw asyncInSyncRender(`<${componentName(type)}>`);
|
|
569
580
|
return withProviderValue(type, props, () => renderNode(result));
|
|
@@ -836,8 +847,14 @@ export const ISLAND_CLOSE = "<!--/krate-island-->";
|
|
|
836
847
|
// islandOpen is the opening comment of a Preact island: its module, export,
|
|
837
848
|
// chunk and props as JSON. "<", ">" and "--" are escaped (they occur only in
|
|
838
849
|
// strings), so no value can end the comment or open another one.
|
|
839
|
-
function islandOpen(moduleId, exportName, src, props, ids) {
|
|
840
|
-
const
|
|
850
|
+
function islandOpen(moduleId, exportName, src, props, ids, key) {
|
|
851
|
+
const head = { m: moduleId, e: exportName, s: src, i: ids };
|
|
852
|
+
// The React key: the router keeps a keyed island's state by key, not by
|
|
853
|
+
// position, across a refresh (see island-swap.ts).
|
|
854
|
+
if (key != null)
|
|
855
|
+
head.k = key;
|
|
856
|
+
head.p = props;
|
|
857
|
+
const json = JSON.stringify(head)
|
|
841
858
|
.replace(/</g, "\\u003c")
|
|
842
859
|
.replace(/>/g, "\\u003e")
|
|
843
860
|
.replace(/--/g, "-\\u002d");
|
|
@@ -845,22 +862,28 @@ function islandOpen(moduleId, exportName, src, props, ids) {
|
|
|
845
862
|
}
|
|
846
863
|
// islandIdPrefix names the next island of a component in the render that is
|
|
847
864
|
// running: a hash of its module and export, and how many islands of that
|
|
848
|
-
// component came before it in this document
|
|
865
|
+
// component came before it in this document (for a keyed island, how many
|
|
866
|
+
// before it had its key, and a hash of the key). useId() numbers from 0 inside
|
|
849
867
|
// each island, so the prefix is what keeps ids unique across the page. The
|
|
850
|
-
// router pairs the n-th island of a component in the live page with
|
|
851
|
-
// one in the new markup,
|
|
852
|
-
//
|
|
868
|
+
// router pairs the n-th unkeyed island of a component in the live page with
|
|
869
|
+
// the n-th one in the new markup, and a keyed one with the one of the same
|
|
870
|
+
// key, so a page that a navigation swaps in never reuses the prefix of an
|
|
871
|
+
// island the swap keeps.
|
|
853
872
|
const looseIslands = new Map();
|
|
854
|
-
function
|
|
855
|
-
|
|
873
|
+
function fnv36(s) {
|
|
874
|
+
let h = 0x811C9DC5;
|
|
875
|
+
for (let i = 0; i < s.length; i++)
|
|
876
|
+
h = Math.imul(h ^ s.charCodeAt(i), 0x01000193) >>> 0;
|
|
877
|
+
return h.toString(36).padStart(7, "0");
|
|
878
|
+
}
|
|
879
|
+
function islandIdPrefix(moduleId, exportName, key) {
|
|
880
|
+
const component = moduleId + "#" + exportName;
|
|
856
881
|
const scope = currentRenderScope();
|
|
857
882
|
const seen = scope ? (scope.islands ?? (scope.islands = new Map())) : looseIslands;
|
|
858
|
-
const
|
|
859
|
-
seen.
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
h = Math.imul(h ^ key.charCodeAt(i), 0x01000193) >>> 0;
|
|
863
|
-
return h.toString(36).padStart(7, "0") + "_" + n;
|
|
883
|
+
const counter = key == null ? component : component + "\u0000" + key;
|
|
884
|
+
const n = seen.get(counter) ?? 0;
|
|
885
|
+
seen.set(counter, n + 1);
|
|
886
|
+
return fnv36(component) + "_" + (key == null ? "" : "k" + fnv36(key) + "_") + n;
|
|
864
887
|
}
|
|
865
888
|
const warnedUncloneable = new Set();
|
|
866
889
|
// RENDER_CALL is the third argument the renderer passes when it renders a
|
|
@@ -933,15 +956,17 @@ export function clientBoundary(moduleId, exportName, src, impl, render, hydrate
|
|
|
933
956
|
// Called by user code, the boundary fails here and now. An async function
|
|
934
957
|
// would hand back a rejected promise the caller never awaits, and that
|
|
935
958
|
// unhandled rejection would take the renderer process down.
|
|
936
|
-
const Boundary = (props, vals, from) => {
|
|
959
|
+
const Boundary = (props, vals, from, key) => {
|
|
937
960
|
if (from !== RENDER_CALL)
|
|
938
961
|
throw clientCallError(exportName, name);
|
|
939
|
-
return renderBoundary(props, vals);
|
|
962
|
+
return renderBoundary(props, vals, key);
|
|
940
963
|
};
|
|
941
|
-
const renderBoundary = async (props, vals) => {
|
|
964
|
+
const renderBoundary = async (props, vals, elementKey) => {
|
|
965
|
+
// A key is not a prop (React leaves it out of props too).
|
|
966
|
+
const { children, key: propKey, ...rest } = props || {};
|
|
967
|
+
const key = elementKey ?? (propKey != null ? String(propKey) : undefined);
|
|
942
968
|
// Taken before the first await, so islands number in render order.
|
|
943
|
-
const ids = islandIdPrefix(moduleId, exportName);
|
|
944
|
-
const { children, ...rest } = props || {};
|
|
969
|
+
const ids = islandIdPrefix(moduleId, exportName, key);
|
|
945
970
|
const slots = [];
|
|
946
971
|
const slotCtx = { hydrate: false, component: name, slots, vals };
|
|
947
972
|
const data = await clientProps(rest, slotCtx, flight);
|
|
@@ -984,9 +1009,10 @@ export function clientBoundary(moduleId, exportName, src, impl, render, hydrate
|
|
|
984
1009
|
// element of its own; a Preact island hydrates the nodes between its two
|
|
985
1010
|
// comments in place.
|
|
986
1011
|
if (!flight)
|
|
987
|
-
return raw(islandOpen(moduleId, exportName, src, data, ids) + html + ISLAND_CLOSE);
|
|
1012
|
+
return raw(islandOpen(moduleId, exportName, src, data, ids, key) + html + ISLAND_CLOSE);
|
|
1013
|
+
const keyAttr = key != null ? ` data-key="${escapeHTML(key)}"` : "";
|
|
988
1014
|
return raw(propsScript(data) +
|
|
989
|
-
`<krate-island style="display:contents" data-module="${escapeHTML(moduleId)}" data-export="${escapeHTML(exportName)}" ` +
|
|
1015
|
+
`<krate-island style="display:contents" data-module="${escapeHTML(moduleId)}" data-export="${escapeHTML(exportName)}"${keyAttr} ` +
|
|
990
1016
|
`data-src="${escapeHTML(src)}">${html}</krate-island>`);
|
|
991
1017
|
};
|
|
992
1018
|
Object.defineProperty(Boundary, "name", { value: name });
|
package/dist/shared-isr.js
CHANGED
|
@@ -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.d.ts
CHANGED
|
@@ -216,6 +216,8 @@ export type CSSFlagsScope<K extends string> = [
|
|
|
216
216
|
trigger: (flag: K) => Record<string, unknown>;
|
|
217
217
|
panel: (flag: K) => Record<string, unknown>;
|
|
218
218
|
close: () => Record<string, unknown>;
|
|
219
|
+
/** Condition atom for `showIf`: true while `flag` is on. */
|
|
220
|
+
is: (flag: K) => boolean;
|
|
219
221
|
};
|
|
220
222
|
export declare function createCSSFlags<K extends string>(flags: readonly K[], _options?: CSSARIAOptions & {
|
|
221
223
|
vars?: Record<string, Partial<Record<K, string>>>;
|
|
@@ -261,6 +263,12 @@ export type CSSScope<T> = [() => T, (next: T) => void] & {
|
|
|
261
263
|
close: () => Record<string, unknown>;
|
|
262
264
|
/** The current selected value (live, via `--krate-current`). */
|
|
263
265
|
current: () => T;
|
|
266
|
+
/** Condition atom for `showIf`: true while `value` is selected. */
|
|
267
|
+
is: (value: T) => boolean;
|
|
268
|
+
/** Condition atom for a toggle: true while it is on. */
|
|
269
|
+
on: () => boolean;
|
|
270
|
+
/** Negated condition atom for a toggle: true while it is off. */
|
|
271
|
+
off: () => boolean;
|
|
264
272
|
};
|
|
265
273
|
/** Actions destructured from a `createCSSStack` result. */
|
|
266
274
|
export interface CSSStackActions<K extends string> {
|
package/dist/signal.js
CHANGED
|
@@ -571,6 +571,7 @@ export function createCSSFlags(flags, _options) {
|
|
|
571
571
|
scope.trigger = (flag) => ({ 'data-krate-scope': 'trigger', value: flag });
|
|
572
572
|
scope.panel = (flag) => ({ 'data-krate-scope': 'panel', value: flag });
|
|
573
573
|
scope.close = () => ({ 'data-krate-scope': 'close' });
|
|
574
|
+
scope.is = (flag) => getters[flag]?.() ?? false;
|
|
574
575
|
return scope;
|
|
575
576
|
}
|
|
576
577
|
/**
|
|
@@ -595,6 +596,9 @@ function withScopeHelpers(result) {
|
|
|
595
596
|
scope.panel = (value) => ({ 'data-krate-scope': 'panel', value });
|
|
596
597
|
scope.close = () => ({ 'data-krate-scope': 'close' });
|
|
597
598
|
scope.current = () => result[0]();
|
|
599
|
+
scope.is = (value) => result[0]() === value;
|
|
600
|
+
scope.on = () => Boolean(result[0]());
|
|
601
|
+
scope.off = () => !result[0]();
|
|
598
602
|
return scope;
|
|
599
603
|
}
|
|
600
604
|
/**
|
package/dist/ssr-core.d.ts
CHANGED
|
@@ -47,6 +47,7 @@ export interface RenderRequest {
|
|
|
47
47
|
id: string;
|
|
48
48
|
kind?: "component" | "page";
|
|
49
49
|
}[];
|
|
50
|
+
prerender?: boolean;
|
|
50
51
|
}
|
|
51
52
|
export type CacheStatus = "hit" | "stale" | "miss";
|
|
52
53
|
export interface RenderResponse {
|
|
@@ -56,6 +57,8 @@ export interface RenderResponse {
|
|
|
56
57
|
notFound?: boolean;
|
|
57
58
|
/** 401 or 403 when the render called unauthorized() or forbidden(). */
|
|
58
59
|
denied?: 401 | 403;
|
|
60
|
+
/** Request-only API that bailed out a build-time prerender. */
|
|
61
|
+
dynamicUsage?: string;
|
|
59
62
|
cached?: boolean;
|
|
60
63
|
cacheStatus?: CacheStatus;
|
|
61
64
|
error?: string;
|
|
@@ -77,11 +80,14 @@ export interface QueryTracker {
|
|
|
77
80
|
}
|
|
78
81
|
export declare function newQueryTracker(): QueryTracker;
|
|
79
82
|
export declare function trackedQuery(query: Record<string, string> | undefined, t: QueryTracker): Record<string, string>;
|
|
83
|
+
export declare function cacheEntryBytes(e: CacheEntry): number;
|
|
80
84
|
export declare class ISRCache {
|
|
81
85
|
private cache;
|
|
82
86
|
private byTag;
|
|
83
87
|
private revalidationIntervals;
|
|
84
|
-
constructor(maxSize?: number);
|
|
88
|
+
constructor(maxSize?: number, maxBytes?: number);
|
|
89
|
+
/** The approximate memory the cached pages take, in bytes. */
|
|
90
|
+
get bytes(): number;
|
|
85
91
|
setRevalidation(route: string, seconds: number): void;
|
|
86
92
|
get(key: string): CacheEntry | undefined;
|
|
87
93
|
set(key: string, entry: CacheEntry): void;
|
|
@@ -113,6 +119,11 @@ export interface SSRCoreOptions {
|
|
|
113
119
|
loadRuntimeRenderer(relPath: string): (props: string | Record<string, any>) => string | Promise<string>;
|
|
114
120
|
/** ISR/region cache entry bound (default 512). */
|
|
115
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;
|
|
116
127
|
/** Per-render timeout in ms (default 5000, 0 disables). */
|
|
117
128
|
timeoutMs?: number;
|
|
118
129
|
ppr?: boolean;
|
|
@@ -148,7 +159,7 @@ export interface IsrStore {
|
|
|
148
159
|
/** The in-process IsrStore: a bounded map with a tag index. */
|
|
149
160
|
export declare class MemoryIsrStore implements IsrStore {
|
|
150
161
|
readonly cache: ISRCache;
|
|
151
|
-
constructor(maxSize?: number);
|
|
162
|
+
constructor(maxSize?: number, maxBytes?: number);
|
|
152
163
|
get(key: string): CacheEntry | undefined;
|
|
153
164
|
set(key: string, entry: CacheEntry): void;
|
|
154
165
|
deleteRoute(route: string): number;
|
package/dist/ssr-core.js
CHANGED
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
7
|
import { jsx, renderToStringAsync } from "./server.js";
|
|
8
8
|
import { sha256Hex as portableSha256Hex } from "./sha256.js";
|
|
9
|
-
import { isNotFoundSignal, accessDeniedStatus, parseCookieHeader, redirectTarget, redirectStatus, thenable, thenableRecord, } from "./request-context.js";
|
|
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
|
|
@@ -370,14 +380,17 @@ export function createSSRCore(opts) {
|
|
|
370
380
|
headers: h,
|
|
371
381
|
cookies: parseCookieHeader(h.get("cookie")),
|
|
372
382
|
params: { ...req.params },
|
|
373
|
-
searchParams: tracker ? trackedQuery(req.query, tracker) : { ...req.query },
|
|
383
|
+
searchParams: req.prerender ? {} : tracker ? trackedQuery(req.query, tracker) : { ...req.query },
|
|
384
|
+
prerender: !!req.prerender,
|
|
374
385
|
};
|
|
375
386
|
}
|
|
376
387
|
function withRequest(req, fn, tracker) {
|
|
377
388
|
return requestStore.run(requestContextFor(req, tracker), fn);
|
|
378
389
|
}
|
|
379
|
-
// renderSignal maps notFound()/redirect() thrown during a render to a response.
|
|
390
|
+
// renderSignal maps notFound()/redirect()/dynamic-usage thrown during a render to a response.
|
|
380
391
|
function renderSignal(err) {
|
|
392
|
+
if (isDynamicUsageError(err))
|
|
393
|
+
return { html: "", status: 200, dynamicUsage: dynamicUsageReason(err) };
|
|
381
394
|
if (isNotFoundSignal(err))
|
|
382
395
|
return { html: "", status: 404, notFound: true };
|
|
383
396
|
const denied = accessDeniedStatus(err);
|
|
@@ -388,6 +401,36 @@ export function createSSRCore(opts) {
|
|
|
388
401
|
return { html: "", status: redirectStatus(err), redirect: to };
|
|
389
402
|
return null;
|
|
390
403
|
}
|
|
404
|
+
// prerenderSearchParams returns a thenable Proxy that throws a dynamic-usage
|
|
405
|
+
// error if page or generateMetadata code reads any key or enumerates it,
|
|
406
|
+
// while letting pages that merely receive props without reading searchParams
|
|
407
|
+
// prerender cleanly.
|
|
408
|
+
function prerenderSearchParams() {
|
|
409
|
+
const makeProxy = () => new Proxy({}, {
|
|
410
|
+
get(obj, prop, recv) {
|
|
411
|
+
if (typeof prop === "string" && prop !== "then" && prop !== "toJSON" && prop !== "constructor") {
|
|
412
|
+
throw dynamicUsageError("searchParams");
|
|
413
|
+
}
|
|
414
|
+
return Reflect.get(obj, prop, recv);
|
|
415
|
+
},
|
|
416
|
+
has(_obj, prop) {
|
|
417
|
+
if (typeof prop === "string" && prop !== "then" && prop !== "toJSON" && prop !== "constructor") {
|
|
418
|
+
throw dynamicUsageError("searchParams");
|
|
419
|
+
}
|
|
420
|
+
return false;
|
|
421
|
+
},
|
|
422
|
+
ownKeys() {
|
|
423
|
+
throw dynamicUsageError("searchParams");
|
|
424
|
+
},
|
|
425
|
+
getOwnPropertyDescriptor(obj, prop) {
|
|
426
|
+
if (typeof prop === "string" && prop !== "then" && prop !== "toJSON" && prop !== "constructor") {
|
|
427
|
+
throw dynamicUsageError("searchParams");
|
|
428
|
+
}
|
|
429
|
+
return Reflect.getOwnPropertyDescriptor(obj, prop);
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
return thenable(makeProxy(), makeProxy);
|
|
433
|
+
}
|
|
391
434
|
function buildProps(req, tracker) {
|
|
392
435
|
// Page-level data fetching (getStaticProps/getServerSideProps) has been
|
|
393
436
|
// removed; per-request data is provided by server components (@server),
|
|
@@ -401,6 +444,10 @@ export function createSSRCore(opts) {
|
|
|
401
444
|
const props = {};
|
|
402
445
|
if (req.params)
|
|
403
446
|
props.params = thenableRecord(req.params);
|
|
447
|
+
if (req.prerender) {
|
|
448
|
+
props.searchParams = prerenderSearchParams();
|
|
449
|
+
return props;
|
|
450
|
+
}
|
|
404
451
|
if (tracker) {
|
|
405
452
|
// Reads through the view are recorded; awaiting it resolves to another
|
|
406
453
|
// tracked view so `const { page } = await searchParams` records "page".
|
|
@@ -457,7 +504,7 @@ export function createSSRCore(opts) {
|
|
|
457
504
|
}
|
|
458
505
|
// An uncacheable read (no-store) keeps the render out of the ISR cache, and
|
|
459
506
|
// a revalidation that landed mid-render means the HTML may be stale.
|
|
460
|
-
if (page.mode === "isr" && !scope.noStore && !invalidated) {
|
|
507
|
+
if (!req.prerender && page.mode === "isr" && !scope.noStore && !invalidated) {
|
|
461
508
|
recordQueryReads(req, tracker);
|
|
462
509
|
await isrStore.set(pageKey(req), {
|
|
463
510
|
html,
|
|
@@ -552,7 +599,7 @@ export function createSSRCore(opts) {
|
|
|
552
599
|
return { html: "", status: 404, notFound: true };
|
|
553
600
|
}
|
|
554
601
|
// ISR: serve from cache with stale-while-revalidate.
|
|
555
|
-
if (page.mode === "isr") {
|
|
602
|
+
if (!req.prerender && page.mode === "isr") {
|
|
556
603
|
const interval = page.revalidate || 60;
|
|
557
604
|
pageIntervals.set(page.route, interval);
|
|
558
605
|
const key = pageKey(req);
|
|
@@ -601,7 +648,10 @@ export function createSSRCore(opts) {
|
|
|
601
648
|
// Bounded like the page cache, and with the same segmented eviction: a
|
|
602
649
|
// client minting params variants cannot push out the regions hit twice.
|
|
603
650
|
const REGION_CACHE_MAX = opts.maxCacheSize && opts.maxCacheSize > 0 ? opts.maxCacheSize : 512;
|
|
604
|
-
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
|
+
});
|
|
605
655
|
const inFlightRegionRevalidations = new Map();
|
|
606
656
|
function paramsKeyOf(params) {
|
|
607
657
|
return variantKey({ route: "", params });
|
|
@@ -861,6 +911,8 @@ export function createSSRCore(opts) {
|
|
|
861
911
|
frame.notFound = true;
|
|
862
912
|
if (result.denied)
|
|
863
913
|
frame.denied = result.denied;
|
|
914
|
+
if (result.dynamicUsage)
|
|
915
|
+
frame.dynamicUsage = result.dynamicUsage;
|
|
864
916
|
if (result.redirect)
|
|
865
917
|
frame.redirect = result.redirect;
|
|
866
918
|
if (result.error)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krate/runtime",
|
|
3
|
-
"version": "0.5.0-rc.
|
|
3
|
+
"version": "0.5.0-rc.16",
|
|
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",
|