akanjs 3.2.0-rc.2 → 3.2.0-rc.4
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/ARCHITECTURE.md +3 -3
- package/CHANGELOG.md +105 -1
- package/client/csrTypes.ts +13 -1
- package/client/frameConfig.ts +4 -2
- package/client/route/RouteHead.ts +11 -2
- package/client/router.ts +8 -4
- package/client/rscNavigation.ts +2 -3
- package/client/rscRoutePatch.ts +42 -0
- package/common/akanGraph.ts +1 -1
- package/common/types.ts +5 -0
- package/dictionary/base.dictionary.ts +1 -0
- package/fetch/client/fetchClient.ts +11 -16
- package/fetch/client/fetchHandle.ts +11 -1
- package/fetch/client/inflightQueries.ts +17 -0
- package/fetch/index.ts +1 -0
- package/fetch/staleReads.ts +27 -0
- package/fetch/streamedRead.ts +23 -0
- package/package.json +1 -1
- package/server/akanServer.ts +1 -1
- package/server/artifact/builderRpc.ts +22 -7
- package/server/artifact/ipcTypes.ts +9 -3
- package/server/devtools/README.md +4 -3
- package/server/devtools/devtoolsRouter.ts +9 -5
- package/server/devtools/studio/studioRoute.ts +18 -11
- package/server/head.tsx +4 -19
- package/server/routeElementComposer.tsx +86 -3
- package/server/routeState.ts +5 -1
- package/server/routeTreeBuilder.ts +1 -0
- package/server/rscClient.tsx +118 -21
- package/server/rscClientChunks.ts +62 -0
- package/server/rscClientFetch.ts +6 -1
- package/server/rscHeadPatch.ts +17 -56
- package/server/rscParkedRedirect.ts +23 -0
- package/server/rscPatchPlanner.ts +220 -0
- package/server/rscPrefetchCache.ts +39 -17
- package/server/rscResponse.ts +78 -0
- package/server/rscSegmentOutlet.tsx +24 -5
- package/server/rscSegmentOutletReference.ts +10 -12
- package/server/rscWorker.tsx +109 -154
- package/server/rscWorkerCache.ts +56 -0
- package/server/rscWorkerHost.ts +6 -8
- package/server/ssrTypes.ts +9 -0
- package/server/webRouter.ts +12 -89
- package/store/action.ts +282 -299
- package/store/actionErrorKey.ts +9 -0
- package/store/draftFlush.ts +5 -0
- package/store/draftStore.ts +2 -0
- package/store/formDrafts.ts +115 -0
- package/store/modelListPatch.ts +53 -0
- package/store/modelWriteGuard.ts +91 -0
- package/store/rootStore.ts +2 -0
- package/store/storeInstance.ts +9 -10
- package/store/uploadPoll.ts +52 -0
- package/store/withSelector.ts +6 -4
- package/types/client/csrTypes.d.ts +13 -1
- package/types/client/route/RouteHead.d.ts +5 -1
- package/types/client/router.d.ts +3 -2
- package/types/client/rscNavigation.d.ts +2 -2
- package/types/client/rscRoutePatch.d.ts +22 -0
- package/types/common/akanGraph.d.ts +1 -1
- package/types/common/types.d.ts +5 -0
- package/types/dictionary/base.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +8 -8
- package/types/fetch/client/fetchHandle.d.ts +1 -0
- package/types/fetch/client/inflightQueries.d.ts +2 -0
- package/types/fetch/index.d.ts +1 -0
- package/types/fetch/staleReads.d.ts +8 -0
- package/types/fetch/streamedRead.d.ts +8 -0
- package/types/server/artifact/builderRpc.d.ts +8 -0
- package/types/server/artifact/ipcTypes.d.ts +8 -0
- package/types/server/devtools/devtoolsRouter.d.ts +4 -1
- package/types/server/devtools/studio/studioRoute.d.ts +2 -1
- package/types/server/head.d.ts +2 -2
- package/types/server/logging/rotatingLogWriter.d.ts +1 -1
- package/types/server/routeElementComposer.d.ts +24 -1
- package/types/server/routeState.d.ts +2 -0
- package/types/server/rscClient.d.ts +1 -0
- package/types/server/rscClientChunks.d.ts +10 -0
- package/types/server/rscHeadPatch.d.ts +5 -4
- package/types/server/rscParkedRedirect.d.ts +12 -0
- package/types/server/rscPatchPlanner.d.ts +49 -0
- package/types/server/rscPrefetchCache.d.ts +2 -2
- package/types/server/rscResponse.d.ts +9 -0
- package/types/server/rscSegmentOutlet.d.ts +5 -0
- package/types/server/rscSegmentOutletReference.d.ts +2 -1
- package/types/server/rscWorkerCache.d.ts +33 -1
- package/types/server/rscWorkerHost.d.ts +2 -6
- package/types/server/ssrTypes.d.ts +8 -0
- package/types/server/webRouter.d.ts +1 -7
- package/types/store/action.d.ts +10 -6
- package/types/store/actionErrorKey.d.ts +1 -0
- package/types/store/baseSt.d.ts +1 -295
- package/types/store/draftFlush.d.ts +4 -0
- package/types/store/formDrafts.d.ts +21 -0
- package/types/store/modelListPatch.d.ts +34 -0
- package/types/store/modelWriteGuard.d.ts +45 -0
- package/types/store/rootStore.d.ts +6 -0
- package/types/store/uploadPoll.d.ts +16 -0
- package/types/store/withSelector.d.ts +1 -1
- package/types/ui/Link/inputModality.d.ts +5 -0
- package/types/ui/Link/viewportPrefetch.d.ts +4 -0
- package/types/ui/Load/Edit_Client.d.ts +2 -0
- package/types/ui/Load/Pagination.d.ts +4 -1
- package/types/ui/Load/Stream.d.ts +7 -1
- package/types/ui/Load/StreamBoundary.d.ts +20 -0
- package/types/ui/Load/Units.d.ts +2 -0
- package/types/ui/Load/View.d.ts +2 -0
- package/types/webkit/nativeCore.d.ts +1 -0
- package/types/webkit/nativePurchase.d.ts +2 -1
- package/types/webkit/useFrameRuntime.d.ts +1 -0
- package/types/webkit/useGeoLocation.d.ts +1 -1
- package/types/webkit/usePurchase.d.ts +1 -1
- package/types/webkit/useTransitionStore.d.ts +1 -0
- package/ui/Button.tsx +1 -0
- package/ui/Link/inputModality.ts +21 -0
- package/ui/Link/prefetchIntent.ts +7 -25
- package/ui/Link/viewportPrefetch.ts +30 -0
- package/ui/Load/Edit_Client.tsx +7 -1
- package/ui/Load/Pagination.tsx +10 -2
- package/ui/Load/Stream.tsx +24 -4
- package/ui/Load/StreamBoundary.tsx +70 -0
- package/ui/Load/Units.tsx +3 -0
- package/ui/Load/View.tsx +3 -0
- package/ui/System/Client.tsx +28 -7
- package/webkit/nativeCore.ts +1 -0
- package/webkit/nativePurchase.ts +6 -3
- package/webkit/useCamera.tsx +22 -21
- package/webkit/useGeoLocation.tsx +4 -1
- package/webkit/usePurchase.tsx +4 -2
- package/webkit/useTransitionStore.ts +13 -0
package/package.json
CHANGED
package/server/akanServer.ts
CHANGED
|
@@ -787,7 +787,7 @@ export class AkanServer {
|
|
|
787
787
|
websocketPrefix: this.websocketPrefix,
|
|
788
788
|
openapi: this.openapi,
|
|
789
789
|
getStatus: () => this.status,
|
|
790
|
-
...
|
|
790
|
+
...DevtoolsRouter.buildsFrom(webRouter),
|
|
791
791
|
}).createRoutes();
|
|
792
792
|
return { ...openapiRoutes, ...mcpRoutes, ...devtoolsRoutes, ...soloRoutes };
|
|
793
793
|
}
|
|
@@ -27,6 +27,14 @@ export interface BuilderRpcEventHandlers {
|
|
|
27
27
|
onBuildStatus?: (status: DevBuildStatus) => void;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
export interface BuilderRpcTimeout extends Error {
|
|
31
|
+
timeoutMs: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface BuilderRpcUnavailable extends Error {
|
|
35
|
+
kind: "unavailable";
|
|
36
|
+
}
|
|
37
|
+
|
|
30
38
|
/** One per backend process: subscribes to the IPC channel on construction and unsubscribes on `dispose()`. */
|
|
31
39
|
export class BuilderRpc {
|
|
32
40
|
readonly #logger = new Logger("BuilderRpc");
|
|
@@ -46,8 +54,10 @@ export class BuilderRpc {
|
|
|
46
54
|
const waiter = this.#pending.get(res.id);
|
|
47
55
|
if (!waiter) return;
|
|
48
56
|
this.#pending.delete(res.id);
|
|
49
|
-
if (!res.ok)
|
|
50
|
-
|
|
57
|
+
if (!res.ok) {
|
|
58
|
+
const error = new Error(`[builder] ${res.type} failed: ${res.error}`);
|
|
59
|
+
waiter.reject(res.kind === "unavailable" ? Object.assign(error, { kind: res.kind }) : error);
|
|
60
|
+
} else waiter.resolve(res.type === "build-csr-res" ? undefined : res.data);
|
|
51
61
|
return;
|
|
52
62
|
}
|
|
53
63
|
const ev = msg as BuilderEvent;
|
|
@@ -131,6 +141,14 @@ export class BuilderRpc {
|
|
|
131
141
|
);
|
|
132
142
|
}
|
|
133
143
|
|
|
144
|
+
static isTimeout(error: unknown): error is BuilderRpcTimeout {
|
|
145
|
+
return error instanceof Error && "timeoutMs" in error;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static isUnavailable(error: unknown): error is BuilderRpcUnavailable {
|
|
149
|
+
return error instanceof Error && "kind" in error && error.kind === "unavailable";
|
|
150
|
+
}
|
|
151
|
+
|
|
134
152
|
static #timeoutMs(): number {
|
|
135
153
|
const configured = Number(process.env.AKAN_BUILDER_RPC_TIMEOUT_MS);
|
|
136
154
|
if (Number.isFinite(configured) && configured > 0) return configured;
|
|
@@ -144,11 +162,8 @@ export class BuilderRpc {
|
|
|
144
162
|
return await new Promise<T>((resolve, reject) => {
|
|
145
163
|
const timer = setTimeout(() => {
|
|
146
164
|
this.#pending.delete(id);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
`[builder] ${label} got no answer in ${timeoutMs}ms; the builder was likely recycled or restarted mid-request — reload to retry`,
|
|
150
|
-
),
|
|
151
|
-
);
|
|
165
|
+
const message = `[builder] ${label} got no answer in ${timeoutMs}ms; the builder was likely recycled or restarted mid-request — reload to retry`;
|
|
166
|
+
reject(Object.assign(new Error(message), { timeoutMs }));
|
|
152
167
|
}, timeoutMs);
|
|
153
168
|
|
|
154
169
|
timer.unref?.();
|
|
@@ -67,9 +67,15 @@ export type BuilderReq = {
|
|
|
67
67
|
generation?: number;
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Only the dev host writes it, on a reply no builder answered (stopped, not started, gone mid-request); a builder's own
|
|
72
|
+
* failure has none. Optional, so a dev host and a server released before it still talk to ones released after.
|
|
73
|
+
*/
|
|
74
|
+
export type BuilderFailureKind = "unavailable";
|
|
75
|
+
|
|
70
76
|
export type BuilderRes =
|
|
71
77
|
| { type: "build-route-res"; id: number; ok: true; data: BuildRouteResultPayload }
|
|
72
|
-
| { type: "build-route-res"; id: number; ok: false; error: string };
|
|
78
|
+
| { type: "build-route-res"; id: number; ok: false; error: string; kind?: BuilderFailureKind };
|
|
73
79
|
|
|
74
80
|
/**
|
|
75
81
|
* Sent by the first `/__csr` or `?csr=true` request (a device WebView in mobile dev) to arm the lazy dev CSR build.
|
|
@@ -79,7 +85,7 @@ export type BuilderCsrReq = { type: "build-csr"; id: number; reason: string; she
|
|
|
79
85
|
|
|
80
86
|
export type BuilderCsrRes =
|
|
81
87
|
| { type: "build-csr-res"; id: number; ok: true }
|
|
82
|
-
| { type: "build-csr-res"; id: number; ok: false; error: string };
|
|
88
|
+
| { type: "build-csr-res"; id: number; ok: false; error: string; kind?: BuilderFailureKind };
|
|
83
89
|
|
|
84
90
|
export interface BuildGraphPayload {
|
|
85
91
|
workspaceRoot: string;
|
|
@@ -112,7 +118,7 @@ export type BuilderGraphRes =
|
|
|
112
118
|
ok: true;
|
|
113
119
|
data: BuildGraphPayload | BuildChangesPayload | BuildFrameworkPayload;
|
|
114
120
|
}
|
|
115
|
-
| { type: "build-graph-res"; id: number; ok: false; error: string };
|
|
121
|
+
| { type: "build-graph-res"; id: number; ok: false; error: string; kind?: BuilderFailureKind };
|
|
116
122
|
|
|
117
123
|
/** Drain and exit (the only way bundler memory returns to the OS); draining, not killing, keeps a rebuild whole. */
|
|
118
124
|
export type BuilderControl = { type: "builder-shutdown"; reason: string };
|
|
@@ -676,9 +676,10 @@ for it to be added.
|
|
|
676
676
|
`/_akan/studio?view=runtime` draws `/_akan/deps` and `/_akan/signal` as one picture, next to Studio's
|
|
677
677
|
source-built views (ERD, Signals, Changes, Framework). It calls the same serializers in process, so it reads the
|
|
678
678
|
container of the process that answered and needs that server running; the header says which (`pid`,
|
|
679
|
-
`serverMode`, `replicaIdx`). It needs no dev builder, so `/_akan/studio`
|
|
680
|
-
builder answers
|
|
681
|
-
`
|
|
679
|
+
`serverMode`, `replicaIdx`). It needs no dev builder, so `/_akan/studio` without a known `view` lands on it when
|
|
680
|
+
no builder answers: the server has none, the dev host reports it stopped or gone, or the RPC times out
|
|
681
|
+
(`AKAN_BUILDER_RPC_TIMEOUT_MS`, 120 s by default). A source-built view asked for then, like a failed build,
|
|
682
|
+
answers 503 inside Studio's nav with the reason.
|
|
682
683
|
|
|
683
684
|
- **Columns.** Jobs, then services by boot stage, then adaptors by boot stage. An injection always targets
|
|
684
685
|
an earlier stage, so the latest stage stands leftmost and every arrow points from what injects to what it
|
|
@@ -19,6 +19,7 @@ export interface DevtoolsRouterContext {
|
|
|
19
19
|
prefix: string;
|
|
20
20
|
websocketPrefix: string;
|
|
21
21
|
openapi: boolean;
|
|
22
|
+
web?: boolean;
|
|
22
23
|
getStatus: () => string;
|
|
23
24
|
buildGraph?: () => Promise<BuildGraphPayload | null>;
|
|
24
25
|
buildChanges?: (since: string | null) => Promise<BuildChangesPayload | null>;
|
|
@@ -39,12 +40,14 @@ export class DevtoolsRouter {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
static buildsFrom(
|
|
42
|
-
|
|
43
|
-
): Pick<DevtoolsRouterContext, "buildGraph" | "buildChanges" | "buildFramework"> {
|
|
43
|
+
webRouter: { readonly builderRpc: BuilderRpc | null } | null,
|
|
44
|
+
): Pick<DevtoolsRouterContext, "web" | "buildGraph" | "buildChanges" | "buildFramework"> {
|
|
45
|
+
if (!webRouter) return { web: false };
|
|
44
46
|
return {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
web: true,
|
|
48
|
+
buildGraph: async () => (await webRouter.builderRpc?.buildGraph()) ?? null,
|
|
49
|
+
buildChanges: async (since) => (await webRouter.builderRpc?.buildChanges(since)) ?? null,
|
|
50
|
+
buildFramework: async () => (await webRouter.builderRpc?.buildFramework()) ?? null,
|
|
48
51
|
};
|
|
49
52
|
}
|
|
50
53
|
|
|
@@ -59,6 +62,7 @@ export class DevtoolsRouter {
|
|
|
59
62
|
const studio = new StudioRoute({
|
|
60
63
|
basePath: `${DevtoolsRouter.basePath}/studio`,
|
|
61
64
|
appName: this.#context.env.appName,
|
|
65
|
+
web: this.#context.web ?? true,
|
|
62
66
|
buildGraph: this.#context.buildGraph ?? null,
|
|
63
67
|
buildChanges: this.#context.buildChanges ?? null,
|
|
64
68
|
buildFramework: this.#context.buildFramework ?? null,
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Logger } from "akanjs/common";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
|
-
import
|
|
3
|
+
import {
|
|
4
|
+
type BuildChangesPayload,
|
|
5
|
+
BuilderRpc,
|
|
6
|
+
type BuildFrameworkPayload,
|
|
7
|
+
type BuildGraphPayload,
|
|
8
|
+
} from "../../artifact";
|
|
4
9
|
import { createSystemPageHeaders } from "../../systemPages";
|
|
5
10
|
import { StudioDocument, type StudioEditor, type StudioPayload, type StudioQuery } from "./studioDocument";
|
|
6
11
|
import type { StudioView } from "./studioLayout";
|
|
@@ -9,6 +14,7 @@ import type { StudioRuntimePayload } from "./studioRuntimeGraph";
|
|
|
9
14
|
export interface StudioRouteContext {
|
|
10
15
|
basePath: string;
|
|
11
16
|
appName: string;
|
|
17
|
+
web: boolean;
|
|
12
18
|
buildGraph: (() => Promise<BuildGraphPayload | null>) | null;
|
|
13
19
|
buildChanges: ((since: string | null) => Promise<BuildChangesPayload | null>) | null;
|
|
14
20
|
buildFramework: (() => Promise<BuildFrameworkPayload | null>) | null;
|
|
@@ -42,7 +48,7 @@ export class StudioRoute {
|
|
|
42
48
|
const url = new URL(req.url);
|
|
43
49
|
const asked = StudioRoute.queryOf(url);
|
|
44
50
|
const loaded = await this.#load(asked);
|
|
45
|
-
const landed = "error" in loaded && loaded.
|
|
51
|
+
const landed = "error" in loaded && loaded.unanswered && !views.has(url.searchParams.get("view") as StudioView);
|
|
46
52
|
const query: StudioQuery = landed ? { ...asked, view: "runtime" } : asked;
|
|
47
53
|
const shown = landed ? await this.#load(query) : loaded;
|
|
48
54
|
if ("error" in shown)
|
|
@@ -67,24 +73,25 @@ export class StudioRoute {
|
|
|
67
73
|
return buildGraph ? await buildGraph() : null;
|
|
68
74
|
}
|
|
69
75
|
|
|
70
|
-
async #load(query: StudioQuery): Promise<{ payload: StudioPayload } | { error: string;
|
|
76
|
+
async #load(query: StudioQuery): Promise<{ payload: StudioPayload } | { error: string; unanswered: boolean }> {
|
|
71
77
|
if (query.view === "runtime") return { payload: this.#context.runtime() };
|
|
72
78
|
try {
|
|
73
79
|
const payload = await this.#payloadOf(query);
|
|
74
|
-
return payload
|
|
75
|
-
? { payload }
|
|
76
|
-
: {
|
|
77
|
-
error:
|
|
78
|
-
"Akan Studio reads this view from the dev builder; open it under `akan start`. Runtime (live) reads this running server and needs no builder.",
|
|
79
|
-
builderless: true,
|
|
80
|
-
};
|
|
80
|
+
return payload ? { payload } : { error: this.#builderless(), unanswered: true };
|
|
81
81
|
} catch (error) {
|
|
82
82
|
const message = error instanceof Error ? error.message : String(error);
|
|
83
83
|
StudioRoute.logger.warn(`Failed to build the Akan Graph: ${message}`);
|
|
84
|
-
return { error: message,
|
|
84
|
+
return { error: message, unanswered: BuilderRpc.isTimeout(error) || BuilderRpc.isUnavailable(error) };
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
#builderless(): string {
|
|
89
|
+
const reach = this.#context.web
|
|
90
|
+
? "Akan Studio reads this view from the dev builder; open it under `akan start`."
|
|
91
|
+
: "Akan Studio reads this view from the dev builder, which a server reaches only with its web surface on, and this one serves the API only (`AKAN_SSR=false` or `setWeb(false)`).";
|
|
92
|
+
return `${reach} Runtime (live) reads this running server and needs no builder.`;
|
|
93
|
+
}
|
|
94
|
+
|
|
88
95
|
static #headers(): Headers {
|
|
89
96
|
const headers = createSystemPageHeaders();
|
|
90
97
|
headers.delete("X-Frame-Options");
|
package/server/head.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { ResolvedHead, ResolveHeadResult } from "akanjs/client";
|
|
2
3
|
import { isRecord } from "akanjs/common";
|
|
3
4
|
import { AKAN_RSC_HEAD_SNAPSHOT_VERSION, type AkanHeadSnapshotV1 } from "./routeState";
|
|
4
5
|
|
|
@@ -19,24 +20,8 @@ export function mergeAkanHeadSnapshots(
|
|
|
19
20
|
return snapshots.some(Boolean) ? { version: AKAN_RSC_HEAD_SNAPSHOT_VERSION, nodes } : undefined;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export function
|
|
23
|
-
return (
|
|
24
|
-
<>
|
|
25
|
-
{snapshot.nodes.map((node, index) => {
|
|
26
|
-
const key = `${node.tag}:${index}`;
|
|
27
|
-
const marker = { "data-akan-head": "route", "data-akan-head-key": key };
|
|
28
|
-
if (node.tag === "title") {
|
|
29
|
-
return (
|
|
30
|
-
<title key={key} {...marker}>
|
|
31
|
-
{node.text ?? ""}
|
|
32
|
-
</title>
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
if (node.tag === "meta") return <meta key={key} {...node.attrs} {...marker} />;
|
|
36
|
-
return <link key={key} {...node.attrs} {...marker} />;
|
|
37
|
-
})}
|
|
38
|
-
</>
|
|
39
|
-
);
|
|
23
|
+
export function akanHeadSnapshotKey(snapshot: AkanHeadSnapshotV1): string {
|
|
24
|
+
return createHash("sha256").update(JSON.stringify(snapshot.nodes)).digest("base64url").slice(0, 16);
|
|
40
25
|
}
|
|
41
26
|
|
|
42
27
|
function isResolvedHead(value: unknown): value is ResolvedHead {
|
|
@@ -6,10 +6,12 @@ import type {
|
|
|
6
6
|
PageConfig,
|
|
7
7
|
PathRoute,
|
|
8
8
|
ResolvedHead,
|
|
9
|
+
ResolveHeadResult,
|
|
9
10
|
RouteRender,
|
|
10
11
|
} from "akanjs/client";
|
|
12
|
+
import { StreamedRead } from "akanjs/fetch";
|
|
11
13
|
import { Children, cloneElement, isValidElement, type ReactElement, type ReactNode, Suspense } from "react";
|
|
12
|
-
import { getExplicitPageConfigKeys, resolvePageState } from "../client/frameConfig";
|
|
14
|
+
import { getExplicitPageConfigKeys, mergePageConfigs, resolvePageState } from "../client/frameConfig";
|
|
13
15
|
import { RouteGuards } from "../client/route/RouteGuards";
|
|
14
16
|
import { RouteHead } from "../client/route/RouteHead";
|
|
15
17
|
import type { FallbackHead } from "./fallbackHead";
|
|
@@ -33,7 +35,19 @@ export interface RouteNotFoundInPlace {
|
|
|
33
35
|
head?: FallbackHead;
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
export interface AdmittedNotFoundFallback {
|
|
39
|
+
route: LayoutFallbackRoute;
|
|
40
|
+
params: Record<string, string>;
|
|
41
|
+
searchParams: Record<string, string | string[]>;
|
|
42
|
+
}
|
|
43
|
+
|
|
36
44
|
export class RouteElementComposer {
|
|
45
|
+
static readonly #levelErrors = new WeakSet<object>();
|
|
46
|
+
|
|
47
|
+
static streamed(error: unknown): boolean {
|
|
48
|
+
return StreamedRead.is(error) && !RouteElementComposer.#levelErrors.has(error as object);
|
|
49
|
+
}
|
|
50
|
+
|
|
37
51
|
static async resolveSsrFramePathRoute({
|
|
38
52
|
pathRoute,
|
|
39
53
|
basePath,
|
|
@@ -116,6 +130,32 @@ export class RouteElementComposer {
|
|
|
116
130
|
await Promise.all(renders.map((routeRender) => routeRender?.checkArgs?.({ params, searchParams })));
|
|
117
131
|
}
|
|
118
132
|
|
|
133
|
+
static async prefetchVerdict(
|
|
134
|
+
match: { pathRoute: PathRoute; params: Record<string, string> } | null,
|
|
135
|
+
notFoundFallback: AdmittedNotFoundFallback | null,
|
|
136
|
+
searchParams: Record<string, string | string[]>,
|
|
137
|
+
): Promise<"refused" | "admitted" | "unchecked"> {
|
|
138
|
+
if (!match) {
|
|
139
|
+
const refused = notFoundFallback && (await RouteElementComposer.#layoutsRefusePrefetch(notFoundFallback.route));
|
|
140
|
+
return refused ? "refused" : "unchecked";
|
|
141
|
+
}
|
|
142
|
+
const configChain = await RouteElementComposer.#resolvePageConfigChain(match.pathRoute);
|
|
143
|
+
if (!configChain.some((config) => config.prefetch === false)) return "unchecked";
|
|
144
|
+
try {
|
|
145
|
+
await RouteElementComposer.admit({ ...match, searchParams });
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (RouteElementComposer.#isRedirect(error)) throw error;
|
|
148
|
+
const fallbackRoute = RouteElementComposer.fallbackRouteOf(match.pathRoute, error);
|
|
149
|
+
if (await RouteElementComposer.#layoutsRefusePrefetch(fallbackRoute)) return "refused";
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
return mergePageConfigs(configChain).prefetch === false ? "refused" : "admitted";
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static async #layoutsRefusePrefetch(route: PathRoute | LayoutFallbackRoute): Promise<boolean> {
|
|
156
|
+
return mergePageConfigs(await RouteElementComposer.#resolveLayoutPageConfigChain(route)).prefetch === false;
|
|
157
|
+
}
|
|
158
|
+
|
|
119
159
|
static fallbackRouteOf<Route extends LayoutFallbackRoute>(route: Route, error: unknown): Route | LayoutFallbackRoute {
|
|
120
160
|
const refusedAt = RouteGuards.refusedAt(error);
|
|
121
161
|
if (refusedAt === undefined) return route;
|
|
@@ -135,11 +175,24 @@ export class RouteElementComposer {
|
|
|
135
175
|
await RouteGuards.run([...route.renderRootLayouts, ...route.renderLayouts], props);
|
|
136
176
|
return route;
|
|
137
177
|
} catch (error) {
|
|
138
|
-
if ((error
|
|
178
|
+
if (RouteElementComposer.#isRedirect(error)) throw error;
|
|
139
179
|
return RouteElementComposer.fallbackRouteOf(route, error);
|
|
140
180
|
}
|
|
141
181
|
}
|
|
142
182
|
|
|
183
|
+
static async admitNotFoundFallback(
|
|
184
|
+
matched: { fallbackRoute: LayoutFallbackRoute; params: Record<string, string> } | null,
|
|
185
|
+
searchParams: Record<string, string | string[]>,
|
|
186
|
+
): Promise<AdmittedNotFoundFallback | null> {
|
|
187
|
+
if (!matched) return null;
|
|
188
|
+
const props = { params: matched.params, searchParams };
|
|
189
|
+
return { ...props, route: await RouteElementComposer.admittedFallbackOf(matched.fallbackRoute, props) };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static #isRedirect(error: unknown): boolean {
|
|
193
|
+
return (error as { digest?: unknown } | null)?.digest === "AKAN_REDIRECT";
|
|
194
|
+
}
|
|
195
|
+
|
|
143
196
|
static composeSuffix({
|
|
144
197
|
pathRoute,
|
|
145
198
|
params,
|
|
@@ -205,6 +258,26 @@ export class RouteElementComposer {
|
|
|
205
258
|
return resolveHeadResult(await pathRoute.resolveHead?.({ params, searchParams }));
|
|
206
259
|
}
|
|
207
260
|
|
|
261
|
+
static async resolveHeadLayers({
|
|
262
|
+
pathRoute,
|
|
263
|
+
params,
|
|
264
|
+
searchParams,
|
|
265
|
+
}: {
|
|
266
|
+
pathRoute: PathRoute;
|
|
267
|
+
params: Record<string, string>;
|
|
268
|
+
searchParams: Record<string, string[] | string>;
|
|
269
|
+
}): Promise<{ head: ResolvedHead; layouts: ResolveHeadResult; page: ResolveHeadResult }> {
|
|
270
|
+
if (!pathRoute.resolveLayoutsHead) {
|
|
271
|
+
const page = await pathRoute.resolveHead?.({ params, searchParams });
|
|
272
|
+
return { head: resolveHeadResult(page), layouts: undefined, page };
|
|
273
|
+
}
|
|
274
|
+
const [layouts, page] = await Promise.all([
|
|
275
|
+
pathRoute.resolveLayoutsHead({ params, searchParams }),
|
|
276
|
+
pathRoute.renderPage.resolveHead?.({ params, searchParams }),
|
|
277
|
+
]);
|
|
278
|
+
return { head: resolveHeadResult(RouteHead.mergePage(layouts, page)), layouts, page };
|
|
279
|
+
}
|
|
280
|
+
|
|
208
281
|
static async composeFallback({
|
|
209
282
|
kind,
|
|
210
283
|
route,
|
|
@@ -324,14 +397,24 @@ export class RouteElementComposer {
|
|
|
324
397
|
await routeRender.render({ children, params, searchParams } as never),
|
|
325
398
|
);
|
|
326
399
|
} catch (error) {
|
|
400
|
+
RouteElementComposer.#recordLevelError(error);
|
|
327
401
|
if (!notFound || (error as { digest?: unknown } | null)?.digest !== "AKAN_NOT_FOUND") throw error;
|
|
328
402
|
notFound.onNotFound();
|
|
329
|
-
|
|
403
|
+
try {
|
|
404
|
+
return await RouteElementComposer.#renderNotFoundInPlace(notFound, params, searchParams);
|
|
405
|
+
} catch (viewError) {
|
|
406
|
+
RouteElementComposer.#recordLevelError(viewError);
|
|
407
|
+
throw viewError;
|
|
408
|
+
}
|
|
330
409
|
} finally {
|
|
331
410
|
gate?.leave();
|
|
332
411
|
}
|
|
333
412
|
};
|
|
334
413
|
|
|
414
|
+
static #recordLevelError(error: unknown): void {
|
|
415
|
+
if (typeof error === "object" && error !== null) RouteElementComposer.#levelErrors.add(error);
|
|
416
|
+
}
|
|
417
|
+
|
|
335
418
|
static async #renderNotFoundInPlace(
|
|
336
419
|
{ owners, pathname, systemFallback, head }: RouteNotFoundInPlace & { owners: RouteRender[] },
|
|
337
420
|
params: Record<string, string>,
|
package/server/routeState.ts
CHANGED
|
@@ -16,7 +16,8 @@ export const AKAN_RSC_PATCH_START_SEGMENT_HEADER = "X-Akan-Rsc-Patch-Start-Segme
|
|
|
16
16
|
export const AKAN_RSC_PATCH_HEAD_SAFE_HEADER = "X-Akan-Rsc-Patch-Head-Safe";
|
|
17
17
|
export const AKAN_RSC_PATCH_HEAD_SNAPSHOT_HEADER = "X-Akan-Rsc-Patch-Head-Snapshot";
|
|
18
18
|
export const AKAN_RSC_HEAD_SNAPSHOT_VERSION = 1;
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
export const AKAN_RSC_HEAD_SNAPSHOT_MAX_HEADER_BYTES = 2 * 1024;
|
|
20
21
|
|
|
21
22
|
export type AkanRscPartialStatus = "full" | "candidate" | "patch" | "fallback";
|
|
22
23
|
|
|
@@ -32,6 +33,8 @@ export interface AkanRouterStateV1 {
|
|
|
32
33
|
href: string;
|
|
33
34
|
routeId: string;
|
|
34
35
|
segments: AkanRouteSegmentState[];
|
|
36
|
+
/** Key of the layouts' head the page rendered in a route head; a head-safe patch keeps that head, so needs a match. */
|
|
37
|
+
headKey?: string;
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
export type AkanHeadSnapshotTag = "title" | "meta" | "link";
|
|
@@ -137,6 +140,7 @@ export function isAkanRouterStateV1(value: unknown): value is AkanRouterStateV1
|
|
|
137
140
|
(state.buildId === undefined || typeof state.buildId === "number") &&
|
|
138
141
|
typeof state.href === "string" &&
|
|
139
142
|
typeof state.routeId === "string" &&
|
|
143
|
+
(state.headKey === undefined || typeof state.headKey === "string") &&
|
|
140
144
|
Array.isArray(state.segments) &&
|
|
141
145
|
state.segments.every(isSegmentState)
|
|
142
146
|
);
|
|
@@ -188,6 +188,7 @@ export class RouteTreeBuilder {
|
|
|
188
188
|
renderRootLayouts: layer.pageRenderRootLayouts,
|
|
189
189
|
renderLayouts: layer.pageRenderLayouts,
|
|
190
190
|
resolveHead: RouteTreeBuilder.#composePageHead(route.renderPage.resolveHead, pageHead),
|
|
191
|
+
resolveLayoutsHead: pageHead,
|
|
191
192
|
pageState: route.pageState ?? defaultPageState,
|
|
192
193
|
},
|
|
193
194
|
]
|