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
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type { PageState, PathRoute } from "akanjs/client";
|
|
2
|
+
import type { Logger } from "akanjs/common";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { RouteHead } from "../client/route/RouteHead";
|
|
5
|
+
import { akanHeadSnapshotKey, createAkanLocaleAlternateHeadSnapshot, mergeAkanHeadSnapshots } from "./head";
|
|
6
|
+
import { RouteElementComposer } from "./routeElementComposer";
|
|
7
|
+
import {
|
|
8
|
+
AKAN_RSC_HEAD_SNAPSHOT_MAX_HEADER_BYTES,
|
|
9
|
+
type AkanHeadSnapshotV1,
|
|
10
|
+
type AkanRouterStateV1,
|
|
11
|
+
type AkanRscPatchDecision,
|
|
12
|
+
createAkanRouterState,
|
|
13
|
+
isAkanRscPartialCommitEnabled,
|
|
14
|
+
readAkanRouterStateRequest,
|
|
15
|
+
resolveAkanRscPartialDecision,
|
|
16
|
+
resolveAkanRscPatchDecision,
|
|
17
|
+
} from "./routeState";
|
|
18
|
+
import { RouteTreeBuilder } from "./routeTreeBuilder";
|
|
19
|
+
import { resolveAkanRscHeadSafePatchDecision } from "./rscWorkerCache";
|
|
20
|
+
|
|
21
|
+
type RouteHeadLayers = Awaited<ReturnType<typeof RouteElementComposer.resolveHeadLayers>>;
|
|
22
|
+
type RouteMatch = { pathRoute: PathRoute; params: Record<string, string> };
|
|
23
|
+
type SearchParams = Record<string, string | string[]>;
|
|
24
|
+
type FrameChange = "params-changed" | "layouts-changed" | "page-state-changed" | "frame-unknown";
|
|
25
|
+
|
|
26
|
+
export interface RouteHeadSnapshots {
|
|
27
|
+
layouts: AkanHeadSnapshotV1;
|
|
28
|
+
layoutsKey: string;
|
|
29
|
+
page: AkanHeadSnapshotV1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ResolvedRouteHead {
|
|
33
|
+
layers: RouteHeadLayers;
|
|
34
|
+
snapshots?: RouteHeadSnapshots;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface RscPatchPlan {
|
|
38
|
+
targetRouterState: AkanRouterStateV1 | null;
|
|
39
|
+
currentRouterState: ReturnType<typeof readAkanRouterStateRequest>;
|
|
40
|
+
decision: AkanRscPatchDecision;
|
|
41
|
+
/** Resolved once the route's guards admitted a head-safe patch; the render reuses it rather than run it again. */
|
|
42
|
+
admittedHead?: ResolvedRouteHead;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface RscPatchPlannerProps {
|
|
46
|
+
logger: Logger;
|
|
47
|
+
routes: () => PathRoute[];
|
|
48
|
+
buildId: () => number;
|
|
49
|
+
basePathOf: (url: URL) => string | null;
|
|
50
|
+
localeAlternatesOf: (url: URL) => Record<string, string>;
|
|
51
|
+
defaultHead: () => ReactNode;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class RscPatchPlanner {
|
|
55
|
+
readonly #props: RscPatchPlannerProps;
|
|
56
|
+
readonly #headTooLargeRoutes = new Set<string>();
|
|
57
|
+
|
|
58
|
+
constructor(props: RscPatchPlannerProps) {
|
|
59
|
+
this.#props = props;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static routeStateWith(state: AkanRouterStateV1 | null, headKey: string | undefined): AkanRouterStateV1 | null {
|
|
63
|
+
return state && headKey ? { ...state, headKey } : state;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async plan({
|
|
67
|
+
request,
|
|
68
|
+
url,
|
|
69
|
+
match,
|
|
70
|
+
searchParams,
|
|
71
|
+
admitted = false,
|
|
72
|
+
}: {
|
|
73
|
+
request: Request;
|
|
74
|
+
url: URL;
|
|
75
|
+
match: RouteMatch | null;
|
|
76
|
+
searchParams: SearchParams;
|
|
77
|
+
admitted?: boolean;
|
|
78
|
+
}): Promise<RscPatchPlan> {
|
|
79
|
+
const currentRouterState = readAkanRouterStateRequest(request.headers);
|
|
80
|
+
if (!match) {
|
|
81
|
+
const decision = { status: "full" as const, reason: "missing-route", commonPrefixLength: 0 };
|
|
82
|
+
return { targetRouterState: null, currentRouterState, decision };
|
|
83
|
+
}
|
|
84
|
+
const targetRouterState = createAkanRouterState({
|
|
85
|
+
pathRoute: match.pathRoute,
|
|
86
|
+
href: url.href,
|
|
87
|
+
buildId: this.#props.buildId(),
|
|
88
|
+
});
|
|
89
|
+
const partialDecision = resolveAkanRscPartialDecision({
|
|
90
|
+
currentState: currentRouterState.state,
|
|
91
|
+
currentRoute: currentRouterState.currentRoute,
|
|
92
|
+
targetState: targetRouterState,
|
|
93
|
+
});
|
|
94
|
+
const patchDecision = resolveAkanRscPatchDecision({
|
|
95
|
+
currentState: currentRouterState.state,
|
|
96
|
+
targetState: targetRouterState,
|
|
97
|
+
partialDecision,
|
|
98
|
+
});
|
|
99
|
+
const { decision, admittedHead } = await this.#headSafe(
|
|
100
|
+
url,
|
|
101
|
+
match,
|
|
102
|
+
searchParams,
|
|
103
|
+
currentRouterState.state,
|
|
104
|
+
patchDecision,
|
|
105
|
+
admitted,
|
|
106
|
+
);
|
|
107
|
+
return { targetRouterState, currentRouterState, decision, admittedHead };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async resolveHead(
|
|
111
|
+
url: URL,
|
|
112
|
+
pathRoute: PathRoute,
|
|
113
|
+
params: Record<string, string>,
|
|
114
|
+
searchParams: SearchParams,
|
|
115
|
+
): Promise<ResolvedRouteHead> {
|
|
116
|
+
const layers = await RouteElementComposer.resolveHeadLayers({ pathRoute, params, searchParams });
|
|
117
|
+
return { layers, snapshots: this.#snapshotsOf(url, layers) };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async #headSafe(
|
|
121
|
+
url: URL,
|
|
122
|
+
match: RouteMatch,
|
|
123
|
+
searchParams: SearchParams,
|
|
124
|
+
current: AkanRouterStateV1 | null,
|
|
125
|
+
patchDecision: AkanRscPatchDecision,
|
|
126
|
+
admitted: boolean,
|
|
127
|
+
): Promise<{ decision: AkanRscPatchDecision; admittedHead?: ResolvedRouteHead }> {
|
|
128
|
+
if (patchDecision.status !== "patch" || !patchDecision.patch) return { decision: patchDecision };
|
|
129
|
+
if (!isAkanRscPartialCommitEnabled()) {
|
|
130
|
+
const { commonPrefixLength } = patchDecision;
|
|
131
|
+
return { decision: { status: "full", reason: "guard-disabled", commonPrefixLength } };
|
|
132
|
+
}
|
|
133
|
+
const pageConfig = await match.pathRoute.renderPage.getPageConfig?.();
|
|
134
|
+
const routeHeadKey = current?.headKey;
|
|
135
|
+
const routeHeadMounted = routeHeadKey !== undefined;
|
|
136
|
+
const frameChange =
|
|
137
|
+
pageConfig?.rscPatchHeadSafe === true && routeHeadMounted
|
|
138
|
+
? await this.#frameChangeOf(url, match, current, patchDecision.patch.patchStartIndex)
|
|
139
|
+
: undefined;
|
|
140
|
+
let admittedHead: ResolvedRouteHead | undefined;
|
|
141
|
+
if (pageConfig?.rscPatchHeadSafe === true && routeHeadMounted && !frameChange) {
|
|
142
|
+
|
|
143
|
+
if (!admitted)
|
|
144
|
+
await RouteElementComposer.admit({ pathRoute: match.pathRoute, params: match.params, searchParams });
|
|
145
|
+
admittedHead = await this.resolveHead(url, match.pathRoute, match.params, searchParams);
|
|
146
|
+
}
|
|
147
|
+
const snapshots = admittedHead?.snapshots;
|
|
148
|
+
const decision = resolveAkanRscHeadSafePatchDecision({
|
|
149
|
+
partialCommitEnabled: true,
|
|
150
|
+
patchDecision,
|
|
151
|
+
pageConfig,
|
|
152
|
+
routeHeadMounted,
|
|
153
|
+
frameChange,
|
|
154
|
+
headSnapshot: snapshots?.page,
|
|
155
|
+
layoutsHeadChanged: !!snapshots && snapshots.layoutsKey !== routeHeadKey,
|
|
156
|
+
});
|
|
157
|
+
if (decision.reason === "head-too-large") this.#warnHeadTooLarge(url, match.pathRoute.path);
|
|
158
|
+
return { decision, admittedHead };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#warnHeadTooLarge(url: URL, routePath: string) {
|
|
162
|
+
if (this.#headTooLargeRoutes.has(routePath)) return;
|
|
163
|
+
this.#headTooLargeRoutes.add(routePath);
|
|
164
|
+
const alternates = Object.keys(this.#props.localeAlternatesOf(url)).length;
|
|
165
|
+
this.#props.logger.warn(
|
|
166
|
+
`[rsc] ${routePath} renders whole on sibling navigations: its page head and ${alternates} locale alternate links are over the ${AKAN_RSC_HEAD_SNAPSHOT_MAX_HEADER_BYTES}-byte patch header limit`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#snapshotsOf(url: URL, { head, layouts, page }: RouteHeadLayers): RouteHeadSnapshots | undefined {
|
|
171
|
+
if (!isAkanRscPartialCommitEnabled()) return undefined;
|
|
172
|
+
const layoutsSnapshot = RouteHead.snapshotOf(RouteHead.mergePage(layouts, undefined));
|
|
173
|
+
const pageSnapshot = RouteHead.snapshotOf(page ?? (head.node == null ? this.#props.defaultHead() : undefined));
|
|
174
|
+
if (!layoutsSnapshot || !pageSnapshot) return undefined;
|
|
175
|
+
const alternates = createAkanLocaleAlternateHeadSnapshot(this.#props.localeAlternatesOf(url));
|
|
176
|
+
return {
|
|
177
|
+
layouts: layoutsSnapshot,
|
|
178
|
+
layoutsKey: akanHeadSnapshotKey(layoutsSnapshot),
|
|
179
|
+
page: mergeAkanHeadSnapshots(pageSnapshot, alternates) ?? pageSnapshot,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async #frameChangeOf(
|
|
184
|
+
url: URL,
|
|
185
|
+
match: RouteMatch,
|
|
186
|
+
current: AkanRouterStateV1 | null,
|
|
187
|
+
patchStartIndex: number,
|
|
188
|
+
): Promise<FrameChange | undefined> {
|
|
189
|
+
const currentUrl = current && URL.canParse(current.href) ? new URL(current.href) : null;
|
|
190
|
+
const currentMatch = currentUrl ? RouteTreeBuilder.match(currentUrl.pathname, this.#props.routes()) : null;
|
|
191
|
+
if (!currentMatch) return "frame-unknown";
|
|
192
|
+
const paramNames = new Set([...Object.keys(currentMatch.params), ...Object.keys(match.params)]);
|
|
193
|
+
if ([...paramNames].some((name) => currentMatch.params[name] !== match.params[name])) return "params-changed";
|
|
194
|
+
const keptLayouts = (route: PathRoute) =>
|
|
195
|
+
[...route.renderRootLayouts, ...route.renderLayouts].slice(0, patchStartIndex);
|
|
196
|
+
const [fromLayouts, toLayouts] = [keptLayouts(currentMatch.pathRoute), keptLayouts(match.pathRoute)];
|
|
197
|
+
if (fromLayouts.length !== toLayouts.length || fromLayouts.some((layout, index) => layout !== toLayouts[index]))
|
|
198
|
+
return "layouts-changed";
|
|
199
|
+
try {
|
|
200
|
+
const basePath = this.#props.basePathOf(url);
|
|
201
|
+
const [from, to] = await Promise.all([
|
|
202
|
+
RouteElementComposer.resolveSsrFramePathRoute({ pathRoute: currentMatch.pathRoute, basePath }),
|
|
203
|
+
RouteElementComposer.resolveSsrFramePathRoute({ pathRoute: match.pathRoute, basePath }),
|
|
204
|
+
]);
|
|
205
|
+
return JSON.stringify(RscPatchPlanner.#keptFrameOf(from.pageState)) ===
|
|
206
|
+
JSON.stringify(RscPatchPlanner.#keptFrameOf(to.pageState))
|
|
207
|
+
? undefined
|
|
208
|
+
: "page-state-changed";
|
|
209
|
+
} catch (error) {
|
|
210
|
+
this.#props.logger.warn(
|
|
211
|
+
`[rsc] frame of ${current?.routeId ?? "?"} did not resolve, so the patch renders whole: ${error}`,
|
|
212
|
+
);
|
|
213
|
+
return "frame-unknown";
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
static #keptFrameOf({ cache: _cache, ssr: _ssr, ...frame }: PageState) {
|
|
218
|
+
return frame;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { PrefetchPriority } from "../client/rscNavigation";
|
|
2
|
+
|
|
1
3
|
interface RscPrefetchEntry {
|
|
2
4
|
account: string;
|
|
3
5
|
startedAt: number;
|
|
@@ -6,7 +8,10 @@ interface RscPrefetchEntry {
|
|
|
6
8
|
release: () => void;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
interface RscPrefetchQueued {
|
|
12
|
+
account: string;
|
|
13
|
+
holders: number;
|
|
14
|
+
}
|
|
10
15
|
|
|
11
16
|
export interface RscPrefetched {
|
|
12
17
|
response: Promise<Response | null>;
|
|
@@ -31,6 +36,7 @@ export class RscPrefetchCache {
|
|
|
31
36
|
static readonly defaultMaxConcurrent = 2;
|
|
32
37
|
static readonly defaultMaxEntries = 16;
|
|
33
38
|
static readonly defaultMaxQueued = 16;
|
|
39
|
+
static readonly #none = () => undefined;
|
|
34
40
|
|
|
35
41
|
#fetchResponse: RscPrefetchCacheOptions["fetchResponse"];
|
|
36
42
|
#ttlMs: number;
|
|
@@ -40,8 +46,8 @@ export class RscPrefetchCache {
|
|
|
40
46
|
#maxQueued: number;
|
|
41
47
|
#now: () => number;
|
|
42
48
|
#entries = new Map<string, RscPrefetchEntry>();
|
|
43
|
-
#high = new Map<string,
|
|
44
|
-
#low = new Map<string,
|
|
49
|
+
#high = new Map<string, RscPrefetchQueued>();
|
|
50
|
+
#low = new Map<string, RscPrefetchQueued>();
|
|
45
51
|
#active = 0;
|
|
46
52
|
|
|
47
53
|
constructor(options: RscPrefetchCacheOptions) {
|
|
@@ -67,19 +73,32 @@ export class RscPrefetchCache {
|
|
|
67
73
|
return !!entry && entry.account === account && !this.#isStale(entry);
|
|
68
74
|
}
|
|
69
75
|
|
|
70
|
-
request(href: string, account: string, priority:
|
|
76
|
+
request(href: string, account: string, priority: PrefetchPriority = "high"): () => void {
|
|
71
77
|
this.#sweep();
|
|
72
|
-
if (this.has(href, account)) return;
|
|
73
|
-
if (priority === "low" && this.#high.has(href)) return;
|
|
74
|
-
this.#high.delete(href);
|
|
75
|
-
this.#low.delete(href);
|
|
78
|
+
if (this.has(href, account)) return RscPrefetchCache.#none;
|
|
79
|
+
if (priority === "low" && this.#high.has(href)) return RscPrefetchCache.#none;
|
|
76
80
|
const queue = priority === "high" ? this.#high : this.#low;
|
|
77
|
-
queue.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
const held = queue.get(href);
|
|
82
|
+
const queued: RscPrefetchQueued = held?.account === account ? held : { account, holders: 0 };
|
|
83
|
+
if (priority === "high" || queued !== held) {
|
|
84
|
+
this.#high.delete(href);
|
|
85
|
+
this.#low.delete(href);
|
|
86
|
+
if (priority === "low" && queue.size >= this.#maxQueued) return RscPrefetchCache.#none;
|
|
87
|
+
queue.set(href, queued);
|
|
88
|
+
for (const oldest of queue.keys()) {
|
|
89
|
+
if (queue.size <= this.#maxQueued) break;
|
|
90
|
+
queue.delete(oldest);
|
|
91
|
+
}
|
|
81
92
|
}
|
|
93
|
+
queued.holders += 1;
|
|
82
94
|
this.#pump();
|
|
95
|
+
let withdrawn = false;
|
|
96
|
+
return () => {
|
|
97
|
+
if (withdrawn) return;
|
|
98
|
+
withdrawn = true;
|
|
99
|
+
queued.holders -= 1;
|
|
100
|
+
if (queued.holders === 0 && queue.get(href) === queued) queue.delete(href);
|
|
101
|
+
};
|
|
83
102
|
}
|
|
84
103
|
|
|
85
104
|
take(href: string, account: string, signal?: AbortSignal): RscPrefetched | null {
|
|
@@ -121,11 +140,12 @@ export class RscPrefetchCache {
|
|
|
121
140
|
const newest = [...this.#high].at(-1);
|
|
122
141
|
if (newest) {
|
|
123
142
|
this.#high.delete(newest[0]);
|
|
124
|
-
return newest;
|
|
143
|
+
return [newest[0], newest[1].account];
|
|
125
144
|
}
|
|
126
145
|
const oldest = this.#low.entries().next().value;
|
|
127
|
-
if (oldest)
|
|
128
|
-
|
|
146
|
+
if (!oldest) return undefined;
|
|
147
|
+
this.#low.delete(oldest[0]);
|
|
148
|
+
return [oldest[0], oldest[1].account];
|
|
129
149
|
}
|
|
130
150
|
|
|
131
151
|
#start(href: string, account: string): void {
|
|
@@ -154,10 +174,12 @@ export class RscPrefetchCache {
|
|
|
154
174
|
response: this.#fetchResponse(href, controller.signal).then(
|
|
155
175
|
(response) => {
|
|
156
176
|
|
|
177
|
+
const optedOut = response.status === 204;
|
|
178
|
+
|
|
157
179
|
const usable = response.ok || response.status === 404;
|
|
158
|
-
if (controller.signal.aborted || !usable || !response.body) {
|
|
180
|
+
if (controller.signal.aborted || optedOut || !usable || !response.body) {
|
|
159
181
|
void response.body?.cancel().catch(() => undefined);
|
|
160
|
-
forget();
|
|
182
|
+
if (!optedOut) forget();
|
|
161
183
|
settle();
|
|
162
184
|
return null;
|
|
163
185
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AKAN_RSC_NOT_FOUND_HEADER,
|
|
3
|
+
AKAN_RSC_PATCH_HEAD_SAFE_HEADER,
|
|
4
|
+
AKAN_RSC_PATCH_HEAD_SNAPSHOT_HEADER,
|
|
5
|
+
AKAN_RSC_PATCH_SEGMENT_PATH_HEADER,
|
|
6
|
+
AKAN_RSC_PATCH_START_INDEX_HEADER,
|
|
7
|
+
AKAN_RSC_PATCH_START_SEGMENT_HEADER,
|
|
8
|
+
AKAN_RSC_RESPONSE_STATE_HEADER,
|
|
9
|
+
} from "./routeState";
|
|
10
|
+
import type { RscRedirectMethod, RscRedirectStatus, RscRenderResult } from "./rscWorkerHost";
|
|
11
|
+
import type { RscTraceMetadata } from "./ssrTypes";
|
|
12
|
+
|
|
13
|
+
export function createRscRedirectResponse(
|
|
14
|
+
location: string,
|
|
15
|
+
method: RscRedirectMethod,
|
|
16
|
+
status: RscRedirectStatus = 307,
|
|
17
|
+
): Response {
|
|
18
|
+
return new Response(JSON.stringify({ type: "redirect", location, method, status }), {
|
|
19
|
+
status: 200,
|
|
20
|
+
headers: {
|
|
21
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
22
|
+
"Cache-Control": "no-store",
|
|
23
|
+
"X-Akan-Redirect": location,
|
|
24
|
+
"X-Akan-Redirect-Method": method,
|
|
25
|
+
"X-Akan-Redirect-Status": String(status),
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function createRscStreamResponse(stream: BodyInit, status = 200): Response {
|
|
31
|
+
return new Response(stream, {
|
|
32
|
+
status,
|
|
33
|
+
headers: {
|
|
34
|
+
"Content-Type": "text/x-component; charset=utf-8",
|
|
35
|
+
"Cache-Control": "no-store",
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createRscNotFoundFallbackResponse(): Response {
|
|
41
|
+
return createRscStreamResponse("0:null\n", 404);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function appendRscTraceHeaders(headers: Headers, trace: RscTraceMetadata | undefined, explain: boolean): void {
|
|
45
|
+
if (!trace) return;
|
|
46
|
+
if (explain) {
|
|
47
|
+
if (trace.navId) headers.set("X-Akan-Rsc-Nav-Id", trace.navId);
|
|
48
|
+
headers.set("X-Akan-Rsc-Pathname", trace.pathname);
|
|
49
|
+
headers.set("X-Akan-Rsc-Route", trace.routeId);
|
|
50
|
+
headers.set("X-Akan-Rsc-Cache", trace.cache);
|
|
51
|
+
if (trace.cacheReason) headers.set("X-Akan-Rsc-Cache-Reason", trace.cacheReason);
|
|
52
|
+
if (trace.cacheKeyHash) headers.set("X-Akan-Rsc-Cache-Key", trace.cacheKeyHash);
|
|
53
|
+
if (trace.partialReason) headers.set("X-Akan-Rsc-Partial-Reason", trace.partialReason);
|
|
54
|
+
if (trace.partialCommonPrefixLength !== undefined)
|
|
55
|
+
headers.set("X-Akan-Rsc-Partial-Common-Prefix", String(trace.partialCommonPrefixLength));
|
|
56
|
+
}
|
|
57
|
+
if (trace.partial && (explain || trace.partial === "patch")) headers.set("X-Akan-Rsc-Partial", trace.partial);
|
|
58
|
+
if (trace.patchStartIndex !== undefined)
|
|
59
|
+
headers.set(AKAN_RSC_PATCH_START_INDEX_HEADER, String(trace.patchStartIndex));
|
|
60
|
+
if (trace.patchSegmentPath) headers.set(AKAN_RSC_PATCH_SEGMENT_PATH_HEADER, trace.patchSegmentPath);
|
|
61
|
+
if (trace.patchStartSegment) headers.set(AKAN_RSC_PATCH_START_SEGMENT_HEADER, trace.patchStartSegment);
|
|
62
|
+
if (trace.patchHeadSafe) headers.set(AKAN_RSC_PATCH_HEAD_SAFE_HEADER, "1");
|
|
63
|
+
if (trace.patchHeadSnapshot) headers.set(AKAN_RSC_PATCH_HEAD_SNAPSHOT_HEADER, trace.patchHeadSnapshot);
|
|
64
|
+
if (trace.routeState) headers.set(AKAN_RSC_RESPONSE_STATE_HEADER, trace.routeState);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function createRscNavigationStreamResponse(
|
|
68
|
+
result: Extract<RscRenderResult, { type: "stream" }>,
|
|
69
|
+
{ explain = false }: { explain?: boolean } = {},
|
|
70
|
+
): Promise<Response> {
|
|
71
|
+
|
|
72
|
+
if (result.status === 204) return new Response(null, { status: 204, headers: { "Cache-Control": "no-store" } });
|
|
73
|
+
|
|
74
|
+
const response = createRscStreamResponse(result.stream, result.status ?? 200);
|
|
75
|
+
appendRscTraceHeaders(response.headers, result.trace, explain);
|
|
76
|
+
if (result.status === 404) response.headers.set(AKAN_RSC_NOT_FOUND_HEADER, "view");
|
|
77
|
+
return response;
|
|
78
|
+
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { type ReactNode, use,
|
|
3
|
+
import { type ReactNode, use, useCallback } from "react";
|
|
4
|
+
import { RouteHead } from "../client/route/RouteHead";
|
|
5
|
+
import { readRscRoutePatch, subscribeRscRoutePatch, writeRscRoutePatch } from "../client/rscRoutePatch";
|
|
6
|
+
import { useTransitionStore } from "../webkit/useTransitionStore";
|
|
7
|
+
import type { AkanHeadSnapshotV1 } from "./routeState";
|
|
4
8
|
|
|
5
9
|
type RscSegmentThenable = PromiseLike<ReactNode>;
|
|
6
10
|
|
|
@@ -53,13 +57,28 @@ export function resetAkanSegmentOutletPatches(): void {
|
|
|
53
57
|
for (const listeners of store.listeners.values()) {
|
|
54
58
|
for (const listener of listeners) listener();
|
|
55
59
|
}
|
|
60
|
+
writeRscRoutePatch(null);
|
|
56
61
|
}
|
|
57
62
|
|
|
58
63
|
export function AkanSegmentOutlet({ segmentKey, children }: { segmentKey: string; children: ReactNode }): ReactNode {
|
|
59
|
-
const patchedThenable =
|
|
60
|
-
(listener) => subscribeSegment(segmentKey, listener),
|
|
61
|
-
() => getStore().entries.get(segmentKey) ?? null,
|
|
62
|
-
() => null,
|
|
64
|
+
const patchedThenable = useTransitionStore(
|
|
65
|
+
useCallback((listener: () => void) => subscribeSegment(segmentKey, listener), [segmentKey]),
|
|
66
|
+
useCallback(() => getStore().entries.get(segmentKey) ?? null, [segmentKey]),
|
|
63
67
|
);
|
|
64
68
|
return patchedThenable ? use(patchedThenable) : children;
|
|
65
69
|
}
|
|
70
|
+
|
|
71
|
+
const subscribeRouteHead = (listener: () => void) => subscribeRscRoutePatch(listener, { head: true });
|
|
72
|
+
|
|
73
|
+
const renderHeadSnapshot = (snapshot: AkanHeadSnapshotV1): ReactNode =>
|
|
74
|
+
snapshot.nodes.map((node, index) => {
|
|
75
|
+
const key = `${node.tag}:${index}`;
|
|
76
|
+
if (node.tag === "title") return <title key={key}>{node.text ?? ""}</title>;
|
|
77
|
+
if (node.tag === "meta") return <meta key={key} {...node.attrs} />;
|
|
78
|
+
return <link key={key} {...node.attrs} />;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export function AkanRouteHead({ layouts, page }: { layouts: AkanHeadSnapshotV1; page: AkanHeadSnapshotV1 }): ReactNode {
|
|
82
|
+
const patch = useTransitionStore(subscribeRouteHead, readRscRoutePatch);
|
|
83
|
+
return renderHeadSnapshot(RouteHead.mergeSnapshots([layouts.nodes, (patch?.head ?? page).nodes]));
|
|
84
|
+
}
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import type { AkanSegmentOutlet } from "./rscSegmentOutlet";
|
|
1
|
+
import type { AkanRouteHead, AkanSegmentOutlet } from "./rscSegmentOutlet";
|
|
2
2
|
|
|
3
3
|
const AKAN_SEGMENT_OUTLET_REFERENCE_ID = "pkgs/akanjs/server/rscSegmentOutlet.tsx";
|
|
4
4
|
|
|
5
|
-
function
|
|
6
|
-
throw new Error("[rscSegmentOutlet] AkanSegmentOutlet is a client component and cannot run on the server");
|
|
7
|
-
}
|
|
5
|
+
function resolveAkanSegmentOutletReference<T>(exportName: string): T {
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
const serverReference = (): never => {
|
|
8
|
+
throw new Error(`[rscSegmentOutlet] ${exportName} is a client component and cannot run on the server`);
|
|
9
|
+
};
|
|
10
10
|
try {
|
|
11
11
|
const { registerClientReference } = require("react-server-dom-webpack/server.node") as {
|
|
12
12
|
registerClientReference<T>(proxyImplementation: T, id: string, exportName: string): T;
|
|
13
13
|
};
|
|
14
|
-
return registerClientReference(
|
|
15
|
-
createAkanSegmentOutletServerReference as unknown as typeof AkanSegmentOutlet,
|
|
16
|
-
AKAN_SEGMENT_OUTLET_REFERENCE_ID,
|
|
17
|
-
"AkanSegmentOutlet",
|
|
18
|
-
);
|
|
14
|
+
return registerClientReference(serverReference as unknown as T, AKAN_SEGMENT_OUTLET_REFERENCE_ID, exportName);
|
|
19
15
|
} catch {
|
|
20
|
-
return
|
|
16
|
+
return serverReference as unknown as T;
|
|
21
17
|
}
|
|
22
18
|
}
|
|
23
19
|
|
|
24
|
-
export const AkanSegmentOutletReference =
|
|
20
|
+
export const AkanSegmentOutletReference =
|
|
21
|
+
resolveAkanSegmentOutletReference<typeof AkanSegmentOutlet>("AkanSegmentOutlet");
|
|
22
|
+
export const AkanRouteHeadReference = resolveAkanSegmentOutletReference<typeof AkanRouteHead>("AkanRouteHead");
|