@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/list.js
CHANGED
|
@@ -32,7 +32,29 @@ function keyOf(keyFn, item, i) {
|
|
|
32
32
|
if (!keyFn)
|
|
33
33
|
return String(i);
|
|
34
34
|
const k = keyFn(item, i);
|
|
35
|
-
|
|
35
|
+
if (k == null)
|
|
36
|
+
return String(i);
|
|
37
|
+
if (typeof k === 'object' || typeof k === 'function')
|
|
38
|
+
return objectKey(k);
|
|
39
|
+
return '$' + String(k);
|
|
40
|
+
}
|
|
41
|
+
// Object keys (a DOM node, say) have no useful string form, so they are keyed
|
|
42
|
+
// by identity: a stable id per object.
|
|
43
|
+
let objectIds;
|
|
44
|
+
let nextObjectId = 0;
|
|
45
|
+
let warnedObjectKey = false;
|
|
46
|
+
function objectKey(k) {
|
|
47
|
+
const ids = objectIds || (objectIds = new WeakMap());
|
|
48
|
+
let id = ids.get(k);
|
|
49
|
+
if (id === undefined) {
|
|
50
|
+
id = ++nextObjectId;
|
|
51
|
+
ids.set(k, id);
|
|
52
|
+
}
|
|
53
|
+
if (!warnedObjectKey) {
|
|
54
|
+
warnedObjectKey = true;
|
|
55
|
+
console.error('A list key is a DOM node or object. Use a string or number as the key; Krate keys the row by object identity instead.');
|
|
56
|
+
}
|
|
57
|
+
return '@o' + id;
|
|
36
58
|
}
|
|
37
59
|
function decodeKey(s) {
|
|
38
60
|
try {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare function approxBytes(value: unknown, maxDepth?: number): number;
|
|
2
|
+
/** The default memory budget of the render caches, in bytes. */
|
|
3
|
+
export declare const DEFAULT_CACHE_BYTES: number;
|
|
4
|
+
/**
|
|
5
|
+
* The byte budgets of the in-memory caches, from KRATE_SSR_CACHE_BYTES (the
|
|
6
|
+
* total for one process; 0 or unset is the default). Rendered pages get half,
|
|
7
|
+
* PPR regions and the data cache a quarter each.
|
|
8
|
+
*/
|
|
9
|
+
export declare function cacheByteBudgets(total?: number): {
|
|
10
|
+
pages: number;
|
|
11
|
+
regions: number;
|
|
12
|
+
data: number;
|
|
13
|
+
};
|
|
14
|
+
export declare function cacheBytesFromEnv(env: Record<string, string | undefined> | undefined): number;
|
package/dist/memsize.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Approximate retained size of cached values, for byte-bounded caches. It
|
|
2
|
+
// counts string characters as bytes (V8 stores ASCII text one byte per
|
|
3
|
+
// character), adds a small fixed cost per object, array, key and number, and
|
|
4
|
+
// stops at maxDepth so a deep or cyclic value costs a bounded walk.
|
|
5
|
+
const OBJECT_COST = 32;
|
|
6
|
+
const SLOT_COST = 8;
|
|
7
|
+
export function approxBytes(value, maxDepth = 8) {
|
|
8
|
+
const seen = new Set();
|
|
9
|
+
const walk = (v, depth) => {
|
|
10
|
+
switch (typeof v) {
|
|
11
|
+
case "string":
|
|
12
|
+
return v.length + 16;
|
|
13
|
+
case "number":
|
|
14
|
+
case "boolean":
|
|
15
|
+
case "undefined":
|
|
16
|
+
case "symbol":
|
|
17
|
+
return SLOT_COST;
|
|
18
|
+
case "bigint":
|
|
19
|
+
return 16;
|
|
20
|
+
case "function":
|
|
21
|
+
return OBJECT_COST;
|
|
22
|
+
}
|
|
23
|
+
if (v === null)
|
|
24
|
+
return SLOT_COST;
|
|
25
|
+
const o = v;
|
|
26
|
+
if (seen.has(o))
|
|
27
|
+
return SLOT_COST;
|
|
28
|
+
seen.add(o);
|
|
29
|
+
if (ArrayBuffer.isView(o))
|
|
30
|
+
return o.byteLength + OBJECT_COST;
|
|
31
|
+
if (o instanceof ArrayBuffer)
|
|
32
|
+
return o.byteLength + OBJECT_COST;
|
|
33
|
+
if (depth >= maxDepth)
|
|
34
|
+
return OBJECT_COST;
|
|
35
|
+
let n = OBJECT_COST;
|
|
36
|
+
if (Array.isArray(o)) {
|
|
37
|
+
for (const item of o)
|
|
38
|
+
n += SLOT_COST + walk(item, depth + 1);
|
|
39
|
+
return n;
|
|
40
|
+
}
|
|
41
|
+
if (o instanceof Map) {
|
|
42
|
+
for (const [k, item] of o)
|
|
43
|
+
n += walk(k, depth + 1) + walk(item, depth + 1);
|
|
44
|
+
return n;
|
|
45
|
+
}
|
|
46
|
+
if (o instanceof Set) {
|
|
47
|
+
for (const item of o)
|
|
48
|
+
n += SLOT_COST + walk(item, depth + 1);
|
|
49
|
+
return n;
|
|
50
|
+
}
|
|
51
|
+
for (const k of Object.keys(o))
|
|
52
|
+
n += k.length + SLOT_COST + walk(o[k], depth + 1);
|
|
53
|
+
return n;
|
|
54
|
+
};
|
|
55
|
+
return walk(value, 0);
|
|
56
|
+
}
|
|
57
|
+
/** The default memory budget of the render caches, in bytes. */
|
|
58
|
+
export const DEFAULT_CACHE_BYTES = 64 * 1024 * 1024;
|
|
59
|
+
/**
|
|
60
|
+
* The byte budgets of the in-memory caches, from KRATE_SSR_CACHE_BYTES (the
|
|
61
|
+
* total for one process; 0 or unset is the default). Rendered pages get half,
|
|
62
|
+
* PPR regions and the data cache a quarter each.
|
|
63
|
+
*/
|
|
64
|
+
export function cacheByteBudgets(total) {
|
|
65
|
+
const t = total && total > 0 ? total : DEFAULT_CACHE_BYTES;
|
|
66
|
+
return { pages: Math.floor(t / 2), regions: Math.floor(t / 4), data: Math.floor(t / 4) };
|
|
67
|
+
}
|
|
68
|
+
export function cacheBytesFromEnv(env) {
|
|
69
|
+
const n = Number(env?.KRATE_SSR_CACHE_BYTES);
|
|
70
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;
|
|
71
|
+
}
|
package/dist/metadata.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// viewport (string), verification, appleWebApp, formatDetection, category,
|
|
15
15
|
// itunes, appLinks, archives, assets, bookmarks and other. metadataBase resolves
|
|
16
16
|
// relative URLs. Anything else is ignored.
|
|
17
|
-
import { httpFallbackStatus, redirectTarget, thenableRecord } from './request-context.js';
|
|
17
|
+
import { httpFallbackStatus, isDynamicUsageError, redirectTarget, thenableRecord } from './request-context.js';
|
|
18
18
|
function isThenable(v) {
|
|
19
19
|
return !!v && typeof v.then === 'function';
|
|
20
20
|
}
|
|
@@ -93,7 +93,7 @@ export async function resolveMetadata(segments, rawProps) {
|
|
|
93
93
|
// notFound(), forbidden(), unauthorized() and redirect() in
|
|
94
94
|
// generateMetadata answer the request; any other error costs only the
|
|
95
95
|
// segment's metadata.
|
|
96
|
-
if (httpFallbackStatus(error) || redirectTarget(error))
|
|
96
|
+
if (httpFallbackStatus(error) || redirectTarget(error) || isDynamicUsageError(error))
|
|
97
97
|
throw error;
|
|
98
98
|
errors.push({ file: seg.file || '', error });
|
|
99
99
|
own = undefined;
|
|
@@ -341,10 +341,21 @@ export function renderMetadataTags(md, templates = {}) {
|
|
|
341
341
|
if (parts.length)
|
|
342
342
|
push(meta('name', 'format-detection', parts.join(', ')));
|
|
343
343
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
344
|
+
// Like Next.js, an openGraph or twitter object without its own title takes
|
|
345
|
+
// the resolved top-level title (already templated); twitter takes the
|
|
346
|
+
// openGraph title and description first. Descriptions are not copied from
|
|
347
|
+
// the top level, so a <Head> description override leaves no stale copy.
|
|
348
|
+
const topTitle = typeof md.title === 'string' ? md.title : '';
|
|
349
|
+
let ogTitle = '';
|
|
350
|
+
if (md.openGraph) {
|
|
351
|
+
ogTitle = titleOf(md.openGraph.title, templates.openGraph) || topTitle;
|
|
352
|
+
renderOpenGraph(md.openGraph, base, ogTitle, md.openGraph.description, push);
|
|
353
|
+
}
|
|
354
|
+
if (md.twitter) {
|
|
355
|
+
const twTitle = titleOf(md.twitter.title, templates.twitter) || ogTitle || topTitle;
|
|
356
|
+
const twDescription = md.twitter.description || md.openGraph?.description;
|
|
357
|
+
renderTwitter(md.twitter, base, twTitle, twDescription, push);
|
|
358
|
+
}
|
|
348
359
|
if (md.appLinks) {
|
|
349
360
|
for (const [platform, entries] of Object.entries(md.appLinks))
|
|
350
361
|
for (const entry of toArray(entries))
|
|
@@ -378,9 +389,9 @@ function renderIcons(icons, base, push) {
|
|
|
378
389
|
emit(o.apple, 'apple-touch-icon');
|
|
379
390
|
emit(o.other, 'icon');
|
|
380
391
|
}
|
|
381
|
-
function renderOpenGraph(og, base,
|
|
382
|
-
push(meta('property', 'og:title',
|
|
383
|
-
push(meta('property', 'og:description',
|
|
392
|
+
function renderOpenGraph(og, base, title, description, push) {
|
|
393
|
+
push(meta('property', 'og:title', title));
|
|
394
|
+
push(meta('property', 'og:description', description));
|
|
384
395
|
push(meta('property', 'og:url', resolveBaseJoin(og.url, base)));
|
|
385
396
|
push(meta('property', 'og:site_name', og.siteName));
|
|
386
397
|
push(meta('property', 'og:locale', og.locale));
|
|
@@ -423,7 +434,7 @@ function renderOpenGraph(og, base, template, push) {
|
|
|
423
434
|
push(meta('property', 'profile:gender', og.gender));
|
|
424
435
|
}
|
|
425
436
|
}
|
|
426
|
-
function renderTwitter(tw, base,
|
|
437
|
+
function renderTwitter(tw, base, title, description, push) {
|
|
427
438
|
const images = imageList(tw.images, base);
|
|
428
439
|
const card = tw.card || (images.length > 0 ? 'summary_large_image' : 'summary');
|
|
429
440
|
push(meta('name', 'twitter:card', card));
|
|
@@ -431,8 +442,8 @@ function renderTwitter(tw, base, template, push) {
|
|
|
431
442
|
push(meta('name', 'twitter:site:id', tw.siteId));
|
|
432
443
|
push(meta('name', 'twitter:creator', tw.creator));
|
|
433
444
|
push(meta('name', 'twitter:creator:id', tw.creatorId));
|
|
434
|
-
push(meta('name', 'twitter:title',
|
|
435
|
-
push(meta('name', 'twitter:description',
|
|
445
|
+
push(meta('name', 'twitter:title', title));
|
|
446
|
+
push(meta('name', 'twitter:description', description));
|
|
436
447
|
for (const img of images) {
|
|
437
448
|
push(meta('name', 'twitter:image', img.url));
|
|
438
449
|
push(meta('name', 'twitter:image:alt', img.alt));
|
|
@@ -16,6 +16,8 @@ export interface RequestContext {
|
|
|
16
16
|
cookies: Map<string, string>;
|
|
17
17
|
params: Record<string, string>;
|
|
18
18
|
searchParams: Record<string, string>;
|
|
19
|
+
/** True during build-time prerendering, where request-only APIs bail out. */
|
|
20
|
+
prerender?: boolean;
|
|
19
21
|
}
|
|
20
22
|
/** The current render request, or undefined outside a server render. */
|
|
21
23
|
export declare function getRequestContext(): RequestContext | undefined;
|
|
@@ -88,3 +90,11 @@ export declare function httpFallbackStatus(err: unknown): 401 | 403 | 404 | 0;
|
|
|
88
90
|
export declare function redirectStatus(err: unknown): 307 | 308;
|
|
89
91
|
/** redirectTarget returns the URL of a redirect() signal, or "" for other errors. */
|
|
90
92
|
export declare function redirectTarget(err: unknown): string;
|
|
93
|
+
/** Thrown when a request-only API is read during build-time prerendering. */
|
|
94
|
+
export interface DynamicUsageError extends Error {
|
|
95
|
+
__krateDynamicUsage: string;
|
|
96
|
+
digest: string;
|
|
97
|
+
}
|
|
98
|
+
export declare function dynamicUsageError(api: string): DynamicUsageError;
|
|
99
|
+
export declare function isDynamicUsageError(err: unknown): err is DynamicUsageError;
|
|
100
|
+
export declare function dynamicUsageReason(err: unknown): string;
|
package/dist/request-context.js
CHANGED
|
@@ -167,3 +167,27 @@ export function redirectTarget(err) {
|
|
|
167
167
|
}
|
|
168
168
|
return '';
|
|
169
169
|
}
|
|
170
|
+
export function dynamicUsageError(api) {
|
|
171
|
+
const err = new Error(`Dynamic server usage: ${api} cannot be used while prerendering a static page`);
|
|
172
|
+
err.__krateDynamicUsage = api;
|
|
173
|
+
err.digest = 'DYNAMIC_SERVER_USAGE';
|
|
174
|
+
return err;
|
|
175
|
+
}
|
|
176
|
+
export function isDynamicUsageError(err) {
|
|
177
|
+
if (!err || typeof err !== 'object')
|
|
178
|
+
return false;
|
|
179
|
+
const e = err;
|
|
180
|
+
if (typeof e.__krateDynamicUsage === 'string' && e.__krateDynamicUsage !== '')
|
|
181
|
+
return true;
|
|
182
|
+
return e.digest === 'DYNAMIC_SERVER_USAGE';
|
|
183
|
+
}
|
|
184
|
+
export function dynamicUsageReason(err) {
|
|
185
|
+
if (!err || typeof err !== 'object')
|
|
186
|
+
return 'dynamic server usage';
|
|
187
|
+
const e = err;
|
|
188
|
+
if (typeof e.__krateDynamicUsage === 'string' && e.__krateDynamicUsage !== '')
|
|
189
|
+
return e.__krateDynamicUsage;
|
|
190
|
+
if (typeof e.message === 'string' && e.message !== '')
|
|
191
|
+
return e.message;
|
|
192
|
+
return 'dynamic server usage';
|
|
193
|
+
}
|
package/dist/segmented-lru.d.ts
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
|
+
export interface ByteLimit<V> {
|
|
2
|
+
maxBytes: number;
|
|
3
|
+
sizeOf: (value: V) => number;
|
|
4
|
+
}
|
|
1
5
|
export declare class SegmentedLRU<V> {
|
|
2
6
|
private probation;
|
|
3
7
|
private protectedSeg;
|
|
4
8
|
private readonly max;
|
|
5
9
|
private readonly protectedMax;
|
|
6
|
-
|
|
10
|
+
private readonly limit?;
|
|
11
|
+
private readonly sizes;
|
|
12
|
+
private used;
|
|
13
|
+
constructor(maxSize: number, limit?: ByteLimit<V>);
|
|
7
14
|
get size(): number;
|
|
15
|
+
/** The summed sizes of the entries (0 without a byte limit). */
|
|
16
|
+
get bytes(): number;
|
|
8
17
|
has(key: string): boolean;
|
|
9
18
|
/** Reads an entry without counting as a use. */
|
|
10
19
|
peek(key: string): V | undefined;
|
|
11
20
|
/** Reads an entry and counts the use: probation entries are promoted. */
|
|
12
21
|
get(key: string): V | undefined;
|
|
22
|
+
private demoteColdest;
|
|
13
23
|
/** Stores an entry. An existing key keeps its segment. Returns the evicted entries. */
|
|
14
24
|
set(key: string, value: V): Array<[string, V]>;
|
|
25
|
+
private evictOldest;
|
|
26
|
+
private fitBytes;
|
|
27
|
+
private account;
|
|
15
28
|
delete(key: string): boolean;
|
|
16
29
|
clear(): void;
|
|
17
30
|
keys(): string[];
|
package/dist/segmented-lru.js
CHANGED
|
@@ -6,9 +6,13 @@
|
|
|
6
6
|
// flood of one-off keys only churns probation and never reaches the entries
|
|
7
7
|
// that were requested twice.
|
|
8
8
|
export class SegmentedLRU {
|
|
9
|
-
constructor(maxSize) {
|
|
9
|
+
constructor(maxSize, limit) {
|
|
10
10
|
this.probation = new Map();
|
|
11
11
|
this.protectedSeg = new Map();
|
|
12
|
+
this.sizes = new Map();
|
|
13
|
+
this.used = 0;
|
|
14
|
+
if (limit && limit.maxBytes > 0)
|
|
15
|
+
this.limit = limit;
|
|
12
16
|
this.max = Math.max(1, Math.floor(maxSize));
|
|
13
17
|
// At least one probation slot, so a new entry can always be admitted.
|
|
14
18
|
this.protectedMax = Math.min(this.max - 1, Math.max(1, Math.floor(this.max * 0.8)));
|
|
@@ -16,6 +20,10 @@ export class SegmentedLRU {
|
|
|
16
20
|
get size() {
|
|
17
21
|
return this.probation.size + this.protectedSeg.size;
|
|
18
22
|
}
|
|
23
|
+
/** The summed sizes of the entries (0 without a byte limit). */
|
|
24
|
+
get bytes() {
|
|
25
|
+
return this.used;
|
|
26
|
+
}
|
|
19
27
|
has(key) {
|
|
20
28
|
return this.protectedSeg.has(key) || this.probation.has(key);
|
|
21
29
|
}
|
|
@@ -40,43 +48,104 @@ export class SegmentedLRU {
|
|
|
40
48
|
return cold;
|
|
41
49
|
}
|
|
42
50
|
this.protectedSeg.set(key, cold);
|
|
43
|
-
if (this.protectedSeg.size > this.protectedMax)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this.
|
|
51
|
+
if (this.protectedSeg.size > this.protectedMax)
|
|
52
|
+
this.demoteColdest();
|
|
53
|
+
if (this.limit) {
|
|
54
|
+
// Protected entries keep at most 80% of the bytes too, so probation
|
|
55
|
+
// always has room and new entries never flush the hot set.
|
|
56
|
+
const cap = this.limit.maxBytes * 0.8;
|
|
57
|
+
let hot = 0;
|
|
58
|
+
for (const k of this.protectedSeg.keys())
|
|
59
|
+
hot += this.sizes.get(k) || 0;
|
|
60
|
+
while (hot > cap && this.protectedSeg.size > 1)
|
|
61
|
+
hot -= this.sizes.get(this.demoteColdest()) || 0;
|
|
49
62
|
}
|
|
50
63
|
return cold;
|
|
51
64
|
}
|
|
65
|
+
// demoteColdest moves the coldest protected entry to probation's newest
|
|
66
|
+
// slot and returns its key.
|
|
67
|
+
demoteColdest() {
|
|
68
|
+
const demoted = this.protectedSeg.keys().next().value;
|
|
69
|
+
const v = this.protectedSeg.get(demoted);
|
|
70
|
+
this.protectedSeg.delete(demoted);
|
|
71
|
+
this.probation.set(demoted, v);
|
|
72
|
+
return demoted;
|
|
73
|
+
}
|
|
52
74
|
/** Stores an entry. An existing key keeps its segment. Returns the evicted entries. */
|
|
53
75
|
set(key, value) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
76
|
+
const n = this.limit ? Math.max(0, this.limit.sizeOf(value)) : 0;
|
|
77
|
+
if (this.limit && n > this.limit.maxBytes) {
|
|
78
|
+
// Too large to cache: drop any older copy instead of keeping it stale.
|
|
79
|
+
const old = this.peek(key);
|
|
80
|
+
if (old === undefined)
|
|
81
|
+
return [];
|
|
82
|
+
this.delete(key);
|
|
83
|
+
return [[key, old]];
|
|
58
84
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
85
|
+
const seg = this.protectedSeg.has(key) ? this.protectedSeg : this.probation.has(key) ? this.probation : null;
|
|
86
|
+
if (seg) {
|
|
87
|
+
seg.delete(key);
|
|
88
|
+
seg.set(key, value);
|
|
89
|
+
this.account(key, n);
|
|
90
|
+
return this.fitBytes(key);
|
|
63
91
|
}
|
|
64
92
|
const evicted = [];
|
|
65
|
-
while (this.size >= this.max) {
|
|
66
|
-
|
|
67
|
-
const oldest = seg.keys().next().value;
|
|
68
|
-
evicted.push([oldest, seg.get(oldest)]);
|
|
69
|
-
seg.delete(oldest);
|
|
93
|
+
while (this.size > 0 && (this.size >= this.max || (this.limit && this.used + n > this.limit.maxBytes))) {
|
|
94
|
+
evicted.push(this.evictOldest());
|
|
70
95
|
}
|
|
71
96
|
this.probation.set(key, value);
|
|
97
|
+
this.account(key, n);
|
|
72
98
|
return evicted;
|
|
73
99
|
}
|
|
100
|
+
// evictOldest removes the coldest entry: probation first, then protected.
|
|
101
|
+
evictOldest() {
|
|
102
|
+
const seg = this.probation.size > 0 ? this.probation : this.protectedSeg;
|
|
103
|
+
const oldest = seg.keys().next().value;
|
|
104
|
+
const v = seg.get(oldest);
|
|
105
|
+
seg.delete(oldest);
|
|
106
|
+
this.account(oldest, 0);
|
|
107
|
+
return [oldest, v];
|
|
108
|
+
}
|
|
109
|
+
// fitBytes evicts other entries, coldest first, until the sizes fit the
|
|
110
|
+
// limit again.
|
|
111
|
+
fitBytes(keep) {
|
|
112
|
+
const evicted = [];
|
|
113
|
+
if (!this.limit || this.used <= this.limit.maxBytes)
|
|
114
|
+
return evicted;
|
|
115
|
+
for (const seg of [this.probation, this.protectedSeg]) {
|
|
116
|
+
for (const k of [...seg.keys()]) {
|
|
117
|
+
if (this.used <= this.limit.maxBytes)
|
|
118
|
+
return evicted;
|
|
119
|
+
if (k === keep)
|
|
120
|
+
continue;
|
|
121
|
+
evicted.push([k, seg.get(k)]);
|
|
122
|
+
seg.delete(k);
|
|
123
|
+
this.account(k, 0);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return evicted;
|
|
127
|
+
}
|
|
128
|
+
account(key, n) {
|
|
129
|
+
if (!this.limit)
|
|
130
|
+
return;
|
|
131
|
+
this.used -= this.sizes.get(key) || 0;
|
|
132
|
+
if (n > 0)
|
|
133
|
+
this.sizes.set(key, n);
|
|
134
|
+
else
|
|
135
|
+
this.sizes.delete(key);
|
|
136
|
+
this.used += n;
|
|
137
|
+
}
|
|
74
138
|
delete(key) {
|
|
75
|
-
|
|
139
|
+
const gone = this.protectedSeg.delete(key) || this.probation.delete(key);
|
|
140
|
+
if (gone)
|
|
141
|
+
this.account(key, 0);
|
|
142
|
+
return gone;
|
|
76
143
|
}
|
|
77
144
|
clear() {
|
|
78
145
|
this.probation.clear();
|
|
79
146
|
this.protectedSeg.clear();
|
|
147
|
+
this.sizes.clear();
|
|
148
|
+
this.used = 0;
|
|
80
149
|
}
|
|
81
150
|
keys() {
|
|
82
151
|
return [...this.probation.keys(), ...this.protectedSeg.keys()];
|
package/dist/server-handler.d.ts
CHANGED
|
@@ -65,6 +65,7 @@ export interface HandlerConfig {
|
|
|
65
65
|
ssr: {
|
|
66
66
|
timeout: number;
|
|
67
67
|
maxCacheSize: number;
|
|
68
|
+
cacheBytes?: number;
|
|
68
69
|
ppr: boolean;
|
|
69
70
|
pprRevalidate: number;
|
|
70
71
|
};
|
|
@@ -82,6 +83,8 @@ export interface HandlerPage {
|
|
|
82
83
|
segDir?: string;
|
|
83
84
|
/** A segment page that renders per request: reached through its synthetic route, never a URL. */
|
|
84
85
|
segment?: boolean;
|
|
86
|
+
/** Route params of a page expanded from generateStaticParams, whose route is a plain path. */
|
|
87
|
+
params?: Record<string, string>;
|
|
85
88
|
}
|
|
86
89
|
/**
|
|
87
90
|
* A per-request metadata route. `path` is an exact URL; `prefix` and `suffix`
|
package/dist/server-handler.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// generates (`krate deploy`), and static files come from a pluggable layer.
|
|
12
12
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
13
13
|
import { createSSRCore, MemoryIsrStore, } from "./ssr-core.js";
|
|
14
|
+
import { cacheByteBudgets } from "./memsize.js";
|
|
14
15
|
import { runAfterTasks } from "./after.js";
|
|
15
16
|
import { platformWaitUntil } from "./platform.js";
|
|
16
17
|
import { cacheRuntime, installRenderStore, patchFetch } from "./cache-scope.js";
|
|
@@ -318,7 +319,7 @@ export function createKrateHandler(options) {
|
|
|
318
319
|
installRenderStore(new AsyncLocalStorage());
|
|
319
320
|
patchFetch();
|
|
320
321
|
// The ISR and data caches: this instance's memory, or the shared cache adapter.
|
|
321
|
-
const memoryIsr = new MemoryIsrStore(cfg.ssr.maxCacheSize > 0 ? cfg.ssr.maxCacheSize : 512);
|
|
322
|
+
const memoryIsr = new MemoryIsrStore(cfg.ssr.maxCacheSize > 0 ? cfg.ssr.maxCacheSize : 512, cacheByteBudgets(cfg.ssr.cacheBytes).pages);
|
|
322
323
|
const sharedCache = options.isrStore
|
|
323
324
|
? { store: options.isrStore, ready: Promise.resolve(null) }
|
|
324
325
|
: isrStoreFor(options.cache ?? cacheSpecFromConfig(cfg.cache), memoryIsr);
|
|
@@ -344,6 +345,7 @@ export function createKrateHandler(options) {
|
|
|
344
345
|
return fn;
|
|
345
346
|
},
|
|
346
347
|
maxCacheSize: cfg.ssr.maxCacheSize,
|
|
348
|
+
cacheBytes: cfg.ssr.cacheBytes,
|
|
347
349
|
timeoutMs: cfg.ssr.timeout > 0 ? cfg.ssr.timeout : 5000,
|
|
348
350
|
ppr: cfg.ssr.ppr,
|
|
349
351
|
pprRevalidate: cfg.ssr.pprRevalidate,
|
|
@@ -368,7 +370,7 @@ export function createKrateHandler(options) {
|
|
|
368
370
|
for (const p of pagesBySpecificity) {
|
|
369
371
|
const params = matchRoutePattern(urlPath, p.route);
|
|
370
372
|
if (params)
|
|
371
|
-
return { page: p, params };
|
|
373
|
+
return { page: p, params: p.params && Object.keys(params).length === 0 ? { ...p.params } : params };
|
|
372
374
|
}
|
|
373
375
|
return null;
|
|
374
376
|
}
|
package/dist/server-renderer.js
CHANGED
|
@@ -14,6 +14,7 @@ import { encodeFrame, REGION_FRAMES_TYPE, wantsRegionFrames } from "./region-wir
|
|
|
14
14
|
import { installRenderStore, patchFetch } from "./cache-scope.js";
|
|
15
15
|
import { cacheSpecFromEnv } from "./cache-adapter.js";
|
|
16
16
|
import { isrStoreFor } from "./shared-isr.js";
|
|
17
|
+
import { cacheByteBudgets, cacheBytesFromEnv } from "./memsize.js";
|
|
17
18
|
import { registerLanguage, setFenceResolver } from "./highlight.js";
|
|
18
19
|
import { installProcessGuards } from "./process-guard.js";
|
|
19
20
|
// Render cache scopes (tags / revalidate windows read during a render) and the
|
|
@@ -289,7 +290,8 @@ function loadRuntimeRenderer(relPath) {
|
|
|
289
290
|
}
|
|
290
291
|
// Render core
|
|
291
292
|
const pprEnabledEnv = process.env.KRATE_PPR === "1";
|
|
292
|
-
const
|
|
293
|
+
const cacheBytes = cacheBytesFromEnv(process.env);
|
|
294
|
+
const isrMemory = new MemoryIsrStore(parseInt(process.env.KRATE_SSR_MAX_CACHE || "", 10) || 512, cacheByteBudgets(cacheBytes).pages);
|
|
293
295
|
// With a shared cache adapter (KRATE_CACHE) the ISR cache and the data cache live
|
|
294
296
|
// in the adapter, so every instance serves and invalidates the same pages.
|
|
295
297
|
const sharedCache = isrStoreFor(cacheSpecFromEnv(process.env), isrMemory);
|
|
@@ -300,6 +302,7 @@ const core = createSSRCore({
|
|
|
300
302
|
loadPageModule,
|
|
301
303
|
loadRuntimeRenderer,
|
|
302
304
|
maxCacheSize: parseInt(process.env.KRATE_SSR_MAX_CACHE || "", 10) || 512,
|
|
305
|
+
cacheBytes,
|
|
303
306
|
timeoutMs: parseInt(process.env.KRATE_SSR_TIMEOUT || "", 10) || 5000,
|
|
304
307
|
ppr: pprEnabledEnv,
|
|
305
308
|
pprRevalidate: parseInt(process.env.KRATE_PPR_REVALIDATE || "", 10) || 60,
|
|
@@ -330,13 +333,14 @@ function reloadManifest() {
|
|
|
330
333
|
// ISR cache persistence - survives renderer restarts so a bounce doesn't
|
|
331
334
|
// cold-render every ISR variant. Written debounced (coalesced) to the build
|
|
332
335
|
// output dir (.krate/isr-cache.json), overridable via KRATE_ISR_CACHE.
|
|
336
|
+
const PRERENDER_MODE = process.env.KRATE_PRERENDER === "1";
|
|
333
337
|
const isrCacheFile = process.env.KRATE_ISR_CACHE || "";
|
|
334
338
|
let persistTimer = null;
|
|
335
339
|
function isrCachePath() {
|
|
336
340
|
return isrCacheFile || path.join(OUT_DIR, ".krate", "isr-cache.json");
|
|
337
341
|
}
|
|
338
342
|
function persistIsrNow() {
|
|
339
|
-
if (SSR_WORKERS > 1)
|
|
343
|
+
if (PRERENDER_MODE || SSR_WORKERS > 1)
|
|
340
344
|
return;
|
|
341
345
|
if (persistTimer) {
|
|
342
346
|
clearTimeout(persistTimer);
|
|
@@ -355,13 +359,15 @@ function persistIsrNow() {
|
|
|
355
359
|
function scheduleIsrPersist() {
|
|
356
360
|
// Clustered: multiple workers would race on the same cache file; keep the ISR
|
|
357
361
|
// cache in memory only.
|
|
358
|
-
if (SSR_WORKERS > 1)
|
|
362
|
+
if (PRERENDER_MODE || SSR_WORKERS > 1)
|
|
359
363
|
return;
|
|
360
364
|
if (persistTimer)
|
|
361
365
|
clearTimeout(persistTimer);
|
|
362
366
|
persistTimer = setTimeout(persistIsrNow, 1000);
|
|
363
367
|
}
|
|
364
368
|
function loadIsrCache() {
|
|
369
|
+
if (PRERENDER_MODE)
|
|
370
|
+
return;
|
|
365
371
|
try {
|
|
366
372
|
const file = isrCachePath();
|
|
367
373
|
if (fs.existsSync(file)) {
|
|
@@ -544,6 +550,7 @@ const server = http.createServer(async (req, res) => {
|
|
|
544
550
|
params: parsed.params,
|
|
545
551
|
query: parsed.query,
|
|
546
552
|
regions: parsed.regions,
|
|
553
|
+
prerender: !!parsed.prerender,
|
|
547
554
|
};
|
|
548
555
|
const page = findPage(renderReq.route);
|
|
549
556
|
if (!page) {
|
package/dist/server.d.ts
CHANGED
|
@@ -33,11 +33,13 @@ interface JSXElement {
|
|
|
33
33
|
type: string | Function | object | symbol;
|
|
34
34
|
props: Record<string, any>;
|
|
35
35
|
children: JSXNode[];
|
|
36
|
+
/** The element's React key, when it has one (a client island carries it, see clientBoundary). */
|
|
37
|
+
key?: string;
|
|
36
38
|
}
|
|
37
39
|
export declare function Fragment(_props: {
|
|
38
40
|
children?: JSXNode;
|
|
39
41
|
}): JSXNode;
|
|
40
|
-
export declare function jsx(type: string | Function | object | symbol, props: Record<string, any>,
|
|
42
|
+
export declare function jsx(type: string | Function | object | symbol, props: Record<string, any>, key?: string): JSXElement;
|
|
41
43
|
export declare const jsxs: typeof jsx;
|
|
42
44
|
/**
|
|
43
45
|
* ServerErrorBoundary is the request-time form of a segment's error.tsx under
|