@krate/runtime 0.5.0-rc.4 → 0.5.0-rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions.js +10 -1
- package/dist/cache-scope.d.ts +6 -0
- package/dist/cache-scope.js +14 -1
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +148 -74
- package/dist/index.d.ts +1 -0
- package/dist/krate-core.js +1 -1
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +24 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/list.d.ts +14 -4
- package/dist/list.js +4 -5
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/react-compat.d.ts +26 -0
- package/dist/react-compat.js +292 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/router.js +9 -0
- package/dist/server-handler.js +89 -10
- package/dist/server.d.ts +57 -10
- package/dist/server.js +455 -94
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +8 -0
- package/package.json +19 -2
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -75
- package/dist/after.test.d.ts +0 -1
- package/dist/after.test.js +0 -58
- package/dist/builtin-tags.test.d.ts +0 -1
- package/dist/builtin-tags.test.js +0 -12
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -45
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -37
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -42
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -55
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -25
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -24
- package/dist/inline-action-token.test.d.ts +0 -1
- package/dist/inline-action-token.test.js +0 -31
- package/dist/jsx-svg-types.test.d.ts +0 -1
- package/dist/jsx-svg-types.test.js +0 -58
- package/dist/link-base-path.test.d.ts +0 -1
- package/dist/link-base-path.test.js +0 -28
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -138
- package/dist/platform.test.d.ts +0 -1
- package/dist/platform.test.js +0 -44
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -130
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -247
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -162
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -80
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/site-config.test.d.ts +0 -1
- package/dist/site-config.test.js +0 -30
- package/dist/slot.test.d.ts +0 -1
- package/dist/slot.test.js +0 -25
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -108
- package/dist/testing.test.d.ts +0 -1
- package/dist/testing.test.js +0 -73
package/dist/actions.js
CHANGED
|
@@ -52,6 +52,12 @@ function navigateTo(url) {
|
|
|
52
52
|
else if (typeof location !== 'undefined')
|
|
53
53
|
location.assign(withBase(safe));
|
|
54
54
|
}
|
|
55
|
+
/** Follows an absolute http(s) redirect the server marked external. */
|
|
56
|
+
function navigateExternal(url) {
|
|
57
|
+
if (typeof location === 'undefined' || !/^https?:\/\//i.test(url))
|
|
58
|
+
return;
|
|
59
|
+
location.assign(url);
|
|
60
|
+
}
|
|
55
61
|
/** Only same-origin absolute paths are followed after an action. */
|
|
56
62
|
function safeRedirect(url) {
|
|
57
63
|
if (!url || !url.startsWith('/') || url.startsWith('//'))
|
|
@@ -127,7 +133,10 @@ export async function callServerAction(url, args) {
|
|
|
127
133
|
dispatchRevalidate(payload.revalidation);
|
|
128
134
|
if (typeof payload.redirect === 'string' && payload.redirect) {
|
|
129
135
|
redirectsFollowed++;
|
|
130
|
-
|
|
136
|
+
if (payload.external)
|
|
137
|
+
navigateExternal(payload.redirect);
|
|
138
|
+
else
|
|
139
|
+
navigateTo(payload.redirect);
|
|
131
140
|
}
|
|
132
141
|
return payload.result;
|
|
133
142
|
}
|
package/dist/cache-scope.d.ts
CHANGED
|
@@ -28,6 +28,10 @@ export interface CacheScope {
|
|
|
28
28
|
revalidate: number;
|
|
29
29
|
/** True when a read opted out of caching (no-store, revalidate 0, unstable_noStore). */
|
|
30
30
|
noStore: boolean;
|
|
31
|
+
/** True when an error boundary caught a render error (the response is a 500 and uncacheable). */
|
|
32
|
+
errorCaught?: boolean;
|
|
33
|
+
/** Head tags the render asked for (react-dom preload, preconnect, ...), deduplicated. */
|
|
34
|
+
hints?: Set<string>;
|
|
31
35
|
}
|
|
32
36
|
/** Minimal AsyncLocalStorage shape (the Node class satisfies it). */
|
|
33
37
|
export interface AsyncStore<T> {
|
|
@@ -118,6 +122,8 @@ export declare function routeDataTag(route: string): string;
|
|
|
118
122
|
export declare function recordCacheRead(tags: readonly string[] | undefined, revalidate: number | false | undefined): void;
|
|
119
123
|
/** Marks the current render as uncacheable. Outside a render it has no effect. */
|
|
120
124
|
export declare function markNoStore(): void;
|
|
125
|
+
/** Marks the current render as having shown an error boundary's UI. */
|
|
126
|
+
export declare function markErrorCaught(): void;
|
|
121
127
|
/**
|
|
122
128
|
* Reads a JSON value from the data cache; undefined on a miss. With a shared
|
|
123
129
|
* cache the instances' common store answers; when it cannot, this process's own
|
package/dist/cache-scope.js
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* has no Node imports: the embedded QuickJS API runtime bundles it too, and
|
|
19
19
|
* there the data cache is backed by the Go host (`_go_cache_get/_go_cache_set`).
|
|
20
20
|
*/
|
|
21
|
+
import { HEAD_CLOSE, HEAD_OPEN } from './head-tags.js';
|
|
21
22
|
const STATE_KEY = Symbol.for('krate.cache.runtime');
|
|
22
23
|
const REVALIDATE_STORE_KEY = Symbol.for('krate.revalidate.store');
|
|
23
24
|
/** In-process data cache with tag index and FIFO bound. */
|
|
@@ -147,7 +148,11 @@ export async function withCacheScope(route, fn) {
|
|
|
147
148
|
const scope = { route, tags: new Set(), revalidate: 0, noStore: false };
|
|
148
149
|
const epoch = rt.epoch;
|
|
149
150
|
const store = rt.renderStore;
|
|
150
|
-
|
|
151
|
+
let result = store ? await store.run(scope, () => Promise.resolve().then(fn)) : await fn();
|
|
152
|
+
if (scope.hints && scope.hints.size && typeof result === 'string') {
|
|
153
|
+
// react-dom resource hints go to the head of the response like <Head> tags.
|
|
154
|
+
result = (HEAD_OPEN + [...scope.hints].join('') + HEAD_CLOSE + result);
|
|
155
|
+
}
|
|
151
156
|
return { result, scope, invalidated: rt.epoch !== epoch };
|
|
152
157
|
}
|
|
153
158
|
/**
|
|
@@ -178,6 +183,14 @@ export function markNoStore() {
|
|
|
178
183
|
if (scope)
|
|
179
184
|
scope.noStore = true;
|
|
180
185
|
}
|
|
186
|
+
/** Marks the current render as having shown an error boundary's UI. */
|
|
187
|
+
export function markErrorCaught() {
|
|
188
|
+
const scope = currentRenderScope();
|
|
189
|
+
if (scope) {
|
|
190
|
+
scope.errorCaught = true;
|
|
191
|
+
scope.noStore = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
181
194
|
// Data cache access. In QuickJS the Go host owns the cache (shared by every
|
|
182
195
|
// VM worker and invalidated with the rest of the host's caches); values cross
|
|
183
196
|
// the bridge as JSON.
|
package/dist/context.d.ts
CHANGED
|
@@ -53,3 +53,8 @@ export declare function createContext<T>(defaultValue: T): Context<T>;
|
|
|
53
53
|
* the list runtime have no provider on the stack otherwise.
|
|
54
54
|
*/
|
|
55
55
|
export declare function krateContextRows<A extends unknown[], R>(entries: Array<[Context<any>, () => unknown]>, render: (...args: A) => R): (...args: A) => R;
|
|
56
|
+
/**
|
|
57
|
+
* Runs fn with the given provider values (getters) on top of their context
|
|
58
|
+
* stacks, as if fn rendered inside those providers.
|
|
59
|
+
*/
|
|
60
|
+
export declare function krateWithContexts<R>(entries: Array<[Context<any>, () => unknown]>, fn: () => R): R;
|
package/dist/context.js
CHANGED
|
@@ -109,6 +109,8 @@ export function createContext(defaultValue) {
|
|
|
109
109
|
ctx.id = id;
|
|
110
110
|
ctx.defaultValue = defaultValue;
|
|
111
111
|
ctx.Provider = Provider;
|
|
112
|
+
// Lets a custom renderer map a provider element back to its context.
|
|
113
|
+
Provider.__kctxOf = ctx;
|
|
112
114
|
ctx.Consumer = Consumer;
|
|
113
115
|
ctx.useContext = useContext;
|
|
114
116
|
ctx.use = useContext;
|
|
@@ -123,21 +125,30 @@ export function createContext(defaultValue) {
|
|
|
123
125
|
* the list runtime have no provider on the stack otherwise.
|
|
124
126
|
*/
|
|
125
127
|
export function krateContextRows(entries, render) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
128
|
+
const wrapped = (...args) => krateWithContexts(entries, () => render(...args));
|
|
129
|
+
// Renderers that evaluate the content later (a custom renderer's deferred
|
|
130
|
+
// components) read the entries to make the same providers visible again.
|
|
131
|
+
wrapped.__kctx = entries;
|
|
132
|
+
return wrapped;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Runs fn with the given provider values (getters) on top of their context
|
|
136
|
+
* stacks, as if fn rendered inside those providers.
|
|
137
|
+
*/
|
|
138
|
+
export function krateWithContexts(entries, fn) {
|
|
139
|
+
const pushed = [];
|
|
140
|
+
for (const [ctx, get] of entries) {
|
|
141
|
+
const stack = contextStack.get(ctx.id);
|
|
142
|
+
if (!stack)
|
|
143
|
+
continue;
|
|
144
|
+
stack.push(new LiveContext(get));
|
|
145
|
+
pushed.push(stack);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
return fn();
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
for (let i = pushed.length - 1; i >= 0; i--)
|
|
152
|
+
pushed[i].pop();
|
|
153
|
+
}
|
|
143
154
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type FlightHooks = {
|
|
2
|
+
isAction(v: unknown): string | undefined;
|
|
3
|
+
isJSX(v: unknown): boolean;
|
|
4
|
+
fail(path: string, what: string, hint?: string): Error;
|
|
5
|
+
};
|
|
6
|
+
export declare function describeValue(v: unknown): string;
|
|
7
|
+
export declare function encodeProp(v: unknown, path: string, hooks: FlightHooks): unknown;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Encoding of plain-data props crossing from a server component to a client
|
|
2
|
+
// component, in the spirit of React Flight. JSON carries strings, finite
|
|
3
|
+
// numbers, booleans, null, arrays and plain objects as themselves; every other
|
|
4
|
+
// supported value becomes a tagged object { __krateT: <tag>, v: <payload> }
|
|
5
|
+
// that the client reviver (clientReviveJS in the compiler) turns back into the
|
|
6
|
+
// real value. A plain object that could be mistaken for a tag or a marker is
|
|
7
|
+
// wrapped in an "O" tag so user data can never collide with the encoding.
|
|
8
|
+
const RESERVED = ["__krateT", "__krateAction", "__krateSlotHTML", "__krateSlotTree", "__krateChildren", "__krateTree"];
|
|
9
|
+
const TYPED_ARRAYS = {
|
|
10
|
+
Int8Array: true, Uint8Array: true, Uint8ClampedArray: true, Int16Array: true, Uint16Array: true,
|
|
11
|
+
Int32Array: true, Uint32Array: true, Float32Array: true, Float64Array: true, BigInt64Array: true, BigUint64Array: true,
|
|
12
|
+
};
|
|
13
|
+
function tag(t, v) {
|
|
14
|
+
return v === undefined ? { __krateT: t } : { __krateT: t, v };
|
|
15
|
+
}
|
|
16
|
+
function toBase64(bytes) {
|
|
17
|
+
let s = "";
|
|
18
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
19
|
+
s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
20
|
+
return btoa(s);
|
|
21
|
+
}
|
|
22
|
+
function bytesOf(buf, off = 0, len = buf.byteLength - off) {
|
|
23
|
+
return toBase64(new Uint8Array(buf, off, len));
|
|
24
|
+
}
|
|
25
|
+
export function describeValue(v) {
|
|
26
|
+
if (typeof v === "function")
|
|
27
|
+
return "a function";
|
|
28
|
+
if (typeof v === "symbol")
|
|
29
|
+
return "a symbol that is not registered with Symbol.for";
|
|
30
|
+
if (v && typeof v === "object") {
|
|
31
|
+
const ctor = Object.getPrototypeOf(v)?.constructor?.name;
|
|
32
|
+
return ctor ? `an instance of ${ctor}` : "a non-plain object";
|
|
33
|
+
}
|
|
34
|
+
return typeof v;
|
|
35
|
+
}
|
|
36
|
+
// encodeProp converts one prop value. A top-level undefined is dropped by the
|
|
37
|
+
// caller (an absent prop); nested ones are kept as an "U" tag.
|
|
38
|
+
export function encodeProp(v, path, hooks) {
|
|
39
|
+
if (v === null || typeof v === "string" || typeof v === "boolean")
|
|
40
|
+
return v;
|
|
41
|
+
if (typeof v === "number") {
|
|
42
|
+
if (Object.is(v, -0))
|
|
43
|
+
return tag("N", "-0");
|
|
44
|
+
if (Number.isNaN(v))
|
|
45
|
+
return tag("N", "NaN");
|
|
46
|
+
if (v === Infinity)
|
|
47
|
+
return tag("N", "Infinity");
|
|
48
|
+
if (v === -Infinity)
|
|
49
|
+
return tag("N", "-Infinity");
|
|
50
|
+
return v;
|
|
51
|
+
}
|
|
52
|
+
if (typeof v === "bigint")
|
|
53
|
+
return tag("B", v.toString());
|
|
54
|
+
if (v === undefined)
|
|
55
|
+
return tag("U");
|
|
56
|
+
if (typeof v === "symbol") {
|
|
57
|
+
const key = Symbol.keyFor(v);
|
|
58
|
+
if (key === undefined)
|
|
59
|
+
throw hooks.fail(path, describeValue(v));
|
|
60
|
+
return tag("Y", key);
|
|
61
|
+
}
|
|
62
|
+
const action = hooks.isAction(v);
|
|
63
|
+
if (action !== undefined)
|
|
64
|
+
return { __krateAction: action };
|
|
65
|
+
if (Array.isArray(v))
|
|
66
|
+
return v.map((x, i) => encodeProp(x, `${path}[${i}]`, hooks));
|
|
67
|
+
if (typeof v === "object") {
|
|
68
|
+
if (hooks.isJSX(v)) {
|
|
69
|
+
throw hooks.fail(path, "JSX", "pass JSX as a prop value, an array of JSX, or as children; JSX nested inside an object cannot be sent to a client component");
|
|
70
|
+
}
|
|
71
|
+
const proto = Object.getPrototypeOf(v);
|
|
72
|
+
if (proto === Date.prototype) {
|
|
73
|
+
const t = v.getTime();
|
|
74
|
+
return tag("D", Number.isNaN(t) ? null : t);
|
|
75
|
+
}
|
|
76
|
+
if (proto === Map.prototype) {
|
|
77
|
+
const entries = [];
|
|
78
|
+
let i = 0;
|
|
79
|
+
for (const [k, x] of v) {
|
|
80
|
+
entries.push([encodeProp(k, `${path}.keys[${i}]`, hooks), encodeProp(x, `${path}.get(${i})`, hooks)]);
|
|
81
|
+
i++;
|
|
82
|
+
}
|
|
83
|
+
return tag("M", entries);
|
|
84
|
+
}
|
|
85
|
+
if (proto === Set.prototype) {
|
|
86
|
+
return tag("S", [...v].map((x, i) => encodeProp(x, `${path}.values[${i}]`, hooks)));
|
|
87
|
+
}
|
|
88
|
+
if (proto === ArrayBuffer.prototype)
|
|
89
|
+
return tag("A", bytesOf(v));
|
|
90
|
+
if (ArrayBuffer.isView(v)) {
|
|
91
|
+
if (v instanceof DataView)
|
|
92
|
+
return tag("V", bytesOf(v.buffer, v.byteOffset, v.byteLength));
|
|
93
|
+
const name = v[Symbol.toStringTag];
|
|
94
|
+
if (TYPED_ARRAYS[name])
|
|
95
|
+
return tag("T", { n: name, b: bytesOf(v.buffer, v.byteOffset, v.byteLength) });
|
|
96
|
+
}
|
|
97
|
+
if (proto !== Object.prototype && proto !== null)
|
|
98
|
+
throw hooks.fail(path, describeValue(v));
|
|
99
|
+
const out = {};
|
|
100
|
+
for (const [k, x] of Object.entries(v))
|
|
101
|
+
out[k] = encodeProp(x, `${path}.${k}`, hooks);
|
|
102
|
+
return RESERVED.some((k) => Object.prototype.hasOwnProperty.call(out, k)) ? tag("O", out) : out;
|
|
103
|
+
}
|
|
104
|
+
throw hooks.fail(path, describeValue(v));
|
|
105
|
+
}
|
package/dist/hints.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
interface HintOptions {
|
|
1
|
+
export interface HintOptions {
|
|
2
2
|
as?: string;
|
|
3
3
|
crossOrigin?: string;
|
|
4
4
|
integrity?: string;
|
|
@@ -9,11 +9,34 @@ interface HintOptions {
|
|
|
9
9
|
referrerPolicy?: string;
|
|
10
10
|
imageSrcSet?: string;
|
|
11
11
|
imageSizes?: string;
|
|
12
|
+
precedence?: string;
|
|
12
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Records head tags the build collected for a component that the server does
|
|
16
|
+
* not render (a real-React client component). Server only; the browser
|
|
17
|
+
* already has them in the document.
|
|
18
|
+
*/
|
|
19
|
+
export declare function krateHintTags(tags: readonly string[]): void;
|
|
13
20
|
export declare function krateHintPreload(href: string, opts?: HintOptions): void;
|
|
14
21
|
export declare function krateHintPreloadModule(href: string, opts?: HintOptions): void;
|
|
15
22
|
export declare function krateHintPreconnect(href: string, opts?: HintOptions): void;
|
|
16
23
|
export declare function krateHintPrefetchDNS(href: string): void;
|
|
17
24
|
export declare function krateHintPreinit(href: string, opts?: HintOptions): void;
|
|
18
25
|
export declare function krateHintPreinitModule(href: string, opts?: HintOptions): void;
|
|
19
|
-
export
|
|
26
|
+
export declare const preload: typeof krateHintPreload;
|
|
27
|
+
export declare const preinit: typeof krateHintPreinit;
|
|
28
|
+
export declare const preconnect: typeof krateHintPreconnect;
|
|
29
|
+
export declare const prefetchDNS: typeof krateHintPrefetchDNS;
|
|
30
|
+
export declare const preloadModule: typeof krateHintPreloadModule;
|
|
31
|
+
export declare const preinitModule: typeof krateHintPreinitModule;
|
|
32
|
+
export declare function flushSync<T>(fn: () => T): T;
|
|
33
|
+
declare const _default: {
|
|
34
|
+
preload: typeof krateHintPreload;
|
|
35
|
+
preinit: typeof krateHintPreinit;
|
|
36
|
+
preconnect: typeof krateHintPreconnect;
|
|
37
|
+
prefetchDNS: typeof krateHintPrefetchDNS;
|
|
38
|
+
preloadModule: typeof krateHintPreloadModule;
|
|
39
|
+
preinitModule: typeof krateHintPreinitModule;
|
|
40
|
+
flushSync: typeof flushSync;
|
|
41
|
+
};
|
|
42
|
+
export default _default;
|
package/dist/hints.js
CHANGED
|
@@ -1,97 +1,171 @@
|
|
|
1
1
|
// React DOM's resource hints (preload, preinit, preconnect, prefetchDNS,
|
|
2
2
|
// preloadModule, preinitModule). React emits <link>/<script> tags into the
|
|
3
|
-
// document head for them
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
// document head for them. In the browser they add the tag once, and nothing
|
|
4
|
+
// when the document already has it (the server HTML put it there). While the
|
|
5
|
+
// server renders, they record the tag on the render scope and the renderer
|
|
6
|
+
// puts the recorded tags into the <head> of the response (withCacheScope).
|
|
7
|
+
// When the build renders an island outside any request, they record the tag
|
|
8
|
+
// in the set the build installs as globalThis.__krate_island_hints, and the
|
|
9
|
+
// build puts it into the head of the page.
|
|
10
|
+
// This module must stay free of imports: island bundles inline it.
|
|
11
|
+
function optAttrs(opts, names) {
|
|
12
|
+
const out = [];
|
|
13
|
+
if (!opts)
|
|
14
|
+
return out;
|
|
15
|
+
for (const name of names) {
|
|
16
|
+
const v = opts[name];
|
|
17
|
+
if (v === undefined || v === null || v === false)
|
|
18
|
+
continue;
|
|
19
|
+
// crossOrigin: "" is a valid value (anonymous); the others need text.
|
|
20
|
+
if (v === '' && name !== 'crossOrigin')
|
|
21
|
+
continue;
|
|
22
|
+
const attr = name === 'crossOrigin' ? 'crossorigin' :
|
|
23
|
+
name === 'fetchPriority' ? 'fetchpriority' :
|
|
24
|
+
name === 'referrerPolicy' ? 'referrerpolicy' :
|
|
25
|
+
name === 'imageSrcSet' ? 'imagesrcset' :
|
|
26
|
+
name === 'imageSizes' ? 'imagesizes' : name;
|
|
27
|
+
if (name === 'crossOrigin') {
|
|
28
|
+
// As React: "use-credentials" or anonymous (""), so react-dom in a
|
|
29
|
+
// real-React island finds the tag by the same selector.
|
|
30
|
+
out.push([attr, v === 'use-credentials' ? v : '']);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
out.push([attr, v === true ? '' : String(v)]);
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
6
36
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
37
|
+
function escapeAttr(v) {
|
|
38
|
+
return v.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
|
|
39
|
+
}
|
|
40
|
+
// The scope of the render that is running on the server, if any. The cache
|
|
41
|
+
// runtime keeps it on globalThis, so every copy of this module sees it.
|
|
42
|
+
function serverScope() {
|
|
43
|
+
const rt = globalThis[Symbol.for('krate.cache.runtime')];
|
|
44
|
+
const store = rt && rt.renderStore;
|
|
45
|
+
return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
|
|
46
|
+
}
|
|
47
|
+
// serverHints is where a hint called outside a browser goes: the running
|
|
48
|
+
// render's scope, else the build's island collector, else nowhere.
|
|
49
|
+
function serverHints() {
|
|
50
|
+
const scope = serverScope();
|
|
51
|
+
if (scope)
|
|
52
|
+
return (scope.hints ?? (scope.hints = new Set()));
|
|
53
|
+
const c = globalThis.__krate_island_hints;
|
|
54
|
+
return c instanceof Set ? c : undefined;
|
|
55
|
+
}
|
|
56
|
+
function absURL(href) {
|
|
57
|
+
try {
|
|
58
|
+
return new URL(href, document.baseURI).href;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return href;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// present reports whether the document already has the tag: the same rel (or
|
|
65
|
+
// script type) and the same URL, however it is spelled. A preload also needs
|
|
66
|
+
// the same `as`, as in React.
|
|
67
|
+
function present(tag, attrs) {
|
|
68
|
+
const get = (name) => {
|
|
69
|
+
for (const [k, v] of attrs)
|
|
70
|
+
if (k === name)
|
|
71
|
+
return v;
|
|
72
|
+
return undefined;
|
|
73
|
+
};
|
|
74
|
+
if (tag === 'script') {
|
|
75
|
+
const src = absURL(get('src') || '');
|
|
76
|
+
const module = get('type') === 'module';
|
|
77
|
+
for (const el of Array.from(document.querySelectorAll('script[src]'))) {
|
|
78
|
+
if (el.src === src && (el.type === 'module') === module)
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const rel = get('rel') || '';
|
|
84
|
+
const href = absURL(get('href') || '');
|
|
85
|
+
const as = get('as');
|
|
86
|
+
for (const el of Array.from(document.querySelectorAll('link[href]'))) {
|
|
87
|
+
if (el.href !== href)
|
|
88
|
+
continue;
|
|
89
|
+
if ((el.getAttribute('rel') || '').toLowerCase() !== rel)
|
|
90
|
+
continue;
|
|
91
|
+
if (rel === 'preload' && (el.getAttribute('as') || '') !== (as || ''))
|
|
92
|
+
continue;
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
function tagText(tag, attrs) {
|
|
98
|
+
const text = attrs.map(([k, v]) => (v === '' && k !== 'crossorigin' ? ' ' + k : ' ' + k + '="' + escapeAttr(v) + '"')).join('');
|
|
99
|
+
return tag === 'link' ? '<link' + text + '>' : '<script' + text + '></script>';
|
|
100
|
+
}
|
|
101
|
+
// emit adds one head tag: to the document in a browser, to the render scope
|
|
102
|
+
// (or the build's collector) on the server. A tag that is already there is
|
|
103
|
+
// not added twice.
|
|
104
|
+
function emit(tag, attrs) {
|
|
105
|
+
if (typeof document === 'undefined') {
|
|
106
|
+
serverHints()?.add(tagText(tag, attrs));
|
|
12
107
|
return;
|
|
13
|
-
const link = document.createElement('link');
|
|
14
|
-
link.rel = rel;
|
|
15
|
-
link.href = href;
|
|
16
|
-
if (opts) {
|
|
17
|
-
if (opts.as)
|
|
18
|
-
link.as = opts.as;
|
|
19
|
-
if (opts.crossOrigin !== undefined)
|
|
20
|
-
link.crossOrigin = opts.crossOrigin;
|
|
21
|
-
if (opts.integrity)
|
|
22
|
-
link.integrity = opts.integrity;
|
|
23
|
-
if (opts.type)
|
|
24
|
-
link.type = opts.type;
|
|
25
|
-
if (opts.media)
|
|
26
|
-
link.media = opts.media;
|
|
27
|
-
if (opts.nonce)
|
|
28
|
-
link.nonce = opts.nonce;
|
|
29
|
-
if (opts.fetchPriority)
|
|
30
|
-
link.setAttribute('fetchpriority', opts.fetchPriority);
|
|
31
|
-
if (opts.referrerPolicy)
|
|
32
|
-
link.referrerPolicy = opts.referrerPolicy;
|
|
33
|
-
if (opts.imageSrcSet)
|
|
34
|
-
link.setAttribute('imagesrcset', opts.imageSrcSet);
|
|
35
|
-
if (opts.imageSizes)
|
|
36
|
-
link.setAttribute('imagesizes', opts.imageSizes);
|
|
37
108
|
}
|
|
38
|
-
|
|
109
|
+
if (present(tag, attrs))
|
|
110
|
+
return;
|
|
111
|
+
const el = document.createElement(tag);
|
|
112
|
+
for (const [k, v] of attrs)
|
|
113
|
+
el.setAttribute(k, v);
|
|
114
|
+
document.head.appendChild(el);
|
|
115
|
+
}
|
|
116
|
+
function link(rel, href, opts, names) {
|
|
117
|
+
if (!href || typeof href !== 'string')
|
|
118
|
+
return;
|
|
119
|
+
emit('link', [['rel', rel], ['href', href], ...optAttrs(opts, names)]);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Records head tags the build collected for a component that the server does
|
|
123
|
+
* not render (a real-React client component). Server only; the browser
|
|
124
|
+
* already has them in the document.
|
|
125
|
+
*/
|
|
126
|
+
export function krateHintTags(tags) {
|
|
127
|
+
if (typeof document !== 'undefined')
|
|
128
|
+
return;
|
|
129
|
+
const hints = serverHints();
|
|
130
|
+
if (hints)
|
|
131
|
+
for (const t of tags)
|
|
132
|
+
hints.add(t);
|
|
39
133
|
}
|
|
40
134
|
export function krateHintPreload(href, opts) {
|
|
41
|
-
|
|
135
|
+
link('preload', href, opts, ['as', 'crossOrigin', 'integrity', 'type', 'media', 'nonce', 'fetchPriority', 'referrerPolicy', 'imageSrcSet', 'imageSizes']);
|
|
42
136
|
}
|
|
43
137
|
export function krateHintPreloadModule(href, opts) {
|
|
44
|
-
|
|
138
|
+
link('modulepreload', href, opts, ['as', 'crossOrigin', 'integrity', 'nonce']);
|
|
45
139
|
}
|
|
46
140
|
export function krateHintPreconnect(href, opts) {
|
|
47
|
-
|
|
141
|
+
link('preconnect', href, opts, ['crossOrigin']);
|
|
48
142
|
}
|
|
49
143
|
export function krateHintPrefetchDNS(href) {
|
|
50
|
-
|
|
144
|
+
link('dns-prefetch', href, undefined, []);
|
|
51
145
|
}
|
|
52
146
|
export function krateHintPreinit(href, opts) {
|
|
53
|
-
if (typeof
|
|
147
|
+
if (!href || typeof href !== 'string')
|
|
54
148
|
return;
|
|
55
149
|
if (opts && opts.as === 'script') {
|
|
56
|
-
|
|
57
|
-
return;
|
|
58
|
-
const s = document.createElement('script');
|
|
59
|
-
s.src = href;
|
|
60
|
-
s.async = true;
|
|
61
|
-
if (opts.crossOrigin !== undefined)
|
|
62
|
-
s.crossOrigin = opts.crossOrigin;
|
|
63
|
-
if (opts.integrity)
|
|
64
|
-
s.integrity = opts.integrity;
|
|
65
|
-
if (opts.nonce)
|
|
66
|
-
s.nonce = opts.nonce;
|
|
67
|
-
document.head.appendChild(s);
|
|
150
|
+
emit('script', [['src', href], ['async', ''], ...optAttrs(opts, ['crossOrigin', 'integrity', 'nonce', 'fetchPriority'])]);
|
|
68
151
|
return;
|
|
69
152
|
}
|
|
70
|
-
// as: "style" (the default use)
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
const link = document.createElement('link');
|
|
74
|
-
link.rel = 'stylesheet';
|
|
75
|
-
link.href = href;
|
|
76
|
-
if (opts && opts.crossOrigin !== undefined)
|
|
77
|
-
link.crossOrigin = opts.crossOrigin;
|
|
78
|
-
if (opts && opts.integrity)
|
|
79
|
-
link.integrity = opts.integrity;
|
|
80
|
-
if (opts && opts.media)
|
|
81
|
-
link.media = opts.media;
|
|
82
|
-
document.head.appendChild(link);
|
|
153
|
+
// as: "style" (the default use) loads the stylesheet.
|
|
154
|
+
link('stylesheet', href, opts, ['crossOrigin', 'integrity', 'media', 'nonce', 'fetchPriority']);
|
|
83
155
|
}
|
|
84
156
|
export function krateHintPreinitModule(href, opts) {
|
|
85
|
-
if (typeof
|
|
157
|
+
if (!href || typeof href !== 'string')
|
|
86
158
|
return;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
159
|
+
emit('script', [['type', 'module'], ['src', href], ['async', ''], ...optAttrs(opts, ['crossOrigin', 'integrity', 'nonce'])]);
|
|
160
|
+
}
|
|
161
|
+
// The react-dom exports a server module can use. flushSync(fn) is fn() here.
|
|
162
|
+
export const preload = krateHintPreload;
|
|
163
|
+
export const preinit = krateHintPreinit;
|
|
164
|
+
export const preconnect = krateHintPreconnect;
|
|
165
|
+
export const prefetchDNS = krateHintPrefetchDNS;
|
|
166
|
+
export const preloadModule = krateHintPreloadModule;
|
|
167
|
+
export const preinitModule = krateHintPreinitModule;
|
|
168
|
+
export function flushSync(fn) {
|
|
169
|
+
return fn();
|
|
97
170
|
}
|
|
171
|
+
export default { preload, preinit, preconnect, prefetchDNS, preloadModule, preinitModule, flushSync };
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { clearPersisted, readPersisted, writePersisted } from './storage.js';
|
|
|
10
10
|
export type { PersistOptions, PersistSpec, PersistStore, StorageLike } from './storage.js';
|
|
11
11
|
export { h, mount, hydrate, insert, clearNodes, disposeNode, krateApplyStyle } from './dom.js';
|
|
12
12
|
export { krateList, krateBindList } from './list.js';
|
|
13
|
+
export type { KrateListValue } from './list.js';
|
|
13
14
|
export { krateHighlightInit, krateHighlight } from './highlight-bind.js';
|
|
14
15
|
export { krateCondSwap, krateCondDetached, krateCondReset, krateSanitizeAttr, BLOCKED_URL } from './slots.js';
|
|
15
16
|
export { Fragment, jsx, jsxs } from './jsx-runtime.js';
|