@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
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `react` module of a server bundle (an SSR page or server component).
|
|
3
|
+
*
|
|
4
|
+
* The compiler maps `import { ... } from "react"` in a server bundle onto this
|
|
5
|
+
* module. It exports the part of React's API that has a meaning while a server
|
|
6
|
+
* renders: effects are no-ops (React's server renderer never runs them),
|
|
7
|
+
* `cache` memoizes per request, and the transition/optimistic/action hooks
|
|
8
|
+
* return their initial state. Names that would behave differently from React
|
|
9
|
+
* here (useState, useRef, createContext, ...) are not exported, so importing
|
|
10
|
+
* them fails the build instead of silently misbehaving.
|
|
11
|
+
*/
|
|
12
|
+
import { Suspense, Fragment, jsx, lazy, createSignal, createEffect, createMemo, onCleanup, onMount } from './server.js';
|
|
13
|
+
import { currentRenderScope } from './cache-scope.js';
|
|
14
|
+
export { Suspense, Fragment, lazy, createSignal, createEffect, createMemo, onCleanup, onMount };
|
|
15
|
+
/** Effects never run while a server renders. */
|
|
16
|
+
export function useEffect(_effect, _deps) { }
|
|
17
|
+
export function useLayoutEffect(_effect, _deps) { }
|
|
18
|
+
export function useInsertionEffect(_effect, _deps) { }
|
|
19
|
+
export function createElement(type, props, ...children) {
|
|
20
|
+
const p = { ...(props || {}) };
|
|
21
|
+
delete p.key;
|
|
22
|
+
delete p.ref;
|
|
23
|
+
if (children.length === 1)
|
|
24
|
+
p.children = children[0];
|
|
25
|
+
else if (children.length > 1)
|
|
26
|
+
p.children = children;
|
|
27
|
+
return jsx(type, p);
|
|
28
|
+
}
|
|
29
|
+
export function forwardRef(render) {
|
|
30
|
+
return (props) => render(props, null);
|
|
31
|
+
}
|
|
32
|
+
export function memo(component, _compare) {
|
|
33
|
+
return component;
|
|
34
|
+
}
|
|
35
|
+
export function startTransition(fn) {
|
|
36
|
+
fn();
|
|
37
|
+
}
|
|
38
|
+
export function useTransition() {
|
|
39
|
+
return [false, startTransition];
|
|
40
|
+
}
|
|
41
|
+
export function useOptimistic(passthrough, _reducer) {
|
|
42
|
+
return [passthrough, () => { }];
|
|
43
|
+
}
|
|
44
|
+
export function useActionState(_action, initialState, _permalink) {
|
|
45
|
+
return [initialState, () => { }, false];
|
|
46
|
+
}
|
|
47
|
+
let idCounter = 0;
|
|
48
|
+
/** Ids are unique within the process; server-rendered output is never hydrated by React. */
|
|
49
|
+
export function useId() {
|
|
50
|
+
return ':R' + (idCounter++).toString(36) + ':';
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* use(promise) cannot suspend a synchronous server render. A promise that
|
|
54
|
+
* already settled through React's own bookkeeping is read; anything else
|
|
55
|
+
* points the author at an async component.
|
|
56
|
+
*/
|
|
57
|
+
export function use(usable) {
|
|
58
|
+
const u = usable;
|
|
59
|
+
if (u && typeof u.then === 'function') {
|
|
60
|
+
if (u.status === 'fulfilled')
|
|
61
|
+
return u.value;
|
|
62
|
+
if (u.status === 'rejected')
|
|
63
|
+
throw u.reason;
|
|
64
|
+
throw new Error('use(promise) cannot wait for a promise while a Krate server renders. Make the component async and await the promise.');
|
|
65
|
+
}
|
|
66
|
+
if (u && ('_currentValue' in u || 'defaultValue' in u))
|
|
67
|
+
return (u._currentValue ?? u.defaultValue);
|
|
68
|
+
throw new Error('use() received something that is neither a promise nor a context.');
|
|
69
|
+
}
|
|
70
|
+
const perRequest = new WeakMap();
|
|
71
|
+
/**
|
|
72
|
+
* React's cache(): memoizes fn by argument identity for the request being
|
|
73
|
+
* rendered. Results and thrown errors are both remembered, so a promise
|
|
74
|
+
* returned by fn is shared by every caller in the request. Outside a render
|
|
75
|
+
* (an API route, module scope) there is no request to scope the memo to, so
|
|
76
|
+
* fn runs on every call, as in React.
|
|
77
|
+
*/
|
|
78
|
+
export function cache(fn) {
|
|
79
|
+
const wrapped = function (...args) {
|
|
80
|
+
const scope = currentRenderScope();
|
|
81
|
+
if (!scope)
|
|
82
|
+
return fn.apply(this, args);
|
|
83
|
+
let byFn = perRequest.get(scope);
|
|
84
|
+
if (!byFn) {
|
|
85
|
+
byFn = new WeakMap();
|
|
86
|
+
perRequest.set(scope, byFn);
|
|
87
|
+
}
|
|
88
|
+
let node = byFn.get(wrapped);
|
|
89
|
+
if (!node) {
|
|
90
|
+
node = { done: false };
|
|
91
|
+
byFn.set(wrapped, node);
|
|
92
|
+
}
|
|
93
|
+
for (const arg of args) {
|
|
94
|
+
if (!node.next)
|
|
95
|
+
node.next = new Map();
|
|
96
|
+
let child = node.next.get(arg);
|
|
97
|
+
if (!child) {
|
|
98
|
+
child = { done: false };
|
|
99
|
+
node.next.set(arg, child);
|
|
100
|
+
}
|
|
101
|
+
node = child;
|
|
102
|
+
}
|
|
103
|
+
if (node.done) {
|
|
104
|
+
if (node.failed)
|
|
105
|
+
throw node.error;
|
|
106
|
+
return node.value;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
node.value = fn.apply(this, args);
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
node.failed = true;
|
|
113
|
+
node.error = e;
|
|
114
|
+
node.done = true;
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
node.done = true;
|
|
118
|
+
return node.value;
|
|
119
|
+
};
|
|
120
|
+
return wrapped;
|
|
121
|
+
}
|
|
122
|
+
export default {
|
|
123
|
+
Suspense, Fragment, lazy, createElement, forwardRef, memo, cache, use, useId, startTransition, useTransition,
|
|
124
|
+
useOptimistic, useActionState, useEffect, useLayoutEffect, useInsertionEffect,
|
|
125
|
+
};
|
package/dist/router.js
CHANGED
|
@@ -455,6 +455,15 @@ function setupSpeculationRules() {
|
|
|
455
455
|
},
|
|
456
456
|
],
|
|
457
457
|
});
|
|
458
|
+
// A CSP sent as a header cannot be read from here: when it blocks the
|
|
459
|
+
// inline script, drop it and leave prefetching to the JS path.
|
|
460
|
+
const onViolation = (e) => {
|
|
461
|
+
if (e && e.blockedURI === 'inline' && /^script-src/.test(String(e.effectiveDirective || e.violatedDirective || ''))) {
|
|
462
|
+
script.remove();
|
|
463
|
+
document.removeEventListener('securitypolicyviolation', onViolation);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
document.addEventListener('securitypolicyviolation', onViolation);
|
|
458
467
|
document.head.appendChild(script);
|
|
459
468
|
}
|
|
460
469
|
catch {
|
package/dist/server-handler.js
CHANGED
|
@@ -467,15 +467,23 @@ export function createKrateHandler(options) {
|
|
|
467
467
|
let next = false;
|
|
468
468
|
let rewrite = "";
|
|
469
469
|
const hdrs = [];
|
|
470
|
+
let keep = null;
|
|
471
|
+
const vals = new Map();
|
|
470
472
|
for (const [k, v] of headerPairs(result.headers)) {
|
|
471
473
|
if (k === "x-middleware-next")
|
|
472
474
|
next = true;
|
|
473
475
|
else if (k === "x-middleware-rewrite")
|
|
474
476
|
rewrite = v;
|
|
477
|
+
else if (k === "x-middleware-override-headers")
|
|
478
|
+
keep = new Set(v.split(",").map((n) => n.trim().toLowerCase()).filter(Boolean));
|
|
479
|
+
else if (k.startsWith("x-middleware-request-"))
|
|
480
|
+
vals.set(k.slice("x-middleware-request-".length), v);
|
|
475
481
|
else
|
|
476
482
|
hdrs.push([k, v]);
|
|
477
483
|
}
|
|
478
484
|
if (next || rewrite) {
|
|
485
|
+
if (keep)
|
|
486
|
+
overrideRequestHeaders(rs, keep, vals);
|
|
479
487
|
writeHeaderPairs(rs.res, hdrs);
|
|
480
488
|
if (rewrite)
|
|
481
489
|
applyRewrite(rs, rewrite);
|
|
@@ -488,6 +496,23 @@ export function createKrateHandler(options) {
|
|
|
488
496
|
status = 500;
|
|
489
497
|
return finish(rs, status, rs.method === "HEAD" || !body ? null : body);
|
|
490
498
|
}
|
|
499
|
+
/** NextResponse.next({ request: { headers } }): the downstream request keeps only the listed headers, with the given values. */
|
|
500
|
+
function overrideRequestHeaders(rs, keep, vals) {
|
|
501
|
+
const edit = (h) => {
|
|
502
|
+
const out = new Headers();
|
|
503
|
+
h.forEach((v, k) => {
|
|
504
|
+
if (keep.has(k.toLowerCase()))
|
|
505
|
+
out.set(k, v);
|
|
506
|
+
});
|
|
507
|
+
for (const [k, v] of vals)
|
|
508
|
+
if (keep.has(k) && k !== "host")
|
|
509
|
+
out.set(k, v);
|
|
510
|
+
return out;
|
|
511
|
+
};
|
|
512
|
+
const shared = rs.headers === rs.request.headers;
|
|
513
|
+
rs.request = new Request(rs.request, { headers: edit(rs.request.headers) });
|
|
514
|
+
rs.headers = shared ? rs.request.headers : edit(rs.headers);
|
|
515
|
+
}
|
|
491
516
|
function middlewareFailed(rs, err) {
|
|
492
517
|
console.error(`[krate] middleware error: ${rs.method} ${rs.path}: ${(err && err.stack) || err}`);
|
|
493
518
|
// Fail closed: continuing would turn a broken auth check into an unauthenticated request.
|
|
@@ -651,8 +676,8 @@ export function createKrateHandler(options) {
|
|
|
651
676
|
return notFoundText(rs, true);
|
|
652
677
|
}
|
|
653
678
|
/** trailingSlash "never": a page URL ending in a slash redirects to the slashless URL. */
|
|
654
|
-
function redirectToSlashless(rs) {
|
|
655
|
-
const p =
|
|
679
|
+
function redirectToSlashless(rs, requested = rs.path) {
|
|
680
|
+
const p = requested;
|
|
656
681
|
if (slashMode !== "never" || p.length < 2 || !p.endsWith("/") || notFoundPageURL(p))
|
|
657
682
|
return null;
|
|
658
683
|
if (rs.method !== "GET" && rs.method !== "HEAD")
|
|
@@ -803,7 +828,8 @@ export function createKrateHandler(options) {
|
|
|
803
828
|
const query = queryOf(rs);
|
|
804
829
|
rs.res.set("Content-Type", "text/html; charset=utf-8");
|
|
805
830
|
rs.res.set("X-Content-Type-Options", "nosniff");
|
|
806
|
-
|
|
831
|
+
// A Cache-Control that middleware or a headers rule already set wins over the default.
|
|
832
|
+
if (!isISR && !rs.res.has("Cache-Control"))
|
|
807
833
|
rs.res.set("Cache-Control", "no-store");
|
|
808
834
|
let reval = 0;
|
|
809
835
|
if (isISR) {
|
|
@@ -882,6 +908,7 @@ export function createKrateHandler(options) {
|
|
|
882
908
|
let errStack = "";
|
|
883
909
|
let errDigest = "";
|
|
884
910
|
let noStore = false;
|
|
911
|
+
let errorCaught = false;
|
|
885
912
|
for (const frame of await Promise.all(pending)) {
|
|
886
913
|
switch (frame.type) {
|
|
887
914
|
case "region": {
|
|
@@ -895,6 +922,8 @@ export function createKrateHandler(options) {
|
|
|
895
922
|
}
|
|
896
923
|
if (frame.noStore)
|
|
897
924
|
noStore = true;
|
|
925
|
+
if (frame.errorCaught)
|
|
926
|
+
errorCaught = true;
|
|
898
927
|
if (id === "page") {
|
|
899
928
|
isISRFrame = !!frame.cacheStatus;
|
|
900
929
|
cacheStatus = frame.cacheStatus || "";
|
|
@@ -973,12 +1002,19 @@ export function createKrateHandler(options) {
|
|
|
973
1002
|
}
|
|
974
1003
|
if (isISR && isISRFrame && cacheStatus)
|
|
975
1004
|
rs.res.set("X-Krate-Cache", cacheStatus.toUpperCase());
|
|
976
|
-
if (
|
|
1005
|
+
if (errorCaught) {
|
|
1006
|
+
// The fallback is an error response: no CDN may keep it, whatever cache
|
|
1007
|
+
// headers the route or middleware set.
|
|
1008
|
+
rs.res.set("X-Krate-Error", "render");
|
|
1009
|
+
rs.res.delete("X-Krate-Cache");
|
|
1010
|
+
rs.res.set("Cache-Control", "no-store");
|
|
1011
|
+
}
|
|
1012
|
+
else if (isISR) {
|
|
977
1013
|
if (noStore || renderErr) {
|
|
978
1014
|
rs.res.set("Cache-Control", "private, no-store");
|
|
979
1015
|
rs.res.delete("X-Krate-Cache");
|
|
980
1016
|
}
|
|
981
|
-
else {
|
|
1017
|
+
else if (!rs.res.has("Cache-Control")) {
|
|
982
1018
|
const r = revalidate > 0 ? revalidate : 60;
|
|
983
1019
|
rs.res.set("Cache-Control", `public, s-maxage=${r}, stale-while-revalidate=${2 * r}`);
|
|
984
1020
|
}
|
|
@@ -1011,7 +1047,7 @@ export function createKrateHandler(options) {
|
|
|
1011
1047
|
}
|
|
1012
1048
|
html = mergeHead(html, title, head);
|
|
1013
1049
|
}
|
|
1014
|
-
return finish(rs, segmentStatus || 200, html);
|
|
1050
|
+
return finish(rs, errorCaught ? 500 : segmentStatus || 200, html);
|
|
1015
1051
|
}
|
|
1016
1052
|
// A <Head> inside a streamed Suspense region renders after the head of the
|
|
1017
1053
|
// response has been sent. It is applied by the region's script instead, which
|
|
@@ -1481,7 +1517,7 @@ export function createKrateHandler(options) {
|
|
|
1481
1517
|
}), basePath);
|
|
1482
1518
|
const scope = { kind: "api", paths: [], tags: [] };
|
|
1483
1519
|
const work = async () => {
|
|
1484
|
-
const res = await revalidateStore.run(scope, async () => toResponse(await fn(request, { params: apiRouteParams(file, rs.path) })));
|
|
1520
|
+
const res = await revalidateStore.run(scope, () => runRoute(request, async () => toResponse(await fn(request, { params: apiRouteParams(file, rs.path) }))));
|
|
1485
1521
|
await flushRevalidate(scope);
|
|
1486
1522
|
return res;
|
|
1487
1523
|
};
|
|
@@ -1618,6 +1654,29 @@ export function createKrateHandler(options) {
|
|
|
1618
1654
|
};
|
|
1619
1655
|
return { actionId: id, url: urlPath, method: "POST", headers, cookies, setCookies };
|
|
1620
1656
|
}
|
|
1657
|
+
// Runs an API route inside the action store so headers(), cookies() and
|
|
1658
|
+
// redirect() work in it like in a server action: cookies it sets are added to
|
|
1659
|
+
// the response and a redirect() signal becomes a 307.
|
|
1660
|
+
async function runRoute(request, run) {
|
|
1661
|
+
const ctx = makeActionContext(request.headers, "", new URL(request.url).pathname);
|
|
1662
|
+
ctx.method = request.method;
|
|
1663
|
+
let out;
|
|
1664
|
+
try {
|
|
1665
|
+
out = await actionStore.run(ctx, run);
|
|
1666
|
+
}
|
|
1667
|
+
catch (e) {
|
|
1668
|
+
if (!e || typeof e.__krateRedirect !== "string")
|
|
1669
|
+
throw e;
|
|
1670
|
+
out = new Response(null, { status: 307, headers: { location: e.__krateRedirect, "cache-control": "no-store" } });
|
|
1671
|
+
}
|
|
1672
|
+
if (ctx.setCookies.length > 0) {
|
|
1673
|
+
const h = new Headers(out.headers);
|
|
1674
|
+
for (const c of ctx.setCookies)
|
|
1675
|
+
h.append("set-cookie", c);
|
|
1676
|
+
out = new Response(out.body, { status: out.status, statusText: out.statusText, headers: h });
|
|
1677
|
+
}
|
|
1678
|
+
return out;
|
|
1679
|
+
}
|
|
1621
1680
|
function safeActionRedirect(value) {
|
|
1622
1681
|
if (typeof value !== "string")
|
|
1623
1682
|
return "";
|
|
@@ -1628,6 +1687,20 @@ export function createKrateHandler(options) {
|
|
|
1628
1687
|
return "";
|
|
1629
1688
|
return to;
|
|
1630
1689
|
}
|
|
1690
|
+
// An absolute http(s) URL from a redirect() signal that marked itself external
|
|
1691
|
+
// (the OAuth redirect of @krate/auth signIn()). A redirect returned in an
|
|
1692
|
+
// action's result is never external.
|
|
1693
|
+
function safeExternalRedirect(value) {
|
|
1694
|
+
if (typeof value !== "string" || /[\u0000-\u001f\u007f\\]/.test(value))
|
|
1695
|
+
return "";
|
|
1696
|
+
try {
|
|
1697
|
+
const u = new URL(value.trim());
|
|
1698
|
+
return u.protocol === "https:" || u.protocol === "http:" ? u.href : "";
|
|
1699
|
+
}
|
|
1700
|
+
catch {
|
|
1701
|
+
return "";
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1631
1704
|
function sanitizeActionReturn(value, rs) {
|
|
1632
1705
|
value = value.trim();
|
|
1633
1706
|
if (!value)
|
|
@@ -1820,14 +1893,17 @@ export function createKrateHandler(options) {
|
|
|
1820
1893
|
const scope = { kind: "action", paths: [], tags: [] };
|
|
1821
1894
|
let result;
|
|
1822
1895
|
let redirect = "";
|
|
1896
|
+
let external = false;
|
|
1823
1897
|
let failure = null;
|
|
1824
1898
|
let fallback = 0;
|
|
1825
1899
|
try {
|
|
1826
1900
|
result = await revalidateStore.run(scope, () => actionStore.run(ctx, () => fn(...args)));
|
|
1827
1901
|
}
|
|
1828
1902
|
catch (e) {
|
|
1829
|
-
if (e && typeof e.__krateRedirect === "string")
|
|
1903
|
+
if (e && typeof e.__krateRedirect === "string") {
|
|
1830
1904
|
redirect = e.__krateRedirect;
|
|
1905
|
+
external = e.__krateRedirectExternal === true;
|
|
1906
|
+
}
|
|
1831
1907
|
else if ((fallback = httpFallbackStatus(e)))
|
|
1832
1908
|
failure = null;
|
|
1833
1909
|
else
|
|
@@ -1843,7 +1919,7 @@ export function createKrateHandler(options) {
|
|
|
1843
1919
|
}
|
|
1844
1920
|
if (!redirect && result && typeof result === "object" && typeof result.redirect === "string")
|
|
1845
1921
|
redirect = result.redirect;
|
|
1846
|
-
redirect = safeActionRedirect(redirect);
|
|
1922
|
+
redirect = external ? safeExternalRedirect(redirect) : safeActionRedirect(redirect);
|
|
1847
1923
|
if (fallback && !failure)
|
|
1848
1924
|
return withHeaders(rs, actionFallback(rs, fallback, fetchMode, ctx.setCookies, reqHeaders.get("x-krate-return") || ""));
|
|
1849
1925
|
let body = "";
|
|
@@ -1851,6 +1927,8 @@ export function createKrateHandler(options) {
|
|
|
1851
1927
|
const envelope = { ok: true, result: result === undefined ? null : result };
|
|
1852
1928
|
if (redirect)
|
|
1853
1929
|
envelope.redirect = redirect;
|
|
1930
|
+
if (redirect && external)
|
|
1931
|
+
envelope.external = true;
|
|
1854
1932
|
if (revalidation) {
|
|
1855
1933
|
envelope.revalidated = true;
|
|
1856
1934
|
envelope.revalidation = revalidation;
|
|
@@ -1953,11 +2031,12 @@ export function createKrateHandler(options) {
|
|
|
1953
2031
|
}
|
|
1954
2032
|
async function pageChain(rs) {
|
|
1955
2033
|
return flashNotice(rs, async (rs2) => {
|
|
2034
|
+
const requested = rs2.path;
|
|
1956
2035
|
const blocked = await runMiddleware(rs2);
|
|
1957
2036
|
if (blocked)
|
|
1958
2037
|
return blocked;
|
|
1959
2038
|
// Checked on the requested URL, before a rewrite changes the internal path.
|
|
1960
|
-
const slashless = redirectToSlashless(rs2);
|
|
2039
|
+
const slashless = redirectToSlashless(rs2, requested);
|
|
1961
2040
|
if (slashless)
|
|
1962
2041
|
return slashless;
|
|
1963
2042
|
return redirectRewriteHandler(rs2);
|
package/dist/server.d.ts
CHANGED
|
@@ -30,15 +30,45 @@ interface RawHTML {
|
|
|
30
30
|
export declare function goComponent(name: string, tier: string, baked: Record<string, string> | null | undefined, props: Record<string, any> | undefined, revalidate?: number): JSXNode;
|
|
31
31
|
type JSXNode = string | number | boolean | null | undefined | JSXNode[] | JSXElement | RawHTML;
|
|
32
32
|
interface JSXElement {
|
|
33
|
-
type: string | Function;
|
|
33
|
+
type: string | Function | object | symbol;
|
|
34
34
|
props: Record<string, any>;
|
|
35
35
|
children: JSXNode[];
|
|
36
36
|
}
|
|
37
37
|
export declare function Fragment(_props: {
|
|
38
38
|
children?: JSXNode;
|
|
39
39
|
}): JSXNode;
|
|
40
|
-
export declare function jsx(type: string | Function, props: Record<string, any>, _key?: string): JSXElement;
|
|
40
|
+
export declare function jsx(type: string | Function | object | symbol, props: Record<string, any>, _key?: string): JSXElement;
|
|
41
41
|
export declare const jsxs: typeof jsx;
|
|
42
|
+
/**
|
|
43
|
+
* ServerErrorBoundary is the request-time form of a segment's error.tsx under
|
|
44
|
+
* an async layout: it renders `children`, and when rendering them throws it
|
|
45
|
+
* renders `<fallback error={{ name, message, digest }} />` in their place.
|
|
46
|
+
* notFound(), forbidden(), unauthorized() and redirect() signals pass through.
|
|
47
|
+
* The visitor sees a generic message and a digest; the real error is logged
|
|
48
|
+
* with the same digest. The render's scope records the catch, so the host
|
|
49
|
+
* answers 500 with no-store instead of a cacheable 200.
|
|
50
|
+
*/
|
|
51
|
+
export declare class ServerErrorBoundary {
|
|
52
|
+
props: {
|
|
53
|
+
fallback: any;
|
|
54
|
+
children?: JSXNode;
|
|
55
|
+
};
|
|
56
|
+
state: {
|
|
57
|
+
error: unknown;
|
|
58
|
+
};
|
|
59
|
+
constructor(props: {
|
|
60
|
+
fallback: any;
|
|
61
|
+
children?: JSXNode;
|
|
62
|
+
});
|
|
63
|
+
static getDerivedStateFromError(error: unknown): {
|
|
64
|
+
error: {
|
|
65
|
+
name: string;
|
|
66
|
+
message: string;
|
|
67
|
+
digest: string;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
render(): JSXNode;
|
|
71
|
+
}
|
|
42
72
|
export declare function renderToString(node: JSXNode): string;
|
|
43
73
|
/**
|
|
44
74
|
* renderToStringAsync renders a tree that may contain async (server)
|
|
@@ -57,18 +87,35 @@ interface SuspenseProps {
|
|
|
57
87
|
children: JSXNode;
|
|
58
88
|
}
|
|
59
89
|
export declare function Suspense(props: SuspenseProps): JSXNode;
|
|
60
|
-
|
|
90
|
+
/**
|
|
91
|
+
* lazy() in a server tree: the loader's module is imported while the page
|
|
92
|
+
* renders and its component renders in place. Nothing is code-split to the
|
|
93
|
+
* browser, so a server-only module can be loaded this way.
|
|
94
|
+
*/
|
|
95
|
+
export declare function lazy<P = Record<string, any>>(loader: () => Promise<{
|
|
96
|
+
default: (props: P) => unknown;
|
|
97
|
+
} | ((props: P) => unknown)>): (props: P & {
|
|
98
|
+
children?: JSXNode;
|
|
99
|
+
}) => Promise<JSXNode>;
|
|
100
|
+
type ClientRender = (component: unknown, props: Record<string, unknown>, childrenHTML: string | null, fwd?: SlotValues, capture?: Record<string, SlotValues>) => string;
|
|
101
|
+
type SlotValues = {
|
|
102
|
+
k: string;
|
|
103
|
+
v: unknown;
|
|
104
|
+
}[];
|
|
61
105
|
/**
|
|
62
106
|
* clientBoundary wraps an export of a `"use client"` module, or a component
|
|
63
107
|
* of a React package, for the server bundle of a page that renders server
|
|
64
108
|
* components. The server renders the component's initial HTML (render) and,
|
|
65
|
-
* when it hydrates, puts it in a <krate-island> marker
|
|
66
|
-
* serialized props; the per-module client chunk at src
|
|
67
|
-
* browser. A component without state or effects (hydrate
|
|
68
|
-
* is plain HTML. Children are rendered on the server
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
109
|
+
* when it hydrates, puts it in a <krate-island> marker preceded by a JSON
|
|
110
|
+
* script with its serialized props; the per-module client chunk at src
|
|
111
|
+
* hydrates it in the browser. A component without state or effects (hydrate
|
|
112
|
+
* false, an icon) is plain HTML. Children are rendered on the server into the
|
|
113
|
+
* component's HTML and adopted there by the browser (see DeferredSlot), or,
|
|
114
|
+
* when they hold client components of their own, sent as a tree that keeps
|
|
115
|
+
* those components in the same Preact tree. The children of an asChild component
|
|
116
|
+
* always travel as a tree, with a server component at the top of a child run
|
|
117
|
+
* to the element it renders, so the component's Slot can clone it. A
|
|
118
|
+
* namespace export (Radix's Dialog) is wrapped member by member. With `flight` the children always travel as a
|
|
72
119
|
* tree (a real-React island rebuilds them as elements, not HTML).
|
|
73
120
|
*/
|
|
74
121
|
export declare function clientBoundary(moduleId: string, exportName: string, src: string, impl: unknown, render: ClientRender, hydrate?: boolean, flight?: boolean): unknown;
|