@rangojs/router 0.0.0-experimental.259 → 0.0.0-experimental.26
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/README.md +294 -28
- package/dist/bin/rango.js +355 -47
- package/dist/vite/index.js +1658 -1239
- package/package.json +3 -3
- package/skills/cache-guide/SKILL.md +9 -5
- package/skills/caching/SKILL.md +4 -4
- package/skills/document-cache/SKILL.md +2 -2
- package/skills/hooks/SKILL.md +40 -29
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +79 -0
- package/skills/layout/SKILL.md +62 -2
- package/skills/loader/SKILL.md +229 -15
- package/skills/middleware/SKILL.md +109 -30
- package/skills/parallel/SKILL.md +57 -2
- package/skills/prerender/SKILL.md +189 -19
- package/skills/rango/SKILL.md +1 -2
- package/skills/response-routes/SKILL.md +3 -3
- package/skills/route/SKILL.md +44 -3
- package/skills/router-setup/SKILL.md +80 -3
- package/skills/theme/SKILL.md +5 -4
- package/skills/typesafety/SKILL.md +59 -16
- package/skills/use-cache/SKILL.md +16 -2
- package/src/__internal.ts +1 -1
- package/src/bin/rango.ts +56 -19
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/event-controller.ts +29 -48
- package/src/browser/history-state.ts +80 -0
- package/src/browser/intercept-utils.ts +1 -1
- package/src/browser/link-interceptor.ts +19 -3
- package/src/browser/merge-segment-loaders.ts +9 -2
- package/src/browser/navigation-bridge.ts +66 -443
- package/src/browser/navigation-client.ts +34 -62
- package/src/browser/navigation-store.ts +4 -33
- package/src/browser/navigation-transaction.ts +295 -0
- package/src/browser/partial-update.ts +103 -151
- package/src/browser/prefetch/cache.ts +67 -0
- package/src/browser/prefetch/fetch.ts +137 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +42 -0
- package/src/browser/prefetch/queue.ts +88 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +154 -44
- package/src/browser/react/NavigationProvider.tsx +32 -0
- package/src/browser/react/context.ts +6 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +2 -6
- package/src/browser/react/location-state-shared.ts +29 -11
- package/src/browser/react/location-state.ts +6 -4
- package/src/browser/react/nonce-context.ts +23 -0
- package/src/browser/react/shallow-equal.ts +27 -0
- package/src/browser/react/use-action.ts +23 -45
- package/src/browser/react/use-client-cache.ts +5 -3
- package/src/browser/react/use-handle.ts +21 -64
- package/src/browser/react/use-navigation.ts +7 -32
- package/src/browser/react/use-params.ts +5 -34
- package/src/browser/react/use-pathname.ts +2 -3
- package/src/browser/react/use-router.ts +3 -6
- package/src/browser/react/use-search-params.ts +2 -1
- package/src/browser/react/use-segments.ts +75 -114
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +46 -22
- package/src/browser/scroll-restoration.ts +10 -7
- package/src/browser/server-action-bridge.ts +458 -405
- package/src/browser/types.ts +21 -35
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +38 -13
- package/src/build/generate-route-types.ts +4 -0
- package/src/build/index.ts +1 -0
- package/src/build/route-trie.ts +19 -3
- package/src/build/route-types/codegen.ts +13 -4
- package/src/build/route-types/include-resolution.ts +13 -0
- package/src/build/route-types/per-module-writer.ts +15 -3
- package/src/build/route-types/router-processing.ts +170 -18
- package/src/build/runtime-discovery.ts +13 -1
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-key-utils.ts +44 -0
- package/src/cache/cache-policy.ts +125 -0
- package/src/cache/cache-runtime.ts +136 -123
- package/src/cache/cache-scope.ts +76 -83
- package/src/cache/cf/cf-cache-store.ts +12 -7
- package/src/cache/document-cache.ts +93 -69
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/index.ts +0 -15
- package/src/cache/memory-segment-store.ts +43 -69
- package/src/cache/profile-registry.ts +43 -8
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +140 -117
- package/src/cache/taint.ts +30 -3
- package/src/cache/types.ts +1 -115
- package/src/client.rsc.tsx +0 -1
- package/src/client.tsx +53 -76
- package/src/errors.ts +6 -1
- package/src/handle.ts +1 -1
- package/src/handles/MetaTags.tsx +5 -2
- package/src/host/cookie-handler.ts +8 -3
- package/src/host/index.ts +0 -3
- package/src/host/router.ts +14 -1
- package/src/href-client.ts +3 -1
- package/src/index.rsc.ts +53 -10
- package/src/index.ts +73 -43
- package/src/loader.rsc.ts +12 -4
- package/src/loader.ts +8 -0
- package/src/prerender/store.ts +60 -18
- package/src/prerender.ts +76 -18
- package/src/reverse.ts +11 -7
- package/src/root-error-boundary.tsx +30 -26
- package/src/route-definition/dsl-helpers.ts +9 -6
- package/src/route-definition/index.ts +0 -3
- package/src/route-definition/redirect.ts +15 -3
- package/src/route-map-builder.ts +38 -2
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +7 -0
- package/src/router/content-negotiation.ts +1 -1
- package/src/router/debug-manifest.ts +16 -3
- package/src/router/handler-context.ts +96 -17
- package/src/router/intercept-resolution.ts +6 -4
- package/src/router/lazy-includes.ts +4 -0
- package/src/router/loader-resolution.ts +6 -11
- package/src/router/logging.ts +100 -3
- package/src/router/manifest.ts +32 -3
- package/src/router/match-api.ts +62 -54
- package/src/router/match-context.ts +3 -0
- package/src/router/match-handlers.ts +185 -11
- package/src/router/match-middleware/background-revalidation.ts +65 -85
- package/src/router/match-middleware/cache-lookup.ts +78 -10
- package/src/router/match-middleware/cache-store.ts +2 -0
- package/src/router/match-pipelines.ts +8 -43
- package/src/router/match-result.ts +0 -9
- package/src/router/metrics.ts +233 -13
- package/src/router/middleware-types.ts +34 -39
- package/src/router/middleware.ts +290 -130
- package/src/router/pattern-matching.ts +61 -10
- package/src/router/prerender-match.ts +36 -6
- package/src/router/preview-match.ts +7 -1
- package/src/router/revalidation.ts +61 -2
- package/src/router/router-context.ts +15 -0
- package/src/router/router-interfaces.ts +158 -40
- package/src/router/router-options.ts +223 -1
- package/src/router/router-registry.ts +5 -2
- package/src/router/segment-resolution/fresh.ts +165 -242
- package/src/router/segment-resolution/helpers.ts +263 -0
- package/src/router/segment-resolution/loader-cache.ts +102 -98
- package/src/router/segment-resolution/revalidation.ts +394 -272
- package/src/router/segment-resolution/static-store.ts +2 -2
- package/src/router/segment-resolution.ts +1 -3
- package/src/router/segment-wrappers.ts +3 -0
- package/src/router/telemetry-otel.ts +299 -0
- package/src/router/telemetry.ts +300 -0
- package/src/router/timeout.ts +148 -0
- package/src/router/trie-matching.ts +20 -2
- package/src/router/types.ts +7 -1
- package/src/router.ts +203 -18
- package/src/rsc/handler-context.ts +13 -2
- package/src/rsc/handler.ts +489 -438
- package/src/rsc/helpers.ts +125 -5
- package/src/rsc/index.ts +0 -20
- package/src/rsc/loader-fetch.ts +84 -42
- package/src/rsc/manifest-init.ts +3 -2
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +245 -19
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +47 -43
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +166 -66
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +20 -2
- package/src/search-params.ts +38 -23
- package/src/server/context.ts +61 -7
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +11 -6
- package/src/server/handle-store.ts +84 -12
- package/src/server/loader-registry.ts +11 -46
- package/src/server/request-context.ts +275 -49
- package/src/server.ts +6 -0
- package/src/ssr/index.tsx +67 -28
- package/src/static-handler.ts +7 -0
- package/src/theme/ThemeProvider.tsx +6 -1
- package/src/theme/index.ts +4 -18
- package/src/theme/theme-context.ts +1 -28
- package/src/theme/theme-script.ts +2 -1
- package/src/types/cache-types.ts +6 -1
- package/src/types/error-types.ts +3 -0
- package/src/types/global-namespace.ts +22 -0
- package/src/types/handler-context.ts +103 -16
- package/src/types/index.ts +1 -1
- package/src/types/loader-types.ts +9 -6
- package/src/types/route-config.ts +17 -26
- package/src/types/route-entry.ts +28 -0
- package/src/types/segments.ts +0 -5
- package/src/urls/include-helper.ts +49 -8
- package/src/urls/index.ts +1 -0
- package/src/urls/path-helper-types.ts +30 -12
- package/src/urls/path-helper.ts +17 -2
- package/src/urls/pattern-types.ts +21 -1
- package/src/urls/response-types.ts +29 -7
- package/src/urls/type-extraction.ts +23 -15
- package/src/use-loader.tsx +27 -9
- package/src/vite/discovery/bundle-postprocess.ts +32 -52
- package/src/vite/discovery/discover-routers.ts +52 -26
- package/src/vite/discovery/prerender-collection.ts +58 -41
- package/src/vite/discovery/route-types-writer.ts +7 -7
- package/src/vite/discovery/state.ts +7 -7
- package/src/vite/discovery/virtual-module-codegen.ts +5 -2
- package/src/vite/index.ts +10 -51
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +3 -3
- package/src/vite/plugins/expose-internal-ids.ts +4 -3
- package/src/vite/plugins/refresh-cmd.ts +65 -0
- package/src/vite/plugins/use-cache-transform.ts +91 -3
- package/src/vite/plugins/version-plugin.ts +188 -18
- package/src/vite/rango.ts +61 -36
- package/src/vite/router-discovery.ts +173 -100
- package/src/vite/utils/prerender-utils.ts +81 -0
- package/src/vite/utils/shared-utils.ts +19 -9
- package/skills/testing/SKILL.md +0 -226
- package/src/browser/lru-cache.ts +0 -61
- package/src/browser/react/prefetch.ts +0 -27
- package/src/browser/request-controller.ts +0 -164
- package/src/cache/memory-store.ts +0 -253
- package/src/href-context.ts +0 -33
- package/src/route-definition/route-function.ts +0 -119
- package/src/router.gen.ts +0 -6
- package/src/static-handler.gen.ts +0 -5
- package/src/urls.gen.ts +0 -8
- /package/{CLAUDE.md → AGENTS.md} +0 -0
|
@@ -4,12 +4,11 @@ import type {
|
|
|
4
4
|
RscPayload,
|
|
5
5
|
} from "./types.js";
|
|
6
6
|
import { createPartialUpdater } from "./partial-update.js";
|
|
7
|
-
import { createNavigationTransaction } from "./navigation-
|
|
7
|
+
import { createNavigationTransaction } from "./navigation-transaction.js";
|
|
8
8
|
import {
|
|
9
9
|
reconcileSegments,
|
|
10
10
|
reconcileErrorSegments,
|
|
11
11
|
} from "./segment-reconciler.js";
|
|
12
|
-
import { classifyActionResponse } from "./action-response-classifier.js";
|
|
13
12
|
import { startTransition } from "react";
|
|
14
13
|
import type { EventController } from "./event-controller.js";
|
|
15
14
|
import {
|
|
@@ -22,6 +21,14 @@ import {
|
|
|
22
21
|
isBrowserDebugEnabled,
|
|
23
22
|
startBrowserTransaction,
|
|
24
23
|
} from "./logging.js";
|
|
24
|
+
import { validateRedirectOrigin } from "./validate-redirect-origin.js";
|
|
25
|
+
import {
|
|
26
|
+
extractRscHeaderUrl,
|
|
27
|
+
emptyResponse,
|
|
28
|
+
teeWithCompletion,
|
|
29
|
+
} from "./response-adapter.js";
|
|
30
|
+
import { mergeLocationState } from "./history-state.js";
|
|
31
|
+
import { classifyActionOutcome } from "./action-coordinator.js";
|
|
25
32
|
|
|
26
33
|
// Polyfill Symbol.dispose/asyncDispose for Safari and older browsers
|
|
27
34
|
if (typeof Symbol.dispose === "undefined") {
|
|
@@ -92,27 +99,31 @@ export function createServerActionBridge(
|
|
|
92
99
|
interceptSourceUrl?: string | null;
|
|
93
100
|
}): Promise<void> {
|
|
94
101
|
const src = opts?.interceptSourceUrl ?? null;
|
|
95
|
-
|
|
102
|
+
const navTx = createNavigationTransaction(
|
|
96
103
|
store,
|
|
97
104
|
eventController,
|
|
98
105
|
window.location.href,
|
|
99
106
|
{ replace: true, skipLoadingState: true },
|
|
100
107
|
);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
108
|
+
try {
|
|
109
|
+
await fetchPartialUpdate(
|
|
110
|
+
window.location.href,
|
|
111
|
+
opts?.segments ?? [],
|
|
112
|
+
false,
|
|
113
|
+
navTx.handle.signal,
|
|
114
|
+
navTx.with({
|
|
115
|
+
url: window.location.href,
|
|
116
|
+
storeOnly: true,
|
|
117
|
+
...(src ? { intercept: true, interceptSourceUrl: src } : {}),
|
|
118
|
+
}),
|
|
119
|
+
{
|
|
120
|
+
type: "action" as const,
|
|
121
|
+
...(src ? { interceptSourceUrl: src } : {}),
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
} finally {
|
|
125
|
+
navTx[Symbol.dispose]();
|
|
126
|
+
}
|
|
116
127
|
}
|
|
117
128
|
|
|
118
129
|
/**
|
|
@@ -130,406 +141,495 @@ export function createServerActionBridge(
|
|
|
130
141
|
log("action start", { id, argsCount: args.length });
|
|
131
142
|
|
|
132
143
|
// Start action in event controller - handles lifecycle tracking
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
144
|
+
const handle = eventController.startAction(id, args);
|
|
145
|
+
try {
|
|
146
|
+
const segmentState = store.getSegmentState();
|
|
147
|
+
|
|
148
|
+
// Mark cache as stale immediately when action starts
|
|
149
|
+
// This ensures SWR pattern kicks in if user navigates away during action
|
|
150
|
+
store.markCacheAsStaleAndBroadcast();
|
|
151
|
+
|
|
152
|
+
// Create temporary references for serialization
|
|
153
|
+
const temporaryReferences = deps.createTemporaryReferenceSet();
|
|
154
|
+
|
|
155
|
+
// Capture URL pathname at action start to detect navigation during action
|
|
156
|
+
// Must use window.location (not store.path) because intercepts change URL
|
|
157
|
+
// without changing store.path (e.g., /kanban -> /kanban/card/1)
|
|
158
|
+
const actionStartPathname = window.location.pathname;
|
|
159
|
+
|
|
160
|
+
// Build action request URL with current segments
|
|
161
|
+
const url = new URL(window.location.href);
|
|
162
|
+
url.searchParams.set("_rsc_action", id);
|
|
163
|
+
url.searchParams.set(
|
|
164
|
+
"_rsc_segments",
|
|
165
|
+
segmentState.currentSegmentIds.join(","),
|
|
166
|
+
);
|
|
167
|
+
// Add version param for version mismatch detection
|
|
168
|
+
if (version) {
|
|
169
|
+
url.searchParams.set("_rsc_v", version);
|
|
170
|
+
}
|
|
136
171
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
store.markCacheAsStaleAndBroadcast();
|
|
172
|
+
// Encode arguments
|
|
173
|
+
const encodedBody = await deps.encodeReply(args, { temporaryReferences });
|
|
140
174
|
|
|
141
|
-
|
|
142
|
-
|
|
175
|
+
log("sending action request", {
|
|
176
|
+
url: url.href,
|
|
177
|
+
bodyType: typeof encodedBody,
|
|
178
|
+
isFormData: encodedBody instanceof FormData,
|
|
179
|
+
segmentCount: segmentState.currentSegmentIds.length,
|
|
180
|
+
});
|
|
143
181
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
182
|
+
// Track when the stream completes
|
|
183
|
+
let resolveStreamComplete: () => void;
|
|
184
|
+
const streamComplete = new Promise<void>((resolve) => {
|
|
185
|
+
resolveStreamComplete = resolve;
|
|
186
|
+
});
|
|
148
187
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
188
|
+
// Get intercept source URL if in intercept context
|
|
189
|
+
const interceptSourceUrl = store.getInterceptSourceUrl();
|
|
190
|
+
|
|
191
|
+
// Track streaming token - will be set when response arrives
|
|
192
|
+
let streamingToken: { end(): void } | null = null;
|
|
193
|
+
|
|
194
|
+
// Use a dedicated abort controller for the fetch so we can cancel network
|
|
195
|
+
// I/O without disrupting the Flight stream once the response has arrived.
|
|
196
|
+
// Aborting a response mid-stream causes React's Flight decoder to throw
|
|
197
|
+
// asynchronous unhandled errors (BodyStreamBuffer was aborted).
|
|
198
|
+
const fetchAbort = new AbortController();
|
|
199
|
+
const onHandleAbort = () => fetchAbort.abort();
|
|
200
|
+
handle.signal.addEventListener("abort", onHandleAbort, { once: true });
|
|
201
|
+
|
|
202
|
+
// Send action request with stream tracking
|
|
203
|
+
const responsePromise = fetch(url, {
|
|
204
|
+
method: "POST",
|
|
205
|
+
headers: {
|
|
206
|
+
"rsc-action": id,
|
|
207
|
+
"X-RSC-Router-Client-Path": segmentState.currentUrl,
|
|
208
|
+
...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
|
|
209
|
+
// Send intercept source URL so server can maintain intercept context
|
|
210
|
+
...(interceptSourceUrl && {
|
|
211
|
+
"X-RSC-Router-Intercept-Source": interceptSourceUrl,
|
|
212
|
+
}),
|
|
213
|
+
},
|
|
214
|
+
body: encodedBody,
|
|
215
|
+
signal: fetchAbort.signal,
|
|
216
|
+
}).then(async (response) => {
|
|
217
|
+
// Response arrived — disconnect fetch abort from handle abort so
|
|
218
|
+
// abortAllActions() doesn't disrupt the in-progress Flight stream.
|
|
219
|
+
handle.signal.removeEventListener("abort", onHandleAbort);
|
|
220
|
+
|
|
221
|
+
// Check for version mismatch - server wants us to reload
|
|
222
|
+
const reload = extractRscHeaderUrl(response, "X-RSC-Reload");
|
|
223
|
+
if (reload === "blocked") {
|
|
224
|
+
resolveStreamComplete();
|
|
225
|
+
return emptyResponse();
|
|
226
|
+
}
|
|
227
|
+
if (reload) {
|
|
228
|
+
log("version mismatch on action, reloading", {
|
|
229
|
+
reloadUrl: reload.url,
|
|
230
|
+
});
|
|
231
|
+
window.location.href = reload.url;
|
|
232
|
+
return new Promise<Response>(() => {});
|
|
233
|
+
}
|
|
160
234
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// Get intercept source URL if in intercept context
|
|
178
|
-
const interceptSourceUrl = store.getInterceptSourceUrl();
|
|
179
|
-
|
|
180
|
-
// Track streaming token - will be set when response arrives
|
|
181
|
-
let streamingToken: { end(): void } | null = null;
|
|
182
|
-
|
|
183
|
-
// Send action request with stream tracking
|
|
184
|
-
const responsePromise = fetch(url, {
|
|
185
|
-
method: "POST",
|
|
186
|
-
headers: {
|
|
187
|
-
"rsc-action": id,
|
|
188
|
-
"X-RSC-Router-Client-Path": segmentState.currentUrl,
|
|
189
|
-
...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
|
|
190
|
-
// Send intercept source URL so server can maintain intercept context
|
|
191
|
-
...(interceptSourceUrl && {
|
|
192
|
-
"X-RSC-Router-Intercept-Source": interceptSourceUrl,
|
|
193
|
-
}),
|
|
194
|
-
},
|
|
195
|
-
body: encodedBody,
|
|
196
|
-
}).then(async (response) => {
|
|
197
|
-
// Check for version mismatch - server wants us to reload
|
|
198
|
-
const reloadUrl = response.headers.get("X-RSC-Reload");
|
|
199
|
-
if (reloadUrl) {
|
|
200
|
-
// Validate origin to prevent open redirect via crafted headers
|
|
201
|
-
try {
|
|
202
|
-
const target = new URL(reloadUrl, window.location.origin);
|
|
203
|
-
if (target.origin !== window.location.origin) {
|
|
204
|
-
throw new Error(
|
|
205
|
-
`X-RSC-Reload blocked: origin mismatch (${target.origin})`,
|
|
206
|
-
);
|
|
235
|
+
// Simple redirect from action (no state, no RSC payload).
|
|
236
|
+
// Short-circuits before createFromFetch — no Flight deserialization needed.
|
|
237
|
+
// Check handle.signal.aborted to avoid redirecting from a stale action
|
|
238
|
+
// when the user has already navigated away.
|
|
239
|
+
const redirect = extractRscHeaderUrl(response, "X-RSC-Redirect");
|
|
240
|
+
if (redirect && redirect !== "blocked" && !handle.signal.aborted) {
|
|
241
|
+
log("action simple redirect", { url: redirect.url });
|
|
242
|
+
handle.complete(undefined);
|
|
243
|
+
if (onNavigate) {
|
|
244
|
+
await onNavigate(redirect.url, {
|
|
245
|
+
replace: true,
|
|
246
|
+
_skipCache: true,
|
|
247
|
+
});
|
|
248
|
+
} else {
|
|
249
|
+
window.location.href = redirect.url;
|
|
207
250
|
}
|
|
208
|
-
|
|
209
|
-
console.error("[rango]", e);
|
|
210
|
-
return response;
|
|
251
|
+
return new Promise<Response>(() => {});
|
|
211
252
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
253
|
+
if (redirect === "blocked") {
|
|
254
|
+
resolveStreamComplete();
|
|
255
|
+
return emptyResponse();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Start streaming immediately when response arrives
|
|
259
|
+
if (!handle.signal.aborted) {
|
|
260
|
+
streamingToken = handle.startStreaming();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return teeWithCompletion(response, () => {
|
|
264
|
+
log("stream complete");
|
|
265
|
+
streamingToken?.end();
|
|
266
|
+
resolveStreamComplete();
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Deserialize response (MUST use same temporaryReferences)
|
|
271
|
+
let payload: RscPayload;
|
|
272
|
+
try {
|
|
273
|
+
payload = await deps.createFromFetch<RscPayload>(responsePromise, {
|
|
274
|
+
temporaryReferences,
|
|
275
|
+
});
|
|
276
|
+
} catch (error) {
|
|
277
|
+
// Clean up streaming token on error (may be null if fetch failed before .then() ran)
|
|
278
|
+
// The token is assigned in .then() callback which runs before this catch block,
|
|
279
|
+
// but TypeScript doesn't track cross-async assignments, so use type assertion
|
|
280
|
+
(streamingToken as { end(): void } | null)?.end();
|
|
281
|
+
// resolveStreamComplete is assigned in the Promise constructor so it's safe to call
|
|
282
|
+
resolveStreamComplete!();
|
|
283
|
+
|
|
284
|
+
// Silently swallow abort errors — the action was intentionally cancelled
|
|
285
|
+
// (e.g., user navigated away or abortAllActions was called).
|
|
286
|
+
// Return undefined instead of throwing to avoid surfacing as a page error.
|
|
287
|
+
// Check both DOMException AbortError and stream-level abort messages
|
|
288
|
+
// (BodyStreamBuffer was aborted) that propagate from the aborted fetch.
|
|
289
|
+
if (handle.signal.aborted) {
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Convert network-level errors to NetworkError for proper handling
|
|
294
|
+
const networkError = toNetworkError(error, {
|
|
295
|
+
url: url.toString(),
|
|
296
|
+
operation: "action",
|
|
297
|
+
});
|
|
298
|
+
if (networkError) {
|
|
299
|
+
handle.fail(networkError);
|
|
300
|
+
emitNetworkError(onUpdate, networkError, segmentState.currentUrl);
|
|
301
|
+
throw networkError;
|
|
302
|
+
}
|
|
303
|
+
throw error;
|
|
216
304
|
}
|
|
217
305
|
|
|
218
|
-
|
|
219
|
-
|
|
306
|
+
log("action response received", {
|
|
307
|
+
isPartial: payload.metadata?.isPartial,
|
|
308
|
+
isError: payload.metadata?.isError,
|
|
309
|
+
matchedCount: payload.metadata?.matched?.length ?? 0,
|
|
310
|
+
diffCount: payload.metadata?.diff?.length ?? 0,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// Guard: if the action was aborted while streaming (e.g., user navigated
|
|
314
|
+
// away or abortAllActions fired), bail out before any reconcile/render/cache
|
|
315
|
+
// writes to avoid overwriting the current UI with stale action results.
|
|
316
|
+
if (handle.signal.aborted) {
|
|
317
|
+
log("action aborted after response, skipping reconciliation");
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Process response
|
|
322
|
+
const { metadata, returnValue } = payload;
|
|
323
|
+
|
|
324
|
+
// Handle action redirect: server converted the redirect to a Flight payload
|
|
325
|
+
// so we can perform SPA navigation instead of a full page reload.
|
|
220
326
|
// Check handle.signal.aborted to avoid redirecting from a stale action
|
|
221
327
|
// when the user has already navigated away.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
328
|
+
if (metadata?.redirect && !handle.signal.aborted) {
|
|
329
|
+
const redirectUrl = validateRedirectOrigin(
|
|
330
|
+
metadata.redirect.url,
|
|
331
|
+
window.location.origin,
|
|
332
|
+
);
|
|
333
|
+
if (!redirectUrl) {
|
|
334
|
+
log("blocked action redirect payload", {
|
|
335
|
+
url: metadata.redirect.url,
|
|
227
336
|
});
|
|
337
|
+
handle.complete(returnValue?.data);
|
|
338
|
+
return returnValue?.data;
|
|
228
339
|
}
|
|
229
|
-
|
|
340
|
+
const redirectState = metadata.locationState;
|
|
341
|
+
log("action redirect", { url: redirectUrl });
|
|
342
|
+
handle.complete(returnValue?.data);
|
|
230
343
|
if (onNavigate) {
|
|
231
|
-
await onNavigate(
|
|
344
|
+
await onNavigate(redirectUrl, {
|
|
345
|
+
state: redirectState,
|
|
232
346
|
replace: true,
|
|
233
347
|
_skipCache: true,
|
|
234
348
|
});
|
|
235
349
|
} else {
|
|
236
|
-
window.location.href =
|
|
350
|
+
window.location.href = redirectUrl;
|
|
237
351
|
}
|
|
238
|
-
return
|
|
352
|
+
return returnValue?.data;
|
|
239
353
|
}
|
|
240
354
|
|
|
241
|
-
//
|
|
242
|
-
|
|
243
|
-
|
|
355
|
+
// Bail out if the action was aborted after deserialization (e.g. user
|
|
356
|
+
// navigated away or abortAllActions was called while the Flight stream
|
|
357
|
+
// was being consumed). Without this check the code below would mutate
|
|
358
|
+
// the store / UI for a stale action.
|
|
359
|
+
if (handle.signal.aborted) {
|
|
360
|
+
log("action aborted after deserialization, skipping mutations");
|
|
361
|
+
return returnValue?.data;
|
|
244
362
|
}
|
|
245
363
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
364
|
+
const { matched, diff, segments, isPartial, isError } = metadata || {};
|
|
365
|
+
|
|
366
|
+
// Log action result
|
|
367
|
+
if (returnValue && !returnValue.ok) {
|
|
368
|
+
console.error(`[Browser] Action failed:`, returnValue.data);
|
|
251
369
|
}
|
|
252
370
|
|
|
253
|
-
//
|
|
254
|
-
|
|
371
|
+
// Handle error responses with error boundary UI
|
|
372
|
+
if (isError && isPartial && segments && diff) {
|
|
373
|
+
log("processing error boundary response");
|
|
255
374
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
while (true) {
|
|
261
|
-
const { done } = await reader.read();
|
|
262
|
-
if (done) break;
|
|
263
|
-
}
|
|
264
|
-
} finally {
|
|
265
|
-
reader.releaseLock();
|
|
266
|
-
log("stream complete");
|
|
267
|
-
streamingToken?.end();
|
|
268
|
-
resolveStreamComplete();
|
|
375
|
+
// Fail current handle BEFORE aborting all actions so the event controller
|
|
376
|
+
// records the error state (abortAllActions clears inflight entries)
|
|
377
|
+
if (returnValue && !returnValue.ok) {
|
|
378
|
+
handle.fail(returnValue.data);
|
|
269
379
|
}
|
|
270
|
-
})().catch((error) => {
|
|
271
|
-
console.error("[STREAMING] Error reading tracking stream:", error);
|
|
272
|
-
streamingToken?.end();
|
|
273
|
-
});
|
|
274
380
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
status: response.status,
|
|
279
|
-
statusText: response.statusText,
|
|
280
|
-
});
|
|
281
|
-
});
|
|
381
|
+
// Abort all other pending action requests - error takes precedence
|
|
382
|
+
// This prevents other actions from completing and overwriting the error UI
|
|
383
|
+
eventController.abortAllActions();
|
|
282
384
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
(streamingToken as { end(): void } | null)?.end();
|
|
294
|
-
// resolveStreamComplete is assigned in the Promise constructor so it's safe to call
|
|
295
|
-
resolveStreamComplete!();
|
|
296
|
-
|
|
297
|
-
// Convert network-level errors to NetworkError for proper handling
|
|
298
|
-
const networkError = toNetworkError(error, {
|
|
299
|
-
url: url.toString(),
|
|
300
|
-
operation: "action",
|
|
301
|
-
});
|
|
302
|
-
if (networkError) {
|
|
303
|
-
handle.fail(networkError);
|
|
304
|
-
emitNetworkError(onUpdate, networkError, segmentState.currentUrl);
|
|
305
|
-
throw networkError;
|
|
306
|
-
}
|
|
307
|
-
throw error;
|
|
308
|
-
}
|
|
385
|
+
// Clear concurrent action tracking - no consolidation needed when showing error
|
|
386
|
+
handle.clearConsolidation();
|
|
387
|
+
|
|
388
|
+
// Get current page's cached segments
|
|
389
|
+
const currentKey = store.getHistoryKey();
|
|
390
|
+
const cached = store.getCachedSegments(currentKey);
|
|
391
|
+
const cachedSegments = cached?.segments || [];
|
|
392
|
+
|
|
393
|
+
// Reconcile error segments with cached tree
|
|
394
|
+
const errorResult = reconcileErrorSegments(cachedSegments, segments);
|
|
309
395
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
// Process response
|
|
318
|
-
const { metadata, returnValue } = payload;
|
|
319
|
-
|
|
320
|
-
// Handle action redirect: server converted the redirect to a Flight payload
|
|
321
|
-
// so we can perform SPA navigation instead of a full page reload.
|
|
322
|
-
// Check handle.signal.aborted to avoid redirecting from a stale action
|
|
323
|
-
// when the user has already navigated away.
|
|
324
|
-
if (metadata?.redirect && !handle.signal.aborted) {
|
|
325
|
-
const { url: redirectUrl } = metadata.redirect;
|
|
326
|
-
const redirectState = metadata.locationState;
|
|
327
|
-
console.log(`[Browser] Action redirect to ${redirectUrl}`);
|
|
328
|
-
handle.complete(returnValue?.data);
|
|
329
|
-
if (onNavigate) {
|
|
330
|
-
await onNavigate(redirectUrl, {
|
|
331
|
-
state: redirectState,
|
|
332
|
-
replace: true,
|
|
333
|
-
_skipCache: true,
|
|
396
|
+
// Render the full tree with error segment merged with parent layouts
|
|
397
|
+
const errorTree = await renderSegments(errorResult.mainSegments, {
|
|
398
|
+
isAction: true,
|
|
399
|
+
interceptSegments:
|
|
400
|
+
errorResult.interceptSegments.length > 0
|
|
401
|
+
? errorResult.interceptSegments
|
|
402
|
+
: undefined,
|
|
334
403
|
});
|
|
335
|
-
} else {
|
|
336
|
-
window.location.href = redirectUrl;
|
|
337
|
-
}
|
|
338
|
-
return returnValue?.data;
|
|
339
|
-
}
|
|
340
404
|
|
|
341
|
-
|
|
405
|
+
// Re-check route stability after async renderSegments — user may have
|
|
406
|
+
// navigated away while the error tree was being prepared.
|
|
407
|
+
if (window.location.pathname !== actionStartPathname) {
|
|
408
|
+
log("user navigated during error render, skipping");
|
|
409
|
+
if (returnValue && !returnValue.ok) {
|
|
410
|
+
throw returnValue.data;
|
|
411
|
+
}
|
|
412
|
+
handle.complete(undefined);
|
|
413
|
+
return undefined;
|
|
414
|
+
}
|
|
415
|
+
const currentKeyNow = store.getHistoryKey();
|
|
416
|
+
if (currentKeyNow !== currentKey) {
|
|
417
|
+
log("history key changed during error render, skipping cache update");
|
|
418
|
+
if (returnValue && !returnValue.ok) {
|
|
419
|
+
throw returnValue.data;
|
|
420
|
+
}
|
|
421
|
+
handle.complete(undefined);
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
342
424
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
425
|
+
// Update UI with error boundary
|
|
426
|
+
startTransition(() => {
|
|
427
|
+
onUpdate({ root: errorTree, metadata: metadata! });
|
|
428
|
+
});
|
|
347
429
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
430
|
+
// Update segment tracking to exclude error segment IDs
|
|
431
|
+
const errorSegmentIds = new Set(diff);
|
|
432
|
+
const segmentIdsAfterError = segmentState.currentSegmentIds.filter(
|
|
433
|
+
(id) => !errorSegmentIds.has(id),
|
|
434
|
+
);
|
|
351
435
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
436
|
+
// Update store state
|
|
437
|
+
store.setSegmentIds(segmentIdsAfterError);
|
|
438
|
+
const currentHandleData = eventController.getHandleState().data;
|
|
439
|
+
store.cacheSegmentsForHistory(
|
|
440
|
+
currentKey,
|
|
441
|
+
errorResult.segments,
|
|
442
|
+
currentHandleData,
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
// Throw the error so the action promise rejects
|
|
446
|
+
if (returnValue && !returnValue.ok) {
|
|
447
|
+
throw returnValue.data;
|
|
448
|
+
}
|
|
355
449
|
|
|
356
|
-
|
|
357
|
-
|
|
450
|
+
// No error in returnValue (shouldn't happen with isError: true)
|
|
451
|
+
handle.complete(undefined);
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
358
454
|
|
|
359
|
-
|
|
455
|
+
if (!isPartial) {
|
|
456
|
+
// Protocol invariant: action revalidation responses MUST be partial.
|
|
457
|
+
// The server always sends isPartial: true for successful revalidation
|
|
458
|
+
// and isPartial: true + isError: true for error boundary responses.
|
|
459
|
+
// A non-partial payload here indicates a server-side bug.
|
|
460
|
+
throw new Error(
|
|
461
|
+
`[Browser] Action response missing isPartial — the server must ` +
|
|
462
|
+
`always send partial payloads for action revalidation.`,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
log("processing partial update", {
|
|
467
|
+
serverSegments: segments?.length ?? 0,
|
|
468
|
+
diff: diff?.join(", ") ?? "",
|
|
469
|
+
matched: matched?.join(", ") ?? "",
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
// Record revalidated segments for concurrent action tracking
|
|
473
|
+
if (diff) {
|
|
474
|
+
handle.recordRevalidatedSegments(diff);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Get current page's cached segments for merging
|
|
360
478
|
const currentKey = store.getHistoryKey();
|
|
361
479
|
const cached = store.getCachedSegments(currentKey);
|
|
362
480
|
const cachedSegments = cached?.segments || [];
|
|
363
481
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
// Render the full tree with error segment merged with parent layouts
|
|
368
|
-
const errorTree = await renderSegments(errorResult.mainSegments, {
|
|
369
|
-
isAction: true,
|
|
370
|
-
interceptSegments:
|
|
371
|
-
errorResult.interceptSegments.length > 0
|
|
372
|
-
? errorResult.interceptSegments
|
|
373
|
-
: undefined,
|
|
374
|
-
});
|
|
482
|
+
if (!matched) {
|
|
483
|
+
throw new Error("No matched segments in response");
|
|
484
|
+
}
|
|
375
485
|
|
|
376
|
-
//
|
|
377
|
-
|
|
378
|
-
|
|
486
|
+
// Reconcile server segments with cached segments (single source of truth)
|
|
487
|
+
const reconciled = reconcileSegments({
|
|
488
|
+
actor: "action",
|
|
489
|
+
matched,
|
|
490
|
+
diff: diff || [],
|
|
491
|
+
serverSegments: segments || [],
|
|
492
|
+
cachedSegments,
|
|
379
493
|
});
|
|
494
|
+
const fullSegments = reconciled.segments;
|
|
380
495
|
|
|
381
|
-
|
|
382
|
-
const errorSegmentIds = new Set(diff);
|
|
383
|
-
const segmentIdsAfterError = segmentState.currentSegmentIds.filter(
|
|
384
|
-
(id) => !errorSegmentIds.has(id),
|
|
385
|
-
);
|
|
386
|
-
|
|
387
|
-
// Update store state
|
|
388
|
-
store.setSegmentIds(segmentIdsAfterError);
|
|
389
|
-
const currentHandleData = eventController.getHandleState().data;
|
|
390
|
-
store.cacheSegmentsForHistory(
|
|
391
|
-
currentKey,
|
|
392
|
-
errorResult.segments,
|
|
393
|
-
currentHandleData,
|
|
394
|
-
);
|
|
496
|
+
const returnData = returnValue?.data;
|
|
395
497
|
|
|
396
|
-
// Throw the error so the action promise rejects
|
|
397
498
|
if (returnValue && !returnValue.ok) {
|
|
398
499
|
handle.fail(returnValue.data);
|
|
399
500
|
throw returnValue.data;
|
|
400
501
|
}
|
|
401
502
|
|
|
402
|
-
//
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
503
|
+
// Classify the post-reconciliation scenario
|
|
504
|
+
const scenario = classifyActionOutcome({
|
|
505
|
+
handleId: handle.id,
|
|
506
|
+
inflightActions: eventController.getInflightActions(),
|
|
507
|
+
hadAnyConcurrentActions: eventController.hadAnyConcurrentActions(),
|
|
508
|
+
revalidatedSegments: handle.getRevalidatedSegments(),
|
|
509
|
+
actionStartPathname,
|
|
510
|
+
currentPathname: window.location.pathname,
|
|
511
|
+
actionStartLocationKey: locationKey,
|
|
512
|
+
currentLocationKey: window.history.state?.key,
|
|
513
|
+
reconciledSegmentCount: fullSegments.length,
|
|
514
|
+
matchedCount: matched.length,
|
|
515
|
+
currentInterceptSource: store.getInterceptSourceUrl(),
|
|
516
|
+
});
|
|
413
517
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
518
|
+
switch (scenario.type) {
|
|
519
|
+
case "navigated-away": {
|
|
520
|
+
log("user navigated away during action", {
|
|
521
|
+
from: actionStartPathname,
|
|
522
|
+
to: window.location.pathname,
|
|
523
|
+
historyKeyChanged: scenario.historyKeyChanged,
|
|
524
|
+
});
|
|
525
|
+
// Clear concurrent action tracking - don't consolidate for old route's segments
|
|
526
|
+
handle.clearConsolidation();
|
|
527
|
+
|
|
528
|
+
if (scenario.historyKeyChanged) {
|
|
529
|
+
if (!scenario.onInterceptRoute) {
|
|
530
|
+
store.markCacheAsStaleAndBroadcast();
|
|
531
|
+
refetchRoute().catch((error) => {
|
|
532
|
+
if (isBackgroundSuppressible(error)) return;
|
|
533
|
+
console.error(
|
|
534
|
+
"[Browser] Background revalidation failed:",
|
|
535
|
+
error,
|
|
536
|
+
);
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
419
541
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
542
|
+
// Same history key but different pathname - safe to refetch current route
|
|
543
|
+
store.markCacheAsStaleAndBroadcast();
|
|
544
|
+
await refetchRoute({
|
|
545
|
+
interceptSourceUrl: store.getInterceptSourceUrl(),
|
|
546
|
+
});
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
424
549
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
550
|
+
case "hmr-missing": {
|
|
551
|
+
console.warn(
|
|
552
|
+
`[Browser] Missing segments after action (HMR detected), refetching...`,
|
|
553
|
+
);
|
|
554
|
+
await refetchRoute({ interceptSourceUrl });
|
|
555
|
+
store.broadcastCacheInvalidation();
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
429
558
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
559
|
+
case "consolidation-needed": {
|
|
560
|
+
log("consolidation fetch needed", {
|
|
561
|
+
segmentIds: scenario.segmentIds,
|
|
562
|
+
});
|
|
563
|
+
// Calculate segments to send (exclude the ones we want fresh)
|
|
564
|
+
const currentSegmentIds = store.getSegmentState().currentSegmentIds;
|
|
565
|
+
const segmentsToSend = currentSegmentIds.filter(
|
|
566
|
+
(sid) => !scenario.segmentIds.includes(sid),
|
|
567
|
+
);
|
|
433
568
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
actor: "action",
|
|
437
|
-
matched,
|
|
438
|
-
diff: diff || [],
|
|
439
|
-
serverSegments: segments || [],
|
|
440
|
-
cachedSegments,
|
|
441
|
-
});
|
|
442
|
-
const fullSegments = reconciled.segments;
|
|
443
|
-
|
|
444
|
-
const returnData = returnValue?.data;
|
|
445
|
-
|
|
446
|
-
if (returnValue && !returnValue.ok) {
|
|
447
|
-
handle.fail(returnValue.data);
|
|
448
|
-
throw returnValue.data;
|
|
449
|
-
}
|
|
569
|
+
// Clear consolidation tracking before fetch
|
|
570
|
+
handle.clearConsolidation();
|
|
450
571
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
actionStartPathname,
|
|
459
|
-
currentPathname: window.location.pathname,
|
|
460
|
-
actionStartLocationKey: locationKey,
|
|
461
|
-
currentLocationKey: window.history.state?.key,
|
|
462
|
-
reconciledSegmentCount: fullSegments.length,
|
|
463
|
-
matchedCount: matched.length,
|
|
464
|
-
consolidationSegments: consolidationSegments || null,
|
|
465
|
-
otherFetchingActionCount: otherFetchingActions.length,
|
|
466
|
-
currentInterceptSource: store.getInterceptSourceUrl(),
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
switch (scenario.type) {
|
|
470
|
-
case "navigated-away": {
|
|
471
|
-
log("user navigated away during action", {
|
|
472
|
-
from: actionStartPathname,
|
|
473
|
-
to: window.location.pathname,
|
|
474
|
-
historyKeyChanged: scenario.historyKeyChanged,
|
|
475
|
-
});
|
|
476
|
-
// Clear concurrent action tracking - don't consolidate for old route's segments
|
|
477
|
-
handle.clearConsolidation();
|
|
572
|
+
await refetchRoute({
|
|
573
|
+
segments: segmentsToSend,
|
|
574
|
+
interceptSourceUrl,
|
|
575
|
+
});
|
|
576
|
+
store.broadcastCacheInvalidation();
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
478
579
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
580
|
+
case "concurrent-skip": {
|
|
581
|
+
log("skipping UI update, other actions fetching", {
|
|
582
|
+
otherCount: scenario.otherFetchingCount,
|
|
583
|
+
});
|
|
584
|
+
// Only update store if history key hasn't changed (user didn't navigate away)
|
|
585
|
+
const currentKeyNow = store.getHistoryKey();
|
|
586
|
+
if (currentKeyNow === currentKey) {
|
|
587
|
+
store.setSegmentIds(matched);
|
|
588
|
+
const currentHandleData = eventController.getHandleState().data;
|
|
589
|
+
store.cacheSegmentsForHistory(
|
|
590
|
+
currentKey,
|
|
591
|
+
fullSegments,
|
|
592
|
+
currentHandleData,
|
|
593
|
+
);
|
|
486
594
|
}
|
|
487
595
|
break;
|
|
488
596
|
}
|
|
489
597
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
598
|
+
case "normal": {
|
|
599
|
+
// Prepare new tree (await loader data resolution)
|
|
600
|
+
const newTree = await renderSegments(reconciled.mainSegments, {
|
|
601
|
+
isAction: true,
|
|
602
|
+
interceptSegments:
|
|
603
|
+
reconciled.interceptSegments.length > 0
|
|
604
|
+
? reconciled.interceptSegments
|
|
605
|
+
: undefined,
|
|
606
|
+
});
|
|
497
607
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
store.broadcastCacheInvalidation();
|
|
504
|
-
break;
|
|
505
|
-
}
|
|
608
|
+
// Re-check if user navigated away (could happen during async renderSegments)
|
|
609
|
+
if (window.location.pathname !== actionStartPathname) {
|
|
610
|
+
log("user navigated during render, skipping");
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
506
613
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
614
|
+
// Verify the store's current key still matches what we captured at action start
|
|
615
|
+
// If they differ, user navigated away and we should NOT cache under the old key
|
|
616
|
+
const currentKeyNow = store.getHistoryKey();
|
|
617
|
+
if (currentKeyNow !== currentKey) {
|
|
618
|
+
log("history key changed during action, skipping cache update");
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
514
621
|
|
|
515
|
-
|
|
516
|
-
|
|
622
|
+
startTransition(() => {
|
|
623
|
+
onUpdate({ root: newTree, metadata: metadata! });
|
|
624
|
+
});
|
|
517
625
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
break;
|
|
524
|
-
}
|
|
626
|
+
// Apply server-set location state to history.state (non-redirect flow)
|
|
627
|
+
const actionLocationState = metadata?.locationState;
|
|
628
|
+
if (actionLocationState) {
|
|
629
|
+
mergeLocationState(actionLocationState);
|
|
630
|
+
}
|
|
525
631
|
|
|
526
|
-
|
|
527
|
-
log("skipping UI update, other actions fetching", {
|
|
528
|
-
otherCount: scenario.otherFetchingCount,
|
|
529
|
-
});
|
|
530
|
-
// Only update store if history key hasn't changed (user didn't navigate away)
|
|
531
|
-
const currentKeyNow = store.getHistoryKey();
|
|
532
|
-
if (currentKeyNow === currentKey) {
|
|
632
|
+
// Update store state
|
|
533
633
|
store.setSegmentIds(matched);
|
|
534
634
|
const currentHandleData = eventController.getHandleState().data;
|
|
535
635
|
store.cacheSegmentsForHistory(
|
|
@@ -537,53 +637,16 @@ export function createServerActionBridge(
|
|
|
537
637
|
fullSegments,
|
|
538
638
|
currentHandleData,
|
|
539
639
|
);
|
|
540
|
-
|
|
541
|
-
break;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
case "normal": {
|
|
545
|
-
// Prepare new tree (await loader data resolution)
|
|
546
|
-
const newTree = await renderSegments(reconciled.mainSegments, {
|
|
547
|
-
isAction: true,
|
|
548
|
-
interceptSegments:
|
|
549
|
-
reconciled.interceptSegments.length > 0
|
|
550
|
-
? reconciled.interceptSegments
|
|
551
|
-
: undefined,
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
// Re-check if user navigated away (could happen during async renderSegments)
|
|
555
|
-
if (window.location.pathname !== actionStartPathname) {
|
|
556
|
-
log("user navigated during render, skipping");
|
|
640
|
+
store.markCacheAsStaleAndBroadcast();
|
|
557
641
|
break;
|
|
558
642
|
}
|
|
559
|
-
|
|
560
|
-
// Verify the store's current key still matches what we captured at action start
|
|
561
|
-
// If they differ, user navigated away and we should NOT cache under the old key
|
|
562
|
-
const currentKeyNow = store.getHistoryKey();
|
|
563
|
-
if (currentKeyNow !== currentKey) {
|
|
564
|
-
log("history key changed during action, skipping cache update");
|
|
565
|
-
break;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
startTransition(() => {
|
|
569
|
-
onUpdate({ root: newTree, metadata: metadata! });
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
// Update store state
|
|
573
|
-
store.setSegmentIds(matched);
|
|
574
|
-
const currentHandleData = eventController.getHandleState().data;
|
|
575
|
-
store.cacheSegmentsForHistory(
|
|
576
|
-
currentKey,
|
|
577
|
-
fullSegments,
|
|
578
|
-
currentHandleData,
|
|
579
|
-
);
|
|
580
|
-
store.markCacheAsStaleAndBroadcast();
|
|
581
|
-
break;
|
|
582
643
|
}
|
|
583
|
-
}
|
|
584
644
|
|
|
585
|
-
|
|
586
|
-
|
|
645
|
+
handle.complete(returnData);
|
|
646
|
+
return returnData;
|
|
647
|
+
} finally {
|
|
648
|
+
handle[Symbol.dispose]();
|
|
649
|
+
}
|
|
587
650
|
}
|
|
588
651
|
|
|
589
652
|
return {
|
|
@@ -598,16 +661,6 @@ export function createServerActionBridge(
|
|
|
598
661
|
deps.setServerCallback(handleServerAction);
|
|
599
662
|
isRegistered = true;
|
|
600
663
|
},
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* Unregister the server action callback
|
|
604
|
-
*/
|
|
605
|
-
unregister(): void {
|
|
606
|
-
if (!isRegistered) {
|
|
607
|
-
return;
|
|
608
|
-
}
|
|
609
|
-
isRegistered = false;
|
|
610
|
-
},
|
|
611
664
|
};
|
|
612
665
|
}
|
|
613
666
|
|