@krate/runtime 0.5.0-rc.5 → 0.5.0-rc.7
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 +2 -0
- package/dist/cache-scope.js +6 -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/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +4 -188
- package/dist/head-tags.js +5 -2
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +12 -6
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +148 -74
- package/dist/index.d.ts +1 -0
- package/dist/island-swap.d.ts +45 -0
- package/dist/island-swap.js +608 -0
- package/dist/jsx-types.d.ts +7 -2
- 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-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +452 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- 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 +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router.js +293 -23
- package/dist/server-handler.d.ts +20 -1
- package/dist/server-handler.js +208 -92
- package/dist/server.d.ts +24 -17
- package/dist/server.js +435 -101
- package/dist/site-config.d.ts +2 -0
- package/dist/testing.d.ts +4 -1
- package/dist/testing.js +3 -1
- package/package.json +15 -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 -338
- 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 -91
- 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/server.js
CHANGED
|
@@ -6,6 +6,7 @@ import { highlight as highlightCode, trimSpace } from './highlight.js';
|
|
|
6
6
|
import { accessDeniedStatus, isNotFoundSignal, redirectTarget } from './request-context.js';
|
|
7
7
|
import { sha256Hex } from './sha256.js';
|
|
8
8
|
import { markErrorCaught } from './cache-scope.js';
|
|
9
|
+
import { encodeProp } from './flight-props.js';
|
|
9
10
|
export { getActionContext, headers, cookies, redirect } from './action-context.js';
|
|
10
11
|
export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
11
12
|
export { after } from './after.js';
|
|
@@ -136,6 +137,164 @@ function asyncInSyncRender(where) {
|
|
|
136
137
|
function isClassComponent(fn) {
|
|
137
138
|
return typeof fn === "function" && fn.prototype && typeof fn.prototype.render === "function";
|
|
138
139
|
}
|
|
140
|
+
// React element types that are objects or symbols rather than functions. A
|
|
141
|
+
// package compiled for React (Radix's Slot is a forwardRef) hands these to a
|
|
142
|
+
// dynamic tag (`const Comp = asChild ? Slot.Root : "button"`), so the server
|
|
143
|
+
// renderer must run them as components instead of printing them as a tag name.
|
|
144
|
+
const R_ELEMENT = [Symbol.for("react.element"), Symbol.for("react.transitional.element")];
|
|
145
|
+
const R_FORWARD_REF = Symbol.for("react.forward_ref");
|
|
146
|
+
const R_MEMO = Symbol.for("react.memo");
|
|
147
|
+
const R_LAZY = Symbol.for("react.lazy");
|
|
148
|
+
const R_PROVIDER = Symbol.for("react.provider");
|
|
149
|
+
const R_CONTEXT = Symbol.for("react.context");
|
|
150
|
+
const R_CONSUMER = Symbol.for("react.consumer");
|
|
151
|
+
const R_FRAGMENT = Symbol.for("react.fragment");
|
|
152
|
+
const R_SUSPENSE = Symbol.for("react.suspense");
|
|
153
|
+
const exoticCache = new WeakMap();
|
|
154
|
+
function describeType(type) {
|
|
155
|
+
if (typeof type === "symbol")
|
|
156
|
+
return type.toString();
|
|
157
|
+
if (type && typeof type === "object") {
|
|
158
|
+
const t = type;
|
|
159
|
+
const tag = typeof t.$$typeof === "symbol" ? t.$$typeof.toString() : undefined;
|
|
160
|
+
return tag ? `an object (${tag})` : "an object";
|
|
161
|
+
}
|
|
162
|
+
return String(type);
|
|
163
|
+
}
|
|
164
|
+
function notRenderable(type) {
|
|
165
|
+
return new Error(`Cannot render an element whose type is ${describeType(type)}. A server render can run strings, ` +
|
|
166
|
+
`function and class components, and React forwardRef, memo, lazy and context types; ` +
|
|
167
|
+
`pass a component or an HTML tag name instead.`);
|
|
168
|
+
}
|
|
169
|
+
// isSlotType reports Radix's Slot (createSlot names it `<owner>.Slot`). Its
|
|
170
|
+
// own render clones React elements and cannot work on Krate's, so the server
|
|
171
|
+
// uses ServerSlot for it.
|
|
172
|
+
function isSlotType(type) {
|
|
173
|
+
return typeof type.displayName === "string" && /(^|\.)Slot$/.test(type.displayName);
|
|
174
|
+
}
|
|
175
|
+
function childElements(children) {
|
|
176
|
+
return children.filter((c) => c != null && c !== false && !(typeof c === "string" && c.trim() === ""));
|
|
177
|
+
}
|
|
178
|
+
function mergeSlotProps(slot, child) {
|
|
179
|
+
const out = { ...slot, ...child };
|
|
180
|
+
for (const key of Object.keys(child)) {
|
|
181
|
+
const sv = slot[key];
|
|
182
|
+
const cv = child[key];
|
|
183
|
+
if (/^on[A-Z]/.test(key) && typeof sv === "function" && typeof cv === "function") {
|
|
184
|
+
out[key] = (...args) => {
|
|
185
|
+
cv(...args);
|
|
186
|
+
sv(...args);
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
else if (key === "style" && sv && cv && typeof sv === "object" && typeof cv === "object") {
|
|
190
|
+
out[key] = { ...sv, ...cv };
|
|
191
|
+
}
|
|
192
|
+
else if ((key === "className" || key === "class") && sv && cv) {
|
|
193
|
+
out[key] = `${sv} ${cv}`;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
// ServerSlot is Radix's Slot for the server: it renders its single child
|
|
199
|
+
// element with the Slot's own props merged onto it (the child wins, class
|
|
200
|
+
// names join, handlers compose).
|
|
201
|
+
function ServerSlot(allProps) {
|
|
202
|
+
const { children, ref: _ref, ...slotProps } = allProps;
|
|
203
|
+
const kids = childElements(Array.isArray(children) ? children.flat(Infinity) : children == null ? [] : [children]);
|
|
204
|
+
const child = kids.length === 1 ? normalizeNode(kids[0]) : undefined;
|
|
205
|
+
if (!isElement(child))
|
|
206
|
+
return kids;
|
|
207
|
+
return { type: child.type, props: mergeSlotProps(slotProps, child.props), children: child.children };
|
|
208
|
+
}
|
|
209
|
+
// toReactElement converts a React element object (made by a package's own
|
|
210
|
+
// React) into Krate's element shape.
|
|
211
|
+
function isReactElement(v) {
|
|
212
|
+
return v != null && typeof v === "object" && R_ELEMENT.includes(v.$$typeof);
|
|
213
|
+
}
|
|
214
|
+
function normalizeNode(node) {
|
|
215
|
+
if (!isReactElement(node))
|
|
216
|
+
return node;
|
|
217
|
+
const { children, ref: _ref, ...rest } = node.props ?? {};
|
|
218
|
+
const kids = Array.isArray(children) ? children.flat(Infinity) : children != null ? [children] : [];
|
|
219
|
+
return { type: node.type, props: rest, children: kids };
|
|
220
|
+
}
|
|
221
|
+
function foreignContext(type) {
|
|
222
|
+
if (type.$$typeof === R_PROVIDER)
|
|
223
|
+
return { ctx: type._context, provider: true };
|
|
224
|
+
if (type.$$typeof === R_CONSUMER)
|
|
225
|
+
return { ctx: type._context, provider: false };
|
|
226
|
+
if (type._context)
|
|
227
|
+
return { ctx: type._context, provider: false };
|
|
228
|
+
return { ctx: type, provider: type.Provider === type };
|
|
229
|
+
}
|
|
230
|
+
// withContextValue renders under a foreign Provider: consumers below read the
|
|
231
|
+
// value while its children are resolved.
|
|
232
|
+
function scopedContextFn(ctx, provider) {
|
|
233
|
+
if (provider) {
|
|
234
|
+
const Provider = (props) => props.children;
|
|
235
|
+
Provider.__krateForeignProvider = ctx;
|
|
236
|
+
return Provider;
|
|
237
|
+
}
|
|
238
|
+
const Consumer = (props) => consumerChild(props)(ctx._currentValue);
|
|
239
|
+
Consumer.__krateForeignConsumer = ctx;
|
|
240
|
+
return Consumer;
|
|
241
|
+
}
|
|
242
|
+
function consumerChild(props) {
|
|
243
|
+
const render = Array.isArray(props.children) && props.children.length === 1 ? props.children[0] : props.children;
|
|
244
|
+
if (typeof render !== "function")
|
|
245
|
+
throw new Error("A context Consumer needs a function as its child.");
|
|
246
|
+
return render;
|
|
247
|
+
}
|
|
248
|
+
// exoticType resolves a non-string, non-function element type to something
|
|
249
|
+
// the renderer can run. A pending React.lazy throws its promise.
|
|
250
|
+
function exoticType(type) {
|
|
251
|
+
if (typeof type === "string" || typeof type === "function")
|
|
252
|
+
return type;
|
|
253
|
+
if (type === R_FRAGMENT || type === R_SUSPENSE)
|
|
254
|
+
return Fragment;
|
|
255
|
+
if (type == null || typeof type !== "object")
|
|
256
|
+
throw notRenderable(type);
|
|
257
|
+
switch (type.$$typeof) {
|
|
258
|
+
case R_MEMO:
|
|
259
|
+
return exoticType(type.type);
|
|
260
|
+
case R_LAZY:
|
|
261
|
+
return exoticType(type._init(type._payload));
|
|
262
|
+
case R_FORWARD_REF: {
|
|
263
|
+
let fn = exoticCache.get(type);
|
|
264
|
+
if (!fn) {
|
|
265
|
+
fn = isSlotType(type) ? ServerSlot : (props) => type.render(props, props.ref ?? null);
|
|
266
|
+
exoticCache.set(type, fn);
|
|
267
|
+
}
|
|
268
|
+
return fn;
|
|
269
|
+
}
|
|
270
|
+
case R_PROVIDER:
|
|
271
|
+
case R_CONSUMER:
|
|
272
|
+
case R_CONTEXT: {
|
|
273
|
+
let fn = exoticCache.get(type);
|
|
274
|
+
if (!fn) {
|
|
275
|
+
const { ctx, provider } = foreignContext(type);
|
|
276
|
+
fn = scopedContextFn(ctx, provider);
|
|
277
|
+
exoticCache.set(type, fn);
|
|
278
|
+
}
|
|
279
|
+
return fn;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
throw notRenderable(type);
|
|
283
|
+
}
|
|
284
|
+
// runForeignProvider evaluates `run` with a foreign Provider's value in place.
|
|
285
|
+
function withProviderValue(fn, props, run) {
|
|
286
|
+
const ctx = fn.__krateForeignProvider;
|
|
287
|
+
if (!ctx)
|
|
288
|
+
return run();
|
|
289
|
+
const prev = ctx._currentValue;
|
|
290
|
+
ctx._currentValue = props.value;
|
|
291
|
+
try {
|
|
292
|
+
return run();
|
|
293
|
+
}
|
|
294
|
+
finally {
|
|
295
|
+
ctx._currentValue = prev;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
139
298
|
function classFallback(type, instance, error) {
|
|
140
299
|
if (typeof type.getDerivedStateFromError !== "function")
|
|
141
300
|
throw error;
|
|
@@ -199,6 +358,8 @@ function renderNode(node) {
|
|
|
199
358
|
return node.map(renderNode).join("");
|
|
200
359
|
if (typeof node === "object" && "__raw" in node)
|
|
201
360
|
return node.__raw;
|
|
361
|
+
if (isReactElement(node))
|
|
362
|
+
return renderElement(normalizeNode(node));
|
|
202
363
|
if (typeof node === "object" && "type" in node)
|
|
203
364
|
return renderElement(node);
|
|
204
365
|
return "";
|
|
@@ -296,42 +457,70 @@ function isJSXValue(v) {
|
|
|
296
457
|
// render their children eagerly (marked __krateEagerChildren, e.g. Link) get
|
|
297
458
|
// their children and JSX-valued props resolved first, so an async component
|
|
298
459
|
// passed as a child still renders.
|
|
299
|
-
async function resolveTree(node) {
|
|
460
|
+
async function resolveTree(node, vals) {
|
|
300
461
|
if (isThenable(node))
|
|
301
|
-
return resolveTree(await node);
|
|
462
|
+
return resolveTree(await node, vals);
|
|
302
463
|
if (node == null || typeof node !== "object")
|
|
303
464
|
return node;
|
|
304
465
|
if (Array.isArray(node))
|
|
305
|
-
return Promise.all(node.map(resolveTree));
|
|
466
|
+
return Promise.all(node.map((n) => resolveTree(n, vals)));
|
|
306
467
|
if ("__raw" in node)
|
|
307
468
|
return node;
|
|
469
|
+
if (isReactElement(node))
|
|
470
|
+
node = normalizeNode(node);
|
|
308
471
|
if (!isElement(node))
|
|
309
472
|
return node;
|
|
310
473
|
const { type, props, children } = node;
|
|
311
|
-
if (typeof type
|
|
312
|
-
return { type, props, children: (await Promise.all(children.map(resolveTree))) };
|
|
474
|
+
if (typeof type === "string") {
|
|
475
|
+
return { type, props, children: (await Promise.all(children.map((n) => resolveTree(n, vals)))) };
|
|
476
|
+
}
|
|
477
|
+
const step = await expandComponent(node, vals);
|
|
478
|
+
return resolveTree(step.out, step.vals);
|
|
479
|
+
}
|
|
480
|
+
// expandComponent runs one component element (function, class, React exotic
|
|
481
|
+
// type, foreign Provider or Consumer) and returns what it rendered, with the
|
|
482
|
+
// context values in scope for that output.
|
|
483
|
+
async function expandComponent(node, vals) {
|
|
484
|
+
const { type, props, children } = node;
|
|
485
|
+
let fn = type;
|
|
486
|
+
while (typeof fn !== "function") {
|
|
487
|
+
try {
|
|
488
|
+
fn = exoticType(fn);
|
|
489
|
+
}
|
|
490
|
+
catch (e) {
|
|
491
|
+
if (!isThenable(e))
|
|
492
|
+
throw e;
|
|
493
|
+
await e;
|
|
494
|
+
}
|
|
313
495
|
}
|
|
314
|
-
const fn = type;
|
|
315
496
|
if (fn.__krateSuspense)
|
|
316
|
-
return
|
|
497
|
+
return { out: children, vals };
|
|
317
498
|
let callProps = { ...props, children };
|
|
318
499
|
if (fn.__krateEagerChildren) {
|
|
319
|
-
const entries = await Promise.all(Object.entries(props).map(async ([k, v]) => [k, isJSXValue(v) ? await resolveTree(v) : v]));
|
|
320
|
-
callProps = { ...Object.fromEntries(entries), children: await resolveTree(children) };
|
|
500
|
+
const entries = await Promise.all(Object.entries(props).map(async ([k, v]) => [k, isJSXValue(v) ? await resolveTree(v, vals) : v]));
|
|
501
|
+
callProps = { ...Object.fromEntries(entries), children: await resolveTree(children, vals) };
|
|
321
502
|
}
|
|
322
503
|
if (isClassComponent(fn)) {
|
|
323
504
|
const instance = new fn(callProps);
|
|
324
505
|
try {
|
|
325
|
-
return await resolveTree(instance.render());
|
|
506
|
+
return { out: await resolveTree(instance.render(), vals), vals };
|
|
326
507
|
}
|
|
327
508
|
catch (error) {
|
|
328
|
-
return
|
|
509
|
+
return { out: classFallback(fn, instance, error), vals };
|
|
329
510
|
}
|
|
330
511
|
}
|
|
331
|
-
|
|
512
|
+
if (fn.__krateForeignConsumer) {
|
|
513
|
+
const ctx = fn.__krateForeignConsumer;
|
|
514
|
+
return { out: consumerChild(callProps)(vals?.has(ctx) ? vals.get(ctx) : ctx._currentValue), vals };
|
|
515
|
+
}
|
|
516
|
+
// A client boundary also gets the context values in scope (slotValues).
|
|
517
|
+
let out = fn.__krateClient ? fn(callProps, vals) : fn(callProps);
|
|
332
518
|
if (isThenable(out))
|
|
333
519
|
out = await out;
|
|
334
|
-
|
|
520
|
+
if (fn.__krateForeignProvider) {
|
|
521
|
+
return { out, vals: new Map(vals).set(fn.__krateForeignProvider, props.value) };
|
|
522
|
+
}
|
|
523
|
+
return { out, vals };
|
|
335
524
|
}
|
|
336
525
|
// rawTextContent renders the children of <script>/<style>: their text is raw
|
|
337
526
|
// text in HTML, so entity escaping would corrupt JSON-LD and inline code. The
|
|
@@ -351,6 +540,18 @@ function rawTextContent(tag, children) {
|
|
|
351
540
|
function renderElement(el) {
|
|
352
541
|
const { type, props, children } = el;
|
|
353
542
|
// Component
|
|
543
|
+
if (typeof type !== "string" && typeof type !== "function") {
|
|
544
|
+
let fn;
|
|
545
|
+
try {
|
|
546
|
+
fn = exoticType(type);
|
|
547
|
+
}
|
|
548
|
+
catch (e) {
|
|
549
|
+
if (isThenable(e))
|
|
550
|
+
throw asyncInSyncRender("A lazy component");
|
|
551
|
+
throw e;
|
|
552
|
+
}
|
|
553
|
+
return renderElement({ type: fn, props, children });
|
|
554
|
+
}
|
|
354
555
|
if (typeof type === "function") {
|
|
355
556
|
if (isClassComponent(type)) {
|
|
356
557
|
const instance = new type({ ...props, children });
|
|
@@ -364,7 +565,7 @@ function renderElement(el) {
|
|
|
364
565
|
const result = type({ ...props, children });
|
|
365
566
|
if (isThenable(result))
|
|
366
567
|
throw asyncInSyncRender(`<${componentName(type)}>`);
|
|
367
|
-
return renderNode(result);
|
|
568
|
+
return withProviderValue(type, props, () => renderNode(result));
|
|
368
569
|
}
|
|
369
570
|
// HTML element
|
|
370
571
|
const tag = type;
|
|
@@ -433,59 +634,51 @@ export function lazy(loader) {
|
|
|
433
634
|
return jsx(C, props);
|
|
434
635
|
};
|
|
435
636
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
return typeof v;
|
|
637
|
+
// slotValues is the ContextValues entry holding the client context forwarded
|
|
638
|
+
// into a server-rendered slot of a client component: nested client
|
|
639
|
+
// components render inside it, as they would in one React tree.
|
|
640
|
+
const slotValues = {};
|
|
641
|
+
let slotSeq = 0;
|
|
642
|
+
function deferSlot(ctx, node, done) {
|
|
643
|
+
slotSeq = (slotSeq + 1) % 1e9;
|
|
644
|
+
const id = slotSeq.toString(36) + Math.random().toString(36).slice(2, 8);
|
|
645
|
+
const token = `<!--krate-slot:${id}-->`;
|
|
646
|
+
ctx.slots.push({ token, id, node, done: (html) => done(html, id), vals: ctx.vals });
|
|
647
|
+
return token;
|
|
448
648
|
}
|
|
649
|
+
// slotClose is the marker that ends the HTML of the slot with this id.
|
|
650
|
+
const slotClose = (id) => `<!--/krate-slot:${id}-->`;
|
|
651
|
+
// mergeSlotValues layers the values a component provides at a slot over the
|
|
652
|
+
// ones forwarded into the component.
|
|
653
|
+
function mergeSlotValues(outer, own) {
|
|
654
|
+
if (!own)
|
|
655
|
+
return outer;
|
|
656
|
+
if (!outer || outer.length === 0)
|
|
657
|
+
return own;
|
|
658
|
+
const keys = new Set(own.map((e) => e.k));
|
|
659
|
+
return outer.filter((e) => !keys.has(e.k)).concat(own);
|
|
660
|
+
}
|
|
661
|
+
const flightIsAction = (v) => (isServerAction(v) ? v.actionUrl : undefined);
|
|
662
|
+
const flightIsJSX = (v) => isElement(v) || "__raw" in v;
|
|
449
663
|
// serializeClientProp converts a prop crossing from the server to a client
|
|
450
|
-
// component into JSON-safe data
|
|
451
|
-
// chunk turns back into callable
|
|
452
|
-
//
|
|
664
|
+
// component into JSON-safe data (see flight-props.ts for the encoding). Server
|
|
665
|
+
// actions become references the client chunk turns back into callable
|
|
666
|
+
// proxies; anything JSON and the tag encoding cannot carry faithfully throws
|
|
667
|
+
// with the offending prop path.
|
|
453
668
|
function serializeClientProp(v, path, component) {
|
|
454
|
-
if (v === null || typeof v === "string" || typeof v === "boolean")
|
|
455
|
-
return v;
|
|
456
|
-
if (typeof v === "number") {
|
|
457
|
-
if (!Number.isFinite(v))
|
|
458
|
-
throw clientPropError(path, component, `the number ${v}`);
|
|
459
|
-
return v;
|
|
460
|
-
}
|
|
461
669
|
if (v === undefined)
|
|
462
670
|
return undefined;
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
if (isElement(v) || "__raw" in v) {
|
|
469
|
-
throw clientPropError(path, component, "JSX", "only `children` can carry server-rendered JSX into a client component");
|
|
470
|
-
}
|
|
471
|
-
const proto = Object.getPrototypeOf(v);
|
|
472
|
-
if (proto !== Object.prototype && proto !== null)
|
|
473
|
-
throw clientPropError(path, component, describeValue(v));
|
|
474
|
-
const out = {};
|
|
475
|
-
for (const [k, x] of Object.entries(v)) {
|
|
476
|
-
const sv = serializeClientProp(x, `${path}.${k}`, component);
|
|
477
|
-
if (sv !== undefined)
|
|
478
|
-
out[k] = sv;
|
|
479
|
-
}
|
|
480
|
-
return out;
|
|
481
|
-
}
|
|
482
|
-
throw clientPropError(path, component, describeValue(v));
|
|
671
|
+
return encodeProp(v, path, {
|
|
672
|
+
isAction: flightIsAction,
|
|
673
|
+
isJSX: flightIsJSX,
|
|
674
|
+
fail: (p, what, hint) => clientPropError(p, component, what, hint),
|
|
675
|
+
});
|
|
483
676
|
}
|
|
484
677
|
function clientPropError(path, component, what, hint) {
|
|
485
678
|
return new Error(`Prop \`${path}\` passed to client component <${component}> is ${what}, which cannot be sent from a server ` +
|
|
486
679
|
`component to the client. ` +
|
|
487
|
-
(hint || "Pass
|
|
488
|
-
"move event handlers and class instances into the client component."));
|
|
680
|
+
(hint || "Pass serializable data (strings, numbers, booleans, null, undefined, bigint, Date, Map, Set, typed arrays, " +
|
|
681
|
+
"plain objects, arrays) or a server action; move event handlers and class instances into the client component."));
|
|
489
682
|
}
|
|
490
683
|
function clientRegistry() {
|
|
491
684
|
const g = globalThis;
|
|
@@ -508,12 +701,44 @@ function hasClientElement(node) {
|
|
|
508
701
|
return true;
|
|
509
702
|
return typeof node.type === "string" && node.children.some(hasClientElement);
|
|
510
703
|
}
|
|
511
|
-
|
|
704
|
+
// isSlotValue reports whether a prop holds server-rendered JSX (an element, raw
|
|
705
|
+
// HTML, or an array containing either). Such a prop is a slot: it is rendered
|
|
706
|
+
// on the server and revived on the client like `children`.
|
|
707
|
+
function isSlotValue(v) {
|
|
708
|
+
if (Array.isArray(v))
|
|
709
|
+
return v.some(isSlotValue);
|
|
710
|
+
return v != null && typeof v === "object" && (isElement(v) || "__raw" in v);
|
|
711
|
+
}
|
|
712
|
+
// slotProp renders a JSX-valued prop. A slot without client components of its
|
|
713
|
+
// own travels as HTML; one with them (or any slot when tree is set) travels as
|
|
714
|
+
// a tree that keeps those components in the island's Preact tree.
|
|
715
|
+
async function slotProp(v, ctx, tree) {
|
|
716
|
+
if (tree || hasClientElement(v)) {
|
|
717
|
+
const nested = { hydrate: false, component: ctx.component, slots: ctx.slots, vals: ctx.vals };
|
|
718
|
+
const out = await toFlight(v, nested);
|
|
719
|
+
if (nested.hydrate)
|
|
720
|
+
ctx.hydrate = true;
|
|
721
|
+
return { __krateSlotTree: out };
|
|
722
|
+
}
|
|
723
|
+
const slot = { __krateSlotHTML: "" };
|
|
724
|
+
slot.__krateSlotHTML = deferSlot(ctx, v, (html, id) => {
|
|
725
|
+
if (html !== null)
|
|
726
|
+
slot.__krateSlotHTML = html;
|
|
727
|
+
else {
|
|
728
|
+
delete slot.__krateSlotHTML;
|
|
729
|
+
slot.__krateSlotRef = id;
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
return slot;
|
|
733
|
+
}
|
|
734
|
+
// clientProps serializes the props of a client component, rendering the
|
|
735
|
+
// JSX-valued ones (any name, not only `children`) as slots.
|
|
736
|
+
async function clientProps(props, ctx, tree) {
|
|
512
737
|
const out = {};
|
|
513
738
|
for (const [k, v] of Object.entries(props)) {
|
|
514
739
|
if (k === "key" || k === "children")
|
|
515
740
|
continue;
|
|
516
|
-
const sv = serializeClientProp(v, k, ctx.component);
|
|
741
|
+
const sv = isSlotValue(v) ? await slotProp(v, ctx, tree) : serializeClientProp(v, k, ctx.component);
|
|
517
742
|
if (sv !== undefined)
|
|
518
743
|
out[k] = sv;
|
|
519
744
|
}
|
|
@@ -523,9 +748,15 @@ function flightProps(props, ctx) {
|
|
|
523
748
|
// that keeps nested client components as components, so they render inside the
|
|
524
749
|
// parent's Preact tree and share its context (Radix Dialog.Root with its
|
|
525
750
|
// Trigger and Content). Everything else is host markup or server-rendered HTML.
|
|
526
|
-
|
|
751
|
+
//
|
|
752
|
+
// With inline set (the children of an asChild component) a server component
|
|
753
|
+
// at the top of a child is run here instead of becoming HTML, until a host
|
|
754
|
+
// element or client component comes out: Radix's Slot clones that element and
|
|
755
|
+
// merges its handlers, ref and ARIA props into it, as it does with the React
|
|
756
|
+
// element RSC hands it. Client components in its output stay in the tree.
|
|
757
|
+
async function toFlight(node, ctx, inline = false) {
|
|
527
758
|
if (isThenable(node))
|
|
528
|
-
return toFlight(await node, ctx);
|
|
759
|
+
return toFlight(await node, ctx, inline);
|
|
529
760
|
if (node == null || typeof node === "boolean")
|
|
530
761
|
return [];
|
|
531
762
|
if (typeof node === "string" || typeof node === "number")
|
|
@@ -533,18 +764,20 @@ async function toFlight(node, ctx) {
|
|
|
533
764
|
if (Array.isArray(node)) {
|
|
534
765
|
const out = [];
|
|
535
766
|
for (const n of node)
|
|
536
|
-
out.push(...(await toFlight(n, ctx)));
|
|
767
|
+
out.push(...(await toFlight(n, ctx, inline)));
|
|
537
768
|
return out;
|
|
538
769
|
}
|
|
539
770
|
if (typeof node !== "object")
|
|
540
771
|
return [];
|
|
541
772
|
if ("__raw" in node)
|
|
542
|
-
return [
|
|
773
|
+
return [htmlSlot(ctx, node)];
|
|
774
|
+
if (inline && isReactElement(node))
|
|
775
|
+
node = normalizeNode(node);
|
|
543
776
|
if (!isElement(node))
|
|
544
777
|
return [];
|
|
545
778
|
const { type, props, children } = node;
|
|
546
779
|
if (typeof type === "string") {
|
|
547
|
-
return [{ $: "e", t: type, p:
|
|
780
|
+
return [{ $: "e", t: type, p: await clientProps(props, ctx, true), c: await toFlight(children, ctx) }];
|
|
548
781
|
}
|
|
549
782
|
if (type === Link) {
|
|
550
783
|
return [{ $: "e", t: "a", p: linkAttributes({ ...props, children }), c: await toFlight(children, ctx) }];
|
|
@@ -554,12 +787,72 @@ async function toFlight(node, ctx) {
|
|
|
554
787
|
if (ref.hydrate)
|
|
555
788
|
ctx.hydrate = true;
|
|
556
789
|
const nested = { ...ctx, component: ref.exportName };
|
|
557
|
-
const c = await toFlight(children, nested);
|
|
790
|
+
const c = await toFlight(children, nested, !!props.asChild);
|
|
558
791
|
if (nested.hydrate)
|
|
559
792
|
ctx.hydrate = true;
|
|
560
|
-
|
|
793
|
+
if (props.asChild)
|
|
794
|
+
warnUncloneable(ref.exportName, c);
|
|
795
|
+
return [{ $: "c", m: ref.moduleId, e: ref.exportName, s: ref.src, p: await clientProps(props, nested, true), c }];
|
|
796
|
+
}
|
|
797
|
+
if (inline) {
|
|
798
|
+
const step = await expandComponent(node, ctx.vals);
|
|
799
|
+
const nested = { ...ctx, vals: step.vals };
|
|
800
|
+
const out = await toFlight(step.out, nested, true);
|
|
801
|
+
if (nested.hydrate)
|
|
802
|
+
ctx.hydrate = true;
|
|
803
|
+
return out;
|
|
561
804
|
}
|
|
562
|
-
return [
|
|
805
|
+
return [htmlSlot(ctx, node)];
|
|
806
|
+
}
|
|
807
|
+
// htmlSlot is a server-rendered part of a children tree: { $: "h", h: html },
|
|
808
|
+
// or { $: "h", r: id } once the component has rendered it into the document.
|
|
809
|
+
function htmlSlot(ctx, node) {
|
|
810
|
+
const slot = { $: "h", h: "" };
|
|
811
|
+
slot.h = deferSlot(ctx, node, (html, id) => {
|
|
812
|
+
if (html !== null)
|
|
813
|
+
slot.h = html;
|
|
814
|
+
else {
|
|
815
|
+
delete slot.h;
|
|
816
|
+
slot.r = id;
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
return slot;
|
|
820
|
+
}
|
|
821
|
+
// propsScript is the JSON script that carries a real-React island's props,
|
|
822
|
+
// placed just before its <krate-island>. Every "<" is escaped, so no
|
|
823
|
+
// "</script" or "<!--" in a value can end the script or open a comment.
|
|
824
|
+
function propsScript(data) {
|
|
825
|
+
return `<script type="application/json" data-krate-props>${JSON.stringify(data).replace(/</g, "\\u003c")}</script>`;
|
|
826
|
+
}
|
|
827
|
+
// ISLAND_OPEN starts the comment that opens a Preact island; ISLAND_CLOSE is
|
|
828
|
+
// the comment that ends it. The island's HTML sits between them with no
|
|
829
|
+
// element of its own, so child combinators and sibling selectors of the
|
|
830
|
+
// parent (space-y-*, divide-*, > :first-child) see the component's root
|
|
831
|
+
// nodes, as in React.
|
|
832
|
+
export const ISLAND_OPEN = "krate-island:";
|
|
833
|
+
export const ISLAND_CLOSE = "<!--/krate-island-->";
|
|
834
|
+
// islandOpen is the opening comment of a Preact island: its module, export,
|
|
835
|
+
// chunk and props as JSON. "<", ">" and "--" are escaped (they occur only in
|
|
836
|
+
// strings), so no value can end the comment or open another one.
|
|
837
|
+
function islandOpen(moduleId, exportName, src, props) {
|
|
838
|
+
const json = JSON.stringify({ m: moduleId, e: exportName, s: src, p: props })
|
|
839
|
+
.replace(/</g, "\\u003c")
|
|
840
|
+
.replace(/>/g, "\\u003e")
|
|
841
|
+
.replace(/--/g, "-\\u002d");
|
|
842
|
+
return `<!--${ISLAND_OPEN}${json}-->`;
|
|
843
|
+
}
|
|
844
|
+
const warnedUncloneable = new Set();
|
|
845
|
+
// warnUncloneable reports an asChild component whose child reached the
|
|
846
|
+
// client as server-rendered HTML (raw HTML, a deferred slot), which its Slot
|
|
847
|
+
// cannot clone or merge props into.
|
|
848
|
+
function warnUncloneable(component, tree) {
|
|
849
|
+
if (!tree.some((n) => n != null && typeof n === "object" && n.$ === "h"))
|
|
850
|
+
return;
|
|
851
|
+
if (warnedUncloneable.has(component))
|
|
852
|
+
return;
|
|
853
|
+
warnedUncloneable.add(component);
|
|
854
|
+
console.warn(`[krate] <${component} asChild> received a child rendered to HTML, so it cannot merge its props and handlers ` +
|
|
855
|
+
`into it. Pass a single element or a component that renders one (not raw HTML) as the child.`);
|
|
563
856
|
}
|
|
564
857
|
function wrapClientNamespace(moduleId, exportName, src, ns, render, hydrate) {
|
|
565
858
|
const out = {};
|
|
@@ -582,13 +875,19 @@ function wrapClientNamespace(moduleId, exportName, src, ns, render, hydrate) {
|
|
|
582
875
|
* clientBoundary wraps an export of a `"use client"` module, or a component
|
|
583
876
|
* of a React package, for the server bundle of a page that renders server
|
|
584
877
|
* components. The server renders the component's initial HTML (render) and,
|
|
585
|
-
* when it hydrates, puts it
|
|
586
|
-
* serialized props
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
878
|
+
* when it hydrates, puts it between <!--krate-island:{...}--> (module,
|
|
879
|
+
* export, chunk and serialized props) and <!--/krate-island--> with no
|
|
880
|
+
* element around it; the per-module client chunk at src hydrates those nodes
|
|
881
|
+
* in the browser. A real-React island (flight) gets a <krate-island> element
|
|
882
|
+
* instead, preceded by a JSON script with its props, because createRoot needs
|
|
883
|
+
* a container. A component without state or effects (hydrate
|
|
884
|
+
* false, an icon) is plain HTML. Children are rendered on the server into the
|
|
885
|
+
* component's HTML and adopted there by the browser (see DeferredSlot), or,
|
|
886
|
+
* when they hold client components of their own, sent as a tree that keeps
|
|
887
|
+
* those components in the same Preact tree. The children of an asChild component
|
|
888
|
+
* always travel as a tree, with a server component at the top of a child run
|
|
889
|
+
* to the element it renders, so the component's Slot can clone it. A
|
|
890
|
+
* namespace export (Radix's Dialog) is wrapped member by member. With `flight` the children always travel as a
|
|
592
891
|
* tree (a real-React island rebuilds them as elements, not HTML).
|
|
593
892
|
*/
|
|
594
893
|
export function clientBoundary(moduleId, exportName, src, impl, render, hydrate = true, flight = false) {
|
|
@@ -598,34 +897,54 @@ export function clientBoundary(moduleId, exportName, src, impl, render, hydrate
|
|
|
598
897
|
if (typeof impl !== "function")
|
|
599
898
|
return impl;
|
|
600
899
|
const name = impl.displayName || impl.name || exportName;
|
|
601
|
-
const Boundary = async (props) => {
|
|
900
|
+
const Boundary = async (props, vals) => {
|
|
602
901
|
const { children, ...rest } = props || {};
|
|
603
|
-
const
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
continue;
|
|
607
|
-
const sv = serializeClientProp(v, k, name);
|
|
608
|
-
if (sv !== undefined)
|
|
609
|
-
data[k] = sv;
|
|
610
|
-
}
|
|
902
|
+
const slots = [];
|
|
903
|
+
const slotCtx = { hydrate: false, component: name, slots, vals };
|
|
904
|
+
const data = await clientProps(rest, slotCtx, flight);
|
|
611
905
|
const hasChildren = children != null && !(Array.isArray(children) && children.length === 0);
|
|
612
|
-
let hydrates = hydrate;
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
906
|
+
let hydrates = hydrate || slotCtx.hydrate;
|
|
907
|
+
// An asChild component clones its child, so the child travels as a tree.
|
|
908
|
+
const asChild = !!rest.asChild;
|
|
909
|
+
if (hasChildren && (flight || asChild || hasClientElement(children))) {
|
|
910
|
+
const ctx = { hydrate: false, component: exportName, slots, vals };
|
|
911
|
+
data.__krateTree = await toFlight(children, ctx, asChild);
|
|
617
912
|
if (ctx.hydrate)
|
|
618
913
|
hydrates = true;
|
|
914
|
+
if (asChild)
|
|
915
|
+
warnUncloneable(exportName, data.__krateTree);
|
|
619
916
|
}
|
|
620
917
|
else if (hasChildren) {
|
|
621
|
-
|
|
622
|
-
data.__krateChildren = childrenHTML;
|
|
918
|
+
data.__krateChildren = deferSlot(slotCtx, children, (html, id) => (data.__krateChildren = html ?? { __krateSlotRef: id }));
|
|
623
919
|
}
|
|
624
|
-
const
|
|
920
|
+
const fwd = vals?.get(slotValues);
|
|
921
|
+
const capture = {};
|
|
922
|
+
let html = render(impl, data, null, fwd, capture);
|
|
923
|
+
const rendered = await Promise.all(slots.map(async (s) => {
|
|
924
|
+
const scope = mergeSlotValues(fwd, capture[s.token]);
|
|
925
|
+
const base = s.vals ?? vals;
|
|
926
|
+
const inner = scope ? new Map(base).set(slotValues, scope) : base;
|
|
927
|
+
return renderNode(await resolveTree(s.node, inner));
|
|
928
|
+
}));
|
|
929
|
+
// A slot whose token is in the render is in the document: the browser
|
|
930
|
+
// adopts those nodes, so they are not sent again inside the props. An
|
|
931
|
+
// island keeps the token as the slot's open marker and closes it after
|
|
932
|
+
// the HTML; a component without an island needs no markers.
|
|
933
|
+
const placed = slots.map((s) => html.includes(s.token));
|
|
934
|
+
slots.forEach((s, i) => {
|
|
935
|
+
s.done(placed[i] ? null : rendered[i]);
|
|
936
|
+
html = html.split(s.token).join(hydrates ? s.token + rendered[i] + slotClose(s.id) : rendered[i]);
|
|
937
|
+
});
|
|
625
938
|
if (!hydrates)
|
|
626
939
|
return raw(html);
|
|
627
|
-
|
|
628
|
-
|
|
940
|
+
// A real-React island (flight) mounts with createRoot, which needs an
|
|
941
|
+
// element of its own; a Preact island hydrates the nodes between its two
|
|
942
|
+
// comments in place.
|
|
943
|
+
if (!flight)
|
|
944
|
+
return raw(islandOpen(moduleId, exportName, src, data) + html + ISLAND_CLOSE);
|
|
945
|
+
return raw(propsScript(data) +
|
|
946
|
+
`<krate-island style="display:contents" data-module="${escapeHTML(moduleId)}" data-export="${escapeHTML(exportName)}" ` +
|
|
947
|
+
`data-src="${escapeHTML(src)}">${html}</krate-island>`);
|
|
629
948
|
};
|
|
630
949
|
Object.defineProperty(Boundary, "name", { value: name });
|
|
631
950
|
const ref = { moduleId, exportName, src, impl, hydrate };
|
|
@@ -644,16 +963,15 @@ export const HEAD_CLOSE = "<!--/krate-head-->";
|
|
|
644
963
|
export function Head(props) {
|
|
645
964
|
return [raw(HEAD_OPEN), props.children ?? "", raw(HEAD_CLOSE)];
|
|
646
965
|
}
|
|
647
|
-
// Script
|
|
966
|
+
// Script and Style render the element in place with every attribute (src,
|
|
967
|
+
// defer, async, data-*, nonce, ...), as the static compiler does. Their
|
|
968
|
+
// children are raw text. `strategy` is next/script's and has no HTML meaning.
|
|
648
969
|
export function Script(props) {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
}
|
|
652
|
-
return `<script>${props.children ?? ""}</script>`;
|
|
970
|
+
const { strategy: _strategy, ...rest } = props;
|
|
971
|
+
return jsx("script", rest);
|
|
653
972
|
}
|
|
654
|
-
// Style - renders a <style> tag.
|
|
655
973
|
export function Style(props) {
|
|
656
|
-
return
|
|
974
|
+
return jsx("style", props);
|
|
657
975
|
}
|
|
658
976
|
// linkAttributes computes the attributes of a <Link> anchor. An empty string
|
|
659
977
|
// stands for a boolean attribute.
|
|
@@ -692,6 +1010,22 @@ function linkAttributes(props) {
|
|
|
692
1010
|
attrs.rel = "noopener noreferrer";
|
|
693
1011
|
if (rest.className)
|
|
694
1012
|
attrs.class = String(rest.className);
|
|
1013
|
+
// Every other attribute reaches the anchor, so data-*, aria-*, class and
|
|
1014
|
+
// style that a Slot (asChild) merges onto a Link are not lost.
|
|
1015
|
+
for (const [key, v] of Object.entries(rest)) {
|
|
1016
|
+
const name = ATTR_ALIASES[key] || key;
|
|
1017
|
+
if (name in attrs || v == null || v === false || typeof v === "function")
|
|
1018
|
+
continue;
|
|
1019
|
+
if (key === "key" || key === "ref" || key === "className" || key === "dangerouslySetInnerHTML" || key.startsWith("on") || key.startsWith("__"))
|
|
1020
|
+
continue;
|
|
1021
|
+
if (key === "style" && typeof v === "object") {
|
|
1022
|
+
const css = styleString(v);
|
|
1023
|
+
if (css)
|
|
1024
|
+
attrs.style = css;
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
attrs[name] = v === true ? "" : String(v);
|
|
1028
|
+
}
|
|
695
1029
|
return attrs;
|
|
696
1030
|
}
|
|
697
1031
|
// Link - renders an <a> tag wired for SPA navigation (server renderer).
|