@rangojs/router 0.0.0-experimental.9c9afef3 → 0.0.0-experimental.a014d2b7
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/AGENTS.md +8 -0
- package/README.md +245 -49
- package/dist/bin/rango.js +440 -133
- package/dist/testing/vitest.js +82 -0
- package/dist/vite/index.js +3373 -1176
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +68 -14
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +64 -2
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +224 -32
- package/skills/caching/SKILL.md +279 -17
- package/skills/composability/SKILL.md +27 -3
- package/skills/css/SKILL.md +76 -0
- package/skills/debug-manifest/SKILL.md +4 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +250 -30
- package/skills/host-router/SKILL.md +83 -23
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +87 -18
- package/skills/layout/SKILL.md +35 -9
- package/skills/links/SKILL.md +249 -17
- package/skills/loader/SKILL.md +235 -9
- package/skills/middleware/SKILL.md +52 -13
- package/skills/migrate-nextjs/SKILL.md +584 -0
- package/skills/migrate-react-router/SKILL.md +771 -0
- package/skills/mime-routes/SKILL.md +28 -1
- package/skills/observability/SKILL.md +172 -0
- package/skills/parallel/SKILL.md +77 -7
- package/skills/prerender/SKILL.md +172 -125
- package/skills/rango/SKILL.md +251 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +123 -48
- package/skills/route/SKILL.md +70 -5
- package/skills/router-setup/SKILL.md +65 -8
- package/skills/scripts/SKILL.md +179 -0
- package/skills/server-actions/SKILL.md +775 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/tailwind/SKILL.md +27 -3
- package/skills/testing/SKILL.md +130 -0
- package/skills/testing/bindings.md +103 -0
- package/skills/testing/cache-prerender.md +127 -0
- package/skills/testing/client-components.md +124 -0
- package/skills/testing/e2e-parity.md +125 -0
- package/skills/testing/flight.md +91 -0
- package/skills/testing/handles.md +129 -0
- package/skills/testing/loader.md +128 -0
- package/skills/testing/middleware.md +99 -0
- package/skills/testing/render-handler.md +122 -0
- package/skills/testing/response-routes.md +95 -0
- package/skills/testing/reverse-and-types.md +84 -0
- package/skills/testing/server-actions.md +107 -0
- package/skills/testing/server-tree.md +128 -0
- package/skills/testing/setup.md +123 -0
- package/skills/typesafety/SKILL.md +322 -29
- package/skills/use-cache/SKILL.md +57 -14
- package/skills/view-transitions/SKILL.md +337 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/__internal.ts +1 -66
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/action-fence.ts +47 -0
- package/src/browser/app-shell.ts +39 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/connection-warmup.ts +134 -0
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/event-controller.ts +192 -150
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/invalidate-client-cache.ts +52 -0
- package/src/browser/navigation-bridge.ts +131 -30
- package/src/browser/navigation-client.ts +186 -100
- package/src/browser/navigation-store-handle.ts +38 -0
- package/src/browser/navigation-store.ts +157 -74
- package/src/browser/navigation-transaction.ts +9 -59
- package/src/browser/network-error-handler.ts +34 -7
- package/src/browser/partial-update.ts +165 -112
- package/src/browser/prefetch/cache.ts +205 -62
- package/src/browser/prefetch/fetch.ts +347 -39
- package/src/browser/prefetch/queue.ts +42 -8
- package/src/browser/rango-state.ts +158 -76
- package/src/browser/react/Link.tsx +102 -15
- package/src/browser/react/NavigationProvider.tsx +295 -119
- package/src/browser/react/ScrollRestoration.tsx +10 -6
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/deferred-handle-resolution.ts +75 -0
- package/src/browser/react/filter-segment-order.ts +66 -7
- package/src/browser/react/index.ts +0 -48
- package/src/browser/react/location-state-shared.ts +178 -8
- package/src/browser/react/location-state.ts +39 -14
- package/src/browser/react/use-action.ts +6 -15
- package/src/browser/react/use-handle.ts +23 -69
- package/src/browser/react/use-href.tsx +8 -1
- package/src/browser/react/use-link-status.ts +33 -8
- package/src/browser/react/use-navigation.ts +32 -7
- package/src/browser/react/use-params.ts +20 -10
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +46 -11
- package/src/browser/react/use-search-params.ts +0 -5
- package/src/browser/react/use-segments.ts +11 -21
- package/src/browser/response-adapter.ts +99 -8
- package/src/browser/rsc-router.tsx +114 -24
- package/src/browser/scroll-restoration.ts +37 -22
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +222 -72
- package/src/browser/types.ts +102 -12
- package/src/browser/validate-redirect-origin.ts +43 -16
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +65 -40
- package/src/build/generate-route-types.ts +5 -1
- package/src/build/index.ts +8 -2
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +165 -36
- package/src/build/route-types/ast-route-extraction.ts +15 -8
- package/src/build/route-types/codegen.ts +16 -5
- package/src/build/route-types/include-resolution.ts +125 -24
- package/src/build/route-types/param-extraction.ts +6 -3
- package/src/build/route-types/per-module-writer.ts +22 -6
- package/src/build/route-types/router-processing.ts +260 -94
- package/src/build/route-types/scan-filter.ts +9 -2
- package/src/build/route-types/source-scan.ts +216 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-key-utils.ts +29 -13
- package/src/cache/cache-policy.ts +108 -34
- package/src/cache/cache-runtime.ts +224 -41
- package/src/cache/cache-scope.ts +188 -82
- package/src/cache/cache-tag.ts +103 -0
- package/src/cache/cf/cf-base64.ts +33 -0
- package/src/cache/cf/cf-cache-constants.ts +127 -0
- package/src/cache/cf/cf-cache-store.ts +1989 -378
- package/src/cache/cf/cf-cache-types.ts +349 -0
- package/src/cache/cf/cf-kv-utils.ts +46 -0
- package/src/cache/cf/cf-tag-marker-memo.ts +105 -0
- package/src/cache/cf/index.ts +6 -16
- package/src/cache/document-cache.ts +89 -21
- package/src/cache/handle-snapshot.ts +70 -0
- package/src/cache/index.ts +10 -20
- package/src/cache/memory-segment-store.ts +136 -37
- package/src/cache/profile-registry.ts +46 -31
- package/src/cache/read-through-swr.ts +56 -12
- package/src/cache/segment-codec.ts +9 -17
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/types.ts +37 -100
- package/src/client.rsc.tsx +44 -21
- package/src/client.tsx +119 -290
- package/src/cloudflare/index.ts +11 -0
- package/src/cloudflare/tracing.ts +109 -0
- package/src/component-utils.ts +19 -0
- package/src/components/DefaultDocument.tsx +8 -2
- package/src/context-var.ts +18 -6
- package/src/decode-loader-results.ts +52 -0
- package/src/defer.ts +196 -0
- package/src/deps/ssr.ts +0 -1
- package/src/encode-kv.ts +49 -0
- package/src/errors.ts +30 -4
- package/src/escape-script.ts +52 -0
- package/src/handle.ts +70 -22
- package/src/handles/MetaTags.tsx +62 -19
- package/src/handles/Scripts.tsx +183 -0
- package/src/handles/breadcrumbs.ts +37 -8
- package/src/handles/is-thenable.ts +19 -0
- package/src/handles/meta.ts +51 -40
- package/src/handles/script.ts +244 -0
- package/src/host/cookie-handler.ts +9 -60
- package/src/host/errors.ts +0 -24
- package/src/host/index.ts +8 -2
- package/src/host/pattern-matcher.ts +23 -52
- package/src/host/router.ts +107 -99
- package/src/host/testing.ts +40 -27
- package/src/host/types.ts +37 -4
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +137 -22
- package/src/index.rsc.ts +99 -13
- package/src/index.ts +139 -19
- package/src/internal-debug.ts +11 -10
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +20 -13
- package/src/loader.ts +12 -11
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/outlet-provider.tsx +1 -5
- package/src/prerender/param-hash.ts +16 -16
- package/src/prerender/store.ts +37 -41
- package/src/prerender.ts +198 -82
- package/src/redirect-origin.ts +100 -0
- package/src/regex-escape.ts +8 -0
- package/src/render-error-thrower.tsx +20 -0
- package/src/response-utils.ts +62 -0
- package/src/reverse.ts +65 -15
- package/src/root-error-boundary.tsx +1 -19
- package/src/route-content-wrapper.tsx +19 -77
- package/src/route-definition/dsl-helpers.ts +461 -304
- package/src/route-definition/helper-factories.ts +28 -140
- package/src/route-definition/helpers-types.ts +143 -69
- package/src/route-definition/index.ts +4 -2
- package/src/route-definition/redirect.ts +51 -10
- package/src/route-definition/resolve-handler-use.ts +160 -0
- package/src/route-definition/use-item-types.ts +29 -0
- package/src/route-map-builder.ts +0 -16
- package/src/route-types.ts +37 -46
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +164 -17
- package/src/router/error-handling.ts +45 -18
- package/src/router/find-match.ts +44 -23
- package/src/router/handler-context.ts +52 -31
- package/src/router/instrument.ts +350 -0
- package/src/router/intercept-resolution.ts +48 -24
- package/src/router/lazy-includes.ts +15 -52
- package/src/router/loader-resolution.ts +268 -56
- package/src/router/logging.ts +0 -6
- package/src/router/manifest.ts +40 -42
- package/src/router/match-api.ts +124 -204
- package/src/router/match-context.ts +0 -22
- package/src/router/match-handlers.ts +58 -58
- package/src/router/match-middleware/background-revalidation.ts +40 -24
- package/src/router/match-middleware/cache-lookup.ts +170 -276
- package/src/router/match-middleware/cache-store.ts +64 -52
- package/src/router/match-middleware/intercept-resolution.ts +0 -22
- package/src/router/match-middleware/segment-resolution.ts +45 -14
- package/src/router/match-pipelines.ts +1 -42
- package/src/router/match-result.ts +87 -39
- package/src/router/metrics.ts +0 -34
- package/src/router/middleware-types.ts +7 -140
- package/src/router/middleware.ts +266 -169
- package/src/router/navigation-snapshot.ts +131 -0
- package/src/router/params-util.ts +23 -0
- package/src/router/pattern-matching.ts +132 -90
- package/src/router/prefetch-cache-ttl.ts +51 -0
- package/src/router/prerender-match.ts +195 -56
- package/src/router/preview-match.ts +32 -102
- package/src/router/request-classification.ts +276 -0
- package/src/router/revalidation.ts +123 -73
- package/src/router/route-snapshot.ts +244 -0
- package/src/router/router-context.ts +3 -28
- package/src/router/router-interfaces.ts +115 -35
- package/src/router/router-options.ts +172 -15
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +162 -84
- package/src/router/segment-resolution/helpers.ts +86 -6
- package/src/router/segment-resolution/loader-cache.ts +76 -39
- package/src/router/segment-resolution/revalidation.ts +351 -321
- package/src/router/segment-resolution/static-store.ts +19 -5
- package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
- package/src/router/segment-resolution/view-transition-default.ts +56 -0
- package/src/router/segment-resolution.ts +5 -1
- package/src/router/segment-wrappers.ts +6 -5
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry-otel.ts +161 -199
- package/src/router/telemetry.ts +96 -19
- package/src/router/timeout.ts +0 -20
- package/src/router/tracing.ts +206 -0
- package/src/router/trie-matching.ts +163 -59
- package/src/router/types.ts +9 -63
- package/src/router/url-params.ts +44 -0
- package/src/router.ts +157 -54
- package/src/rsc/handler-context.ts +3 -2
- package/src/rsc/handler.ts +655 -529
- package/src/rsc/helpers.ts +168 -46
- package/src/rsc/index.ts +2 -5
- package/src/rsc/json-route-result.ts +38 -0
- package/src/rsc/loader-fetch.ts +122 -31
- package/src/rsc/manifest-init.ts +33 -42
- package/src/rsc/origin-guard.ts +39 -25
- package/src/rsc/progressive-enhancement.ts +131 -14
- package/src/rsc/redirect-guard.ts +99 -0
- package/src/rsc/response-cache-serve.ts +238 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +99 -189
- package/src/rsc/rsc-rendering.ts +109 -74
- package/src/rsc/runtime-warnings.ts +23 -10
- package/src/rsc/server-action.ts +287 -115
- package/src/rsc/ssr-setup.ts +18 -2
- package/src/rsc/transition-gate.ts +89 -0
- package/src/rsc/types.ts +29 -9
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +35 -30
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +149 -0
- package/src/segment-system.tsx +236 -202
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +224 -52
- package/src/server/cookie-parse.ts +32 -0
- package/src/server/cookie-store.ts +80 -5
- package/src/server/handle-store.ts +40 -38
- package/src/server/loader-registry.ts +38 -46
- package/src/server/request-context.ts +401 -173
- package/src/ssr/index.tsx +24 -16
- package/src/static-handler.ts +27 -18
- package/src/testing/cache-status.ts +162 -0
- package/src/testing/collect-handle.ts +40 -0
- package/src/testing/dispatch.ts +701 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +188 -0
- package/src/testing/e2e/index.ts +128 -0
- package/src/testing/e2e/matchers.ts +35 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +387 -0
- package/src/testing/e2e/server.ts +195 -0
- package/src/testing/flight-matchers.ts +97 -0
- package/src/testing/flight-normalize.ts +11 -0
- package/src/testing/flight-runtime.d.ts +57 -0
- package/src/testing/flight-tree.ts +682 -0
- package/src/testing/flight.entry.ts +52 -0
- package/src/testing/flight.ts +257 -0
- package/src/testing/generated-routes.ts +183 -0
- package/src/testing/index.ts +105 -0
- package/src/testing/internal/context.ts +371 -0
- package/src/testing/internal/flight-client-globals.ts +30 -0
- package/src/testing/internal/seed-vars.ts +54 -0
- package/src/testing/render-handler.ts +357 -0
- package/src/testing/render-route.tsx +581 -0
- package/src/testing/run-loader.ts +385 -0
- package/src/testing/run-middleware.ts +205 -0
- package/src/testing/run-transition-when.ts +164 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
- package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
- package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
- package/src/testing/vitest-stubs/version.ts +5 -0
- package/src/testing/vitest.ts +305 -0
- package/src/theme/ThemeProvider.tsx +20 -58
- package/src/theme/ThemeScript.tsx +7 -9
- package/src/theme/constants.ts +52 -13
- package/src/theme/index.ts +0 -7
- package/src/theme/theme-context.ts +1 -5
- package/src/theme/theme-script.ts +22 -21
- package/src/theme/use-theme.ts +0 -3
- package/src/types/boundaries.ts +0 -35
- package/src/types/cache-types.ts +17 -8
- package/src/types/error-types.ts +30 -90
- package/src/types/global-namespace.ts +54 -41
- package/src/types/handler-context.ts +125 -71
- package/src/types/index.ts +3 -10
- package/src/types/loader-types.ts +40 -11
- package/src/types/request-scope.ts +112 -0
- package/src/types/route-config.ts +6 -50
- package/src/types/route-entry.ts +12 -7
- package/src/types/segments.ts +136 -15
- package/src/urls/include-helper.ts +33 -70
- package/src/urls/index.ts +1 -11
- package/src/urls/path-helper-types.ts +68 -18
- package/src/urls/path-helper.ts +57 -111
- package/src/urls/pattern-types.ts +48 -19
- package/src/urls/response-types.ts +25 -22
- package/src/urls/type-extraction.ts +58 -139
- package/src/urls/urls-function.ts +1 -19
- package/src/use-loader.tsx +346 -89
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +36 -38
- package/src/vite/discovery/discover-routers.ts +130 -85
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +214 -132
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +57 -4
- package/src/vite/discovery/virtual-module-codegen.ts +14 -34
- package/src/vite/index.ts +6 -0
- package/src/vite/inject-client-debug.ts +36 -0
- package/src/vite/plugin-types.ts +178 -5
- package/src/vite/plugins/cjs-to-esm.ts +16 -19
- package/src/vite/plugins/client-ref-dedup.ts +16 -11
- package/src/vite/plugins/client-ref-hashing.ts +28 -15
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +194 -0
- package/src/vite/plugins/expose-action-id.ts +48 -95
- package/src/vite/plugins/expose-id-utils.ts +96 -51
- package/src/vite/plugins/expose-ids/export-analysis.ts +101 -34
- package/src/vite/plugins/expose-ids/handler-transform.ts +15 -64
- package/src/vite/plugins/expose-ids/loader-transform.ts +14 -24
- package/src/vite/plugins/expose-ids/router-transform.ts +118 -29
- package/src/vite/plugins/expose-internal-ids.ts +553 -317
- package/src/vite/plugins/performance-tracks.ts +64 -170
- package/src/vite/plugins/refresh-cmd.ts +89 -27
- package/src/vite/plugins/use-cache-transform.ts +73 -83
- package/src/vite/plugins/version-injector.ts +40 -29
- package/src/vite/plugins/version-plugin.ts +37 -40
- package/src/vite/plugins/virtual-entries.ts +39 -25
- package/src/vite/rango.ts +118 -114
- package/src/vite/router-discovery.ts +941 -142
- package/src/vite/utils/ast-handler-extract.ts +26 -35
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/bundle-analysis.ts +10 -15
- package/src/vite/utils/client-chunks.ts +184 -0
- package/src/vite/utils/directive-prologue.ts +40 -0
- package/src/vite/utils/forward-user-plugins.ts +171 -0
- package/src/vite/utils/manifest-utils.ts +4 -59
- package/src/vite/utils/package-resolution.ts +20 -52
- package/src/vite/utils/prerender-utils.ts +81 -34
- package/src/vite/utils/shared-utils.ts +92 -42
- package/src/browser/action-response-classifier.ts +0 -99
- package/src/browser/debug-channel.ts +0 -93
- package/src/browser/react/use-client-cache.ts +0 -58
- package/src/browser/shallow.ts +0 -40
- package/src/handles/index.ts +0 -7
- package/src/network-error-thrower.tsx +0 -23
- package/src/router/middleware-cookies.ts +0 -55
package/src/rsc/handler.ts
CHANGED
|
@@ -8,21 +8,16 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { createElement } from "react";
|
|
11
|
-
import {
|
|
11
|
+
import { isRouteNotFoundError } from "../errors.js";
|
|
12
12
|
import { matchMiddleware, executeMiddleware } from "../router/middleware.js";
|
|
13
13
|
import {
|
|
14
14
|
runWithRequestContext,
|
|
15
15
|
setRequestContextParams,
|
|
16
|
-
requireRequestContext,
|
|
17
16
|
getRequestContext,
|
|
17
|
+
_getRequestContext,
|
|
18
18
|
createRequestContext,
|
|
19
19
|
} from "../server/request-context.js";
|
|
20
20
|
import * as rscDeps from "@vitejs/plugin-rsc/rsc";
|
|
21
|
-
import {
|
|
22
|
-
DEBUG_ID_HEADER,
|
|
23
|
-
createServerDebugChannel,
|
|
24
|
-
} from "../vite/plugins/performance-tracks.js";
|
|
25
|
-
|
|
26
21
|
import type {
|
|
27
22
|
RscPayload,
|
|
28
23
|
CreateRSCHandlerOptions,
|
|
@@ -35,6 +30,11 @@ import {
|
|
|
35
30
|
interceptRedirectForPartial,
|
|
36
31
|
buildRouteMiddlewareEntries,
|
|
37
32
|
} from "./helpers.js";
|
|
33
|
+
import { guardOutgoingRedirect } from "./redirect-guard.js";
|
|
34
|
+
import {
|
|
35
|
+
isWebSocketUpgradeResponse,
|
|
36
|
+
appendVaryAccept,
|
|
37
|
+
} from "../response-utils.js";
|
|
38
38
|
import {
|
|
39
39
|
handleResponseRoute,
|
|
40
40
|
type ResponseRouteMatch,
|
|
@@ -48,8 +48,6 @@ import {
|
|
|
48
48
|
createReverseFunction,
|
|
49
49
|
stripInternalParams,
|
|
50
50
|
} from "../router/handler-context.js";
|
|
51
|
-
import { getRouterContext } from "../router/router-context.js";
|
|
52
|
-
import { resolveSink, safeEmit } from "../router/telemetry.js";
|
|
53
51
|
import { contextSet } from "../context-var.js";
|
|
54
52
|
import {
|
|
55
53
|
hasCachedManifest,
|
|
@@ -60,6 +58,8 @@ import {
|
|
|
60
58
|
getRouterTrie,
|
|
61
59
|
} from "../route-map-builder.js";
|
|
62
60
|
import type { HandlerContext } from "./handler-context.js";
|
|
61
|
+
import type { CacheErrorCategory } from "../cache/cache-error.js";
|
|
62
|
+
import type { SegmentCacheStore } from "../cache/types.js";
|
|
63
63
|
import { buildRouterTrieFromUrlpatterns } from "./manifest-init.js";
|
|
64
64
|
import { handleProgressiveEnhancement } from "./progressive-enhancement.js";
|
|
65
65
|
import {
|
|
@@ -68,7 +68,10 @@ import {
|
|
|
68
68
|
type ActionContinuation,
|
|
69
69
|
} from "./server-action.js";
|
|
70
70
|
import { handleLoaderFetch } from "./loader-fetch.js";
|
|
71
|
-
import {
|
|
71
|
+
import {
|
|
72
|
+
checkRequestOrigin,
|
|
73
|
+
ORIGIN_CHECK_PHASE_BY_MODE,
|
|
74
|
+
} from "./origin-guard.js";
|
|
72
75
|
import { handleRscRendering } from "./rsc-rendering.js";
|
|
73
76
|
import {
|
|
74
77
|
withTimeout,
|
|
@@ -81,12 +84,19 @@ import {
|
|
|
81
84
|
appendMetric,
|
|
82
85
|
buildMetricsTiming,
|
|
83
86
|
} from "../router/metrics.js";
|
|
87
|
+
import { observePhase, observeEvent, PHASES } from "../router/instrument.js";
|
|
84
88
|
import {
|
|
85
89
|
startSSRSetup,
|
|
86
90
|
getSSRSetup,
|
|
87
91
|
mayNeedSSR,
|
|
92
|
+
isRscRequest,
|
|
88
93
|
SSR_SETUP_VAR,
|
|
89
94
|
} from "./ssr-setup.js";
|
|
95
|
+
import {
|
|
96
|
+
classifyRequest,
|
|
97
|
+
type RequestPlan,
|
|
98
|
+
type ExecutableRequestPlan,
|
|
99
|
+
} from "../router/request-classification.js";
|
|
90
100
|
|
|
91
101
|
/**
|
|
92
102
|
* Create an RSC request handler.
|
|
@@ -121,12 +131,35 @@ import {
|
|
|
121
131
|
* });
|
|
122
132
|
* ```
|
|
123
133
|
*/
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Response that tells the client to do a full document navigation. Shared by
|
|
137
|
+
* the terminal reload plans (version-mismatch and app-switch): an empty 200
|
|
138
|
+
* carrying X-RSC-Reload, which the client turns into window.location.href.
|
|
139
|
+
*/
|
|
140
|
+
function createReloadResponse(reloadUrl: string) {
|
|
141
|
+
return createResponseWithMergedHeaders(null, {
|
|
142
|
+
status: 200,
|
|
143
|
+
headers: {
|
|
144
|
+
"X-RSC-Reload": reloadUrl,
|
|
145
|
+
"content-type": "text/x-component;charset=utf-8",
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
124
150
|
export function createRSCHandler<
|
|
125
151
|
TEnv = unknown,
|
|
126
152
|
TRoutes extends Record<string, string> = Record<string, string>,
|
|
127
153
|
>(options: CreateRSCHandlerOptions<TEnv, TRoutes>) {
|
|
128
154
|
const { router, version = VERSION, nonce: nonceProvider } = options;
|
|
129
155
|
|
|
156
|
+
// Handler-owned registry of explicit per-scope stores from cache({ store }).
|
|
157
|
+
// Lives in the closure so it is scoped per handler (multi-router deployments
|
|
158
|
+
// get separate registries) and accumulates every explicit store this handler
|
|
159
|
+
// resolves across requests. updateTag()/revalidateTag() iterate it to reach
|
|
160
|
+
// stores not covered by the app-level ctx._cacheStore.
|
|
161
|
+
const explicitTaggedStores = new Set<SegmentCacheStore>();
|
|
162
|
+
|
|
130
163
|
// Use provided deps or default to @vitejs/plugin-rsc/rsc exports
|
|
131
164
|
const deps = options.deps ?? rscDeps;
|
|
132
165
|
const {
|
|
@@ -166,10 +199,13 @@ export function createRSCHandler<
|
|
|
166
199
|
phase: ErrorPhase,
|
|
167
200
|
context: Parameters<typeof invokeOnError<TEnv>>[3],
|
|
168
201
|
): void {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
202
|
+
// Guard: abort signal handlers fire asynchronously outside the ALS
|
|
203
|
+
// request scope, so the context may be gone. Skip dedup in that
|
|
204
|
+
// case — the error is from a cancelled stream, not a real failure.
|
|
205
|
+
const reqCtx = _getRequestContext();
|
|
206
|
+
if (error != null && typeof error === "object" && reqCtx) {
|
|
207
|
+
if (reqCtx._reportedErrors.has(error)) return;
|
|
208
|
+
reqCtx._reportedErrors.add(error);
|
|
173
209
|
}
|
|
174
210
|
invokeOnError(router.onError, error, phase, context, "RSC");
|
|
175
211
|
}
|
|
@@ -209,24 +245,16 @@ export function createRSCHandler<
|
|
|
209
245
|
metadata: { timeout: true, phase, durationMs },
|
|
210
246
|
});
|
|
211
247
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
actionId,
|
|
223
|
-
durationMs,
|
|
224
|
-
customHandler: !!router.onTimeout,
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
} catch {
|
|
228
|
-
// Router context may not be available
|
|
229
|
-
}
|
|
248
|
+
observeEvent({
|
|
249
|
+
type: "request.timeout",
|
|
250
|
+
timestamp: performance.now(),
|
|
251
|
+
phase,
|
|
252
|
+
pathname: url.pathname,
|
|
253
|
+
routeKey,
|
|
254
|
+
actionId,
|
|
255
|
+
durationMs,
|
|
256
|
+
customHandler: !!router.onTimeout,
|
|
257
|
+
});
|
|
230
258
|
|
|
231
259
|
if (router.onTimeout) {
|
|
232
260
|
try {
|
|
@@ -258,18 +286,26 @@ export function createRSCHandler<
|
|
|
258
286
|
function createRedirectFlightResponse(
|
|
259
287
|
redirectUrl: string,
|
|
260
288
|
locationState?: Record<string, unknown>,
|
|
289
|
+
external?: boolean,
|
|
261
290
|
): Response {
|
|
262
291
|
const redirectPayload: RscPayload = {
|
|
263
292
|
metadata: {
|
|
264
293
|
pathname: redirectUrl,
|
|
265
294
|
segments: [],
|
|
266
|
-
redirect: { url: redirectUrl },
|
|
295
|
+
redirect: { url: redirectUrl, ...(external && { external: true }) },
|
|
267
296
|
...(locationState && { locationState }),
|
|
268
297
|
},
|
|
269
298
|
};
|
|
270
|
-
const debugChannel = getRequestContext()?._debugChannel;
|
|
271
299
|
const rscStream = renderToReadableStream<RscPayload>(redirectPayload, {
|
|
272
|
-
|
|
300
|
+
onError: (error: unknown) => {
|
|
301
|
+
const reqCtx = _getRequestContext<TEnv>();
|
|
302
|
+
if (!reqCtx) return;
|
|
303
|
+
callOnError(error, "rendering", {
|
|
304
|
+
request: reqCtx.request,
|
|
305
|
+
url: reqCtx.url,
|
|
306
|
+
env: reqCtx.env,
|
|
307
|
+
});
|
|
308
|
+
},
|
|
273
309
|
});
|
|
274
310
|
return createResponseWithMergedHeaders(rscStream, {
|
|
275
311
|
status: 200,
|
|
@@ -351,7 +387,7 @@ export function createRSCHandler<
|
|
|
351
387
|
// Resolve cache store configuration
|
|
352
388
|
// Priority: options.cache (handler override) > router.cache (router default)
|
|
353
389
|
// Store is enabled only if: config provided, enabled, and no ?__no_cache query param
|
|
354
|
-
let cacheStore
|
|
390
|
+
let cacheStore: SegmentCacheStore | undefined;
|
|
355
391
|
const cacheOption = options.cache ?? router.cache;
|
|
356
392
|
if (cacheOption && !url.searchParams.has("__no_cache")) {
|
|
357
393
|
const cacheConfig =
|
|
@@ -418,29 +454,22 @@ export function createRSCHandler<
|
|
|
418
454
|
url,
|
|
419
455
|
variables,
|
|
420
456
|
cacheStore,
|
|
457
|
+
explicitTaggedStores,
|
|
421
458
|
cacheProfiles: router.cacheProfiles,
|
|
422
459
|
executionContext: executionCtx,
|
|
423
460
|
themeConfig: router.themeConfig,
|
|
461
|
+
stateCookieName: router.resolvedStateCookieName,
|
|
462
|
+
version,
|
|
424
463
|
});
|
|
425
464
|
if (earlyMetricsStore) {
|
|
426
465
|
requestContext._debugPerformance = true;
|
|
427
466
|
requestContext._metricsStore = earlyMetricsStore;
|
|
428
467
|
}
|
|
429
|
-
// Dev-only: wire debug channel for React Performance Tracks
|
|
430
|
-
if (process.env.NODE_ENV !== "production") {
|
|
431
|
-
const debugId = request.headers.get(DEBUG_ID_HEADER);
|
|
432
|
-
if (debugId) {
|
|
433
|
-
const channel = createServerDebugChannel(debugId);
|
|
434
|
-
if (channel) {
|
|
435
|
-
requestContext._debugChannel = channel;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
468
|
// Wire background error reporting so "use cache" and other subsystems
|
|
440
469
|
// can surface non-fatal errors through the router's onError callback.
|
|
441
470
|
requestContext._reportBackgroundError = (
|
|
442
471
|
error: unknown,
|
|
443
|
-
category:
|
|
472
|
+
category: CacheErrorCategory,
|
|
444
473
|
) => {
|
|
445
474
|
callOnError(error, "cache", {
|
|
446
475
|
request,
|
|
@@ -470,82 +499,112 @@ export function createRSCHandler<
|
|
|
470
499
|
// - Server components during rendering
|
|
471
500
|
// - Error boundaries
|
|
472
501
|
// - Streaming
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
)
|
|
502
|
+
// Store basename on request context (scoped per-request via existing ALS)
|
|
503
|
+
requestContext._basename = router.basename;
|
|
504
|
+
|
|
505
|
+
// Resolved span tracing for this request (read at each traced phase).
|
|
506
|
+
requestContext._tracing = router.tracing;
|
|
507
|
+
|
|
508
|
+
// The "rango.request" span is opened inside the request context so the
|
|
509
|
+
// Cloudflare runner can read executionContext.tracing, and so every nested
|
|
510
|
+
// phase span (and the platform's automatic KV/D1/fetch spans) nests under
|
|
511
|
+
// it. Construction-bound: the span ends when the Response is built, never
|
|
512
|
+
// wrapping the streamed body. metric:false — handler:total is metered
|
|
513
|
+
// directly below (a grand total incl. the pre-context bootstrap timings).
|
|
514
|
+
// When tracing is off this is a direct pass-through.
|
|
515
|
+
return runWithRequestContext(requestContext, () =>
|
|
516
|
+
observePhase(PHASES.request, async (span) => {
|
|
517
|
+
span.setAttribute("http.method", request.method);
|
|
518
|
+
// The matched route template is not known until match() runs later, so
|
|
519
|
+
// emit the concrete path as url.path (low-level), NOT http.route — the
|
|
520
|
+
// latter is reserved for the low-cardinality template (OTel convention).
|
|
521
|
+
span.setAttribute("url.path", url.pathname);
|
|
522
|
+
|
|
523
|
+
// Core handler logic (wrapped by middleware)
|
|
524
|
+
const coreHandler = async (): Promise<Response> => {
|
|
525
|
+
return coreRequestHandler(request, env, url, variables, nonce);
|
|
526
|
+
};
|
|
490
527
|
|
|
491
|
-
if
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
528
|
+
// Execute middleware chain if any, otherwise call core handler directly
|
|
529
|
+
let response: Response;
|
|
530
|
+
if (matchedMiddleware.length > 0) {
|
|
531
|
+
const mwResponse = await executeMiddleware(
|
|
532
|
+
matchedMiddleware,
|
|
533
|
+
request,
|
|
534
|
+
env,
|
|
535
|
+
variables,
|
|
536
|
+
coreHandler,
|
|
537
|
+
createReverseFunction(getRequiredRouteMap()),
|
|
498
538
|
);
|
|
499
|
-
|
|
539
|
+
|
|
540
|
+
if (
|
|
541
|
+
url.searchParams.has("_rsc_partial") ||
|
|
542
|
+
url.searchParams.has("_rsc_action")
|
|
543
|
+
) {
|
|
544
|
+
const intercepted = interceptRedirectForPartial(
|
|
545
|
+
mwResponse,
|
|
546
|
+
createRedirectFlightResponse,
|
|
547
|
+
);
|
|
548
|
+
response = intercepted ?? finalizeResponse(mwResponse);
|
|
549
|
+
} else {
|
|
550
|
+
response = finalizeResponse(mwResponse);
|
|
551
|
+
}
|
|
500
552
|
} else {
|
|
501
|
-
response =
|
|
553
|
+
response = await coreHandler();
|
|
502
554
|
}
|
|
503
|
-
} else {
|
|
504
|
-
response = await coreHandler();
|
|
505
|
-
}
|
|
506
555
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
556
|
+
// Finalize metrics after all middleware (including post-next work)
|
|
557
|
+
// has completed so :post spans are captured in the timeline.
|
|
558
|
+
// Handler timing parts are always emitted (even without debug metrics)
|
|
559
|
+
// so non-debug requests still get bootstrap Server-Timing entries.
|
|
560
|
+
const handlerTimingArr: string[] = variables.__handlerTiming || [];
|
|
561
|
+
// Preserve any existing Server-Timing set by response routes or middleware
|
|
562
|
+
const existingTiming = response.headers.get("Server-Timing");
|
|
563
|
+
const timingParts = existingTiming
|
|
564
|
+
? [existingTiming, ...handlerTimingArr]
|
|
565
|
+
: [...handlerTimingArr];
|
|
566
|
+
|
|
567
|
+
const metricsStore = requestContext._metricsStore;
|
|
568
|
+
if (metricsStore) {
|
|
569
|
+
// When the store was created at handler start (earlyMetricsStore),
|
|
570
|
+
// handler:total covers the full request. When ctx.debugPerformance()
|
|
571
|
+
// created the store mid-request, use its requestStart to avoid a
|
|
572
|
+
// negative startTime offset.
|
|
573
|
+
const totalStart = earlyMetricsStore
|
|
574
|
+
? handlerStart
|
|
575
|
+
: metricsStore.requestStart;
|
|
576
|
+
appendMetric(
|
|
577
|
+
metricsStore,
|
|
578
|
+
"handler:total",
|
|
579
|
+
totalStart,
|
|
580
|
+
performance.now() - totalStart,
|
|
581
|
+
);
|
|
582
|
+
const metricsTiming = buildMetricsTiming(
|
|
583
|
+
request.method,
|
|
584
|
+
url.pathname,
|
|
585
|
+
metricsStore,
|
|
586
|
+
);
|
|
587
|
+
if (metricsTiming) timingParts.push(metricsTiming);
|
|
588
|
+
}
|
|
540
589
|
|
|
541
|
-
|
|
542
|
-
|
|
590
|
+
const fullTiming = timingParts.join(", ");
|
|
591
|
+
if (fullTiming && !isWebSocketUpgradeResponse(response)) {
|
|
592
|
+
response.headers.set("Server-Timing", fullTiming);
|
|
593
|
+
}
|
|
543
594
|
|
|
544
|
-
|
|
545
|
-
|
|
595
|
+
// Single open-redirect chokepoint: every response (PE, full-page,
|
|
596
|
+
// middleware short-circuit, response-route) funnels through here, so
|
|
597
|
+
// guarding browser-followed (3xx) redirects once covers them all and any
|
|
598
|
+
// future redirect exit. Soft SPA/Flight redirects are 200/204 and pass
|
|
599
|
+
// through untouched (validated client-side instead).
|
|
600
|
+
return guardOutgoingRedirect(response, url.origin, router.basename);
|
|
601
|
+
}),
|
|
602
|
+
);
|
|
546
603
|
};
|
|
547
604
|
|
|
548
|
-
// Core request handling logic (separated for middleware wrapping)
|
|
605
|
+
// Core request handling logic (separated for middleware wrapping).
|
|
606
|
+
// Uses the classify → execute model: classifyRequest produces a RequestPlan,
|
|
607
|
+
// then execution dispatches on the plan mode.
|
|
549
608
|
async function coreRequestHandler(
|
|
550
609
|
request: Request,
|
|
551
610
|
env: TEnv,
|
|
@@ -553,71 +612,103 @@ export function createRSCHandler<
|
|
|
553
612
|
variables: Record<string, any>,
|
|
554
613
|
nonce: string | undefined,
|
|
555
614
|
): Promise<Response> {
|
|
556
|
-
const previewStart = performance.now();
|
|
557
|
-
const preview = await router.previewMatch(request, { env });
|
|
558
|
-
const previewDur = performance.now() - previewStart;
|
|
559
615
|
const handlerTiming: string[] = variables.__handlerTiming || [];
|
|
560
|
-
|
|
561
|
-
//
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
616
|
+
|
|
617
|
+
// Debug manifest endpoint: handled before classification since it
|
|
618
|
+
// doesn't need a route match and needs trie access from the closure.
|
|
619
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
620
|
+
if (
|
|
621
|
+
url.searchParams.has("__debug_manifest") &&
|
|
622
|
+
(isDev || router.allowDebugManifest)
|
|
623
|
+
) {
|
|
624
|
+
const trie = getRouterTrie(router.id) ?? getRouteTrie();
|
|
625
|
+
const routeManifest = getRequiredRouteMap();
|
|
626
|
+
const { extractAncestryFromTrie } =
|
|
627
|
+
await import("../build/route-trie.js");
|
|
628
|
+
return new Response(
|
|
629
|
+
JSON.stringify(
|
|
630
|
+
{
|
|
631
|
+
routerId: router.id,
|
|
632
|
+
routeManifest,
|
|
633
|
+
routeAncestry: trie ? extractAncestryFromTrie(trie) : {},
|
|
634
|
+
routeTrie: trie,
|
|
635
|
+
precomputedEntries: getPrecomputedEntries(),
|
|
636
|
+
},
|
|
637
|
+
null,
|
|
638
|
+
2,
|
|
571
639
|
),
|
|
572
|
-
|
|
573
|
-
|
|
640
|
+
{
|
|
641
|
+
headers: { "Content-Type": "application/json" },
|
|
642
|
+
},
|
|
574
643
|
);
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// ---- 1. Classify ----
|
|
647
|
+
// classifyRequest may throw RouteNotFoundError for unknown routes.
|
|
648
|
+
// In that case, fall through to a full-render plan so the pipeline
|
|
649
|
+
// can render the 404 page via the existing error handling path.
|
|
650
|
+
const classifyStart = performance.now();
|
|
651
|
+
let plan: RequestPlan<TEnv>;
|
|
652
|
+
try {
|
|
653
|
+
plan = await classifyRequest<TEnv>(request, url, {
|
|
654
|
+
findMatch: router.findMatch,
|
|
655
|
+
routerVersion: version,
|
|
656
|
+
routerId: router.id,
|
|
657
|
+
});
|
|
658
|
+
} catch (error) {
|
|
659
|
+
if (isRouteNotFoundError(error)) {
|
|
660
|
+
// Let the render path handle 404 — match()/matchPartial() will
|
|
661
|
+
// re-throw RouteNotFoundError and the catch block in
|
|
662
|
+
// executeRenderWithMiddleware renders the not-found page.
|
|
663
|
+
plan = {
|
|
664
|
+
mode: "full-render",
|
|
665
|
+
route: {
|
|
666
|
+
matched: null as any,
|
|
667
|
+
manifestEntry: null as any,
|
|
668
|
+
entries: [],
|
|
669
|
+
routeKey: "",
|
|
670
|
+
localRouteName: "",
|
|
671
|
+
params: {},
|
|
672
|
+
routeMiddleware: [],
|
|
673
|
+
cacheScope: null,
|
|
674
|
+
isPassthrough: false,
|
|
675
|
+
},
|
|
676
|
+
negotiated: false,
|
|
677
|
+
};
|
|
678
|
+
} else {
|
|
679
|
+
throw error;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
const classifyDur = performance.now() - classifyStart;
|
|
683
|
+
handlerTiming.push(`handler-classify;dur=${classifyDur.toFixed(2)}`);
|
|
684
|
+
|
|
685
|
+
// ---- 2. Terminal plans (no execution needed) ----
|
|
686
|
+
if (plan.mode === "redirect") {
|
|
687
|
+
// Redirects are handled by the pipeline (match/matchPartial),
|
|
688
|
+
// but for partial requests we short-circuit with a Flight redirect.
|
|
689
|
+
if (url.searchParams.has("_rsc_partial")) {
|
|
690
|
+
return createRedirectFlightResponse(plan.redirectUrl);
|
|
584
691
|
}
|
|
585
|
-
|
|
692
|
+
// Full requests: let the pipeline handle the redirect via match()
|
|
693
|
+
// which returns { redirect: url }. Fall through to full-render.
|
|
586
694
|
}
|
|
587
695
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
if (mayNeedSSR(request, url)) {
|
|
592
|
-
variables[SSR_SETUP_VAR] = startSSRSetup(
|
|
593
|
-
handlerCtx,
|
|
594
|
-
request,
|
|
595
|
-
env,
|
|
596
|
-
url,
|
|
597
|
-
router.debugPerformance
|
|
598
|
-
? () => requireRequestContext()._metricsStore
|
|
599
|
-
: undefined,
|
|
696
|
+
if (plan.mode === "version-mismatch") {
|
|
697
|
+
console.log(
|
|
698
|
+
`[RSC] Version mismatch: client=${url.searchParams.get("_rsc_v")}, server=${version}. Forcing reload.`,
|
|
600
699
|
);
|
|
700
|
+
return createReloadResponse(plan.reloadUrl);
|
|
601
701
|
}
|
|
602
702
|
|
|
603
|
-
|
|
703
|
+
if (plan.mode === "app-switch") {
|
|
704
|
+
// Cross-app SPA navigation crossed a host-router app boundary. Force a
|
|
705
|
+
// real document navigation so the target app's document is re-established
|
|
706
|
+
// (stylesheets, theme, warmup, prefetch-TTL). See request-classification.
|
|
707
|
+
return createReloadResponse(plan.reloadUrl);
|
|
708
|
+
}
|
|
604
709
|
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
const isLoaderFetch = url.searchParams.has("_rsc_loader");
|
|
608
|
-
const actionId =
|
|
609
|
-
request.headers.get("rsc-action") || url.searchParams.get("_rsc_action");
|
|
610
|
-
|
|
611
|
-
// Origin guard: reject cross-origin actions, loader fetches, and
|
|
612
|
-
// PE form submissions before any execution. Regular page navigations
|
|
613
|
-
// (GET without _rsc_loader/_rsc_action) are not affected.
|
|
614
|
-
const originPhase: OriginCheckPhase | null = isAction
|
|
615
|
-
? "action"
|
|
616
|
-
: isLoaderFetch
|
|
617
|
-
? "loader"
|
|
618
|
-
: request.method === "POST"
|
|
619
|
-
? "pe-form"
|
|
620
|
-
: null;
|
|
710
|
+
// ---- 3. Origin guard (gate for action/loader/PE modes) ----
|
|
711
|
+
const originPhase = ORIGIN_CHECK_PHASE_BY_MODE[plan.mode];
|
|
621
712
|
if (originPhase) {
|
|
622
713
|
const originResult = await checkRequestOrigin(
|
|
623
714
|
request,
|
|
@@ -645,37 +736,49 @@ export function createRSCHandler<
|
|
|
645
736
|
},
|
|
646
737
|
});
|
|
647
738
|
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
phase: originPhase,
|
|
658
|
-
origin: request.headers.get("origin"),
|
|
659
|
-
host: request.headers.get("host"),
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
} catch {
|
|
663
|
-
// Router context may not be available
|
|
664
|
-
}
|
|
739
|
+
observeEvent({
|
|
740
|
+
type: "request.origin-rejected",
|
|
741
|
+
timestamp: performance.now(),
|
|
742
|
+
method: request.method,
|
|
743
|
+
pathname: url.pathname,
|
|
744
|
+
phase: originPhase,
|
|
745
|
+
origin: request.headers.get("origin"),
|
|
746
|
+
host: request.headers.get("host"),
|
|
747
|
+
});
|
|
665
748
|
|
|
666
749
|
return originResult;
|
|
667
750
|
}
|
|
668
751
|
}
|
|
669
752
|
|
|
670
|
-
//
|
|
671
|
-
|
|
753
|
+
// ---- 4. Execute ----
|
|
754
|
+
return executeRequest(
|
|
755
|
+
plan as ExecutableRequestPlan<TEnv>,
|
|
756
|
+
request,
|
|
757
|
+
env,
|
|
758
|
+
url,
|
|
759
|
+
variables,
|
|
760
|
+
nonce,
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Execute a classified request plan. Dispatches to the appropriate handler
|
|
765
|
+
// based on plan.mode. Lives in the createRSCHandler closure for access to
|
|
766
|
+
// handlerCtx, router, callOnError, etc.
|
|
767
|
+
// Only receives executable plans (version-mismatch is handled above).
|
|
768
|
+
async function executeRequest(
|
|
769
|
+
plan: ExecutableRequestPlan<TEnv>,
|
|
770
|
+
request: Request,
|
|
771
|
+
env: TEnv,
|
|
772
|
+
url: URL,
|
|
773
|
+
variables: Record<string, any>,
|
|
774
|
+
nonce: string | undefined,
|
|
775
|
+
): Promise<Response> {
|
|
776
|
+
// Common setup
|
|
777
|
+
const handleStore = getRequestContext()._handleStore;
|
|
672
778
|
|
|
673
779
|
// Wire up error reporting for late streaming-handle failures
|
|
674
|
-
// (LateHandlePushError: handle pushed after stream completion).
|
|
675
|
-
// Without this, these errors are only caught by React's error boundary
|
|
676
|
-
// and never reach the router's onError callback or telemetry.
|
|
677
780
|
handleStore.onError = (error: Error) => {
|
|
678
|
-
const reqCtx =
|
|
781
|
+
const reqCtx = getRequestContext();
|
|
679
782
|
callOnError(error, "handler", {
|
|
680
783
|
request,
|
|
681
784
|
url,
|
|
@@ -683,66 +786,149 @@ export function createRSCHandler<
|
|
|
683
786
|
params: reqCtx.params as Record<string, string>,
|
|
684
787
|
handledByBoundary: true,
|
|
685
788
|
});
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
pathname: url.pathname,
|
|
696
|
-
routeKey: reqCtx._routeName,
|
|
697
|
-
params: reqCtx.params as Record<string, string>,
|
|
698
|
-
});
|
|
699
|
-
}
|
|
700
|
-
} catch {
|
|
701
|
-
// Router context may not be available (e.g. prerender path)
|
|
702
|
-
}
|
|
789
|
+
observeEvent({
|
|
790
|
+
type: "handler.error",
|
|
791
|
+
timestamp: performance.now(),
|
|
792
|
+
error,
|
|
793
|
+
handledByBoundary: true,
|
|
794
|
+
pathname: url.pathname,
|
|
795
|
+
routeKey: reqCtx._routeName,
|
|
796
|
+
params: reqCtx.params as Record<string, string>,
|
|
797
|
+
});
|
|
703
798
|
};
|
|
704
799
|
|
|
705
800
|
// Set route params early so all execution paths can access ctx.params.
|
|
706
|
-
|
|
707
|
-
|
|
801
|
+
// Also store the classified snapshot so match/matchPartial can reuse it
|
|
802
|
+
// instead of calling resolveRoute again.
|
|
803
|
+
if (plan.mode !== "redirect") {
|
|
804
|
+
setRequestContextParams(plan.route.params, plan.route.routeKey);
|
|
805
|
+
getRequestContext()._classifiedRoute = plan.route;
|
|
708
806
|
}
|
|
709
807
|
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
//
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
routeMiddleware:
|
|
808
|
+
const routeReverse = createReverseFunction(getRequiredRouteMap());
|
|
809
|
+
|
|
810
|
+
// ---- Response route: skip entire RSC pipeline ----
|
|
811
|
+
if (plan.mode === "response") {
|
|
812
|
+
// Build ResponseRouteMatch from plan fields. handleResponseRoute
|
|
813
|
+
// expects a flat object with params at the top level.
|
|
814
|
+
const responseMatch: ResponseRouteMatch = {
|
|
815
|
+
responseType: plan.responseType,
|
|
816
|
+
handler: plan.handler,
|
|
817
|
+
params: plan.route.params,
|
|
818
|
+
negotiated: plan.negotiated,
|
|
819
|
+
manifestEntry: plan.manifestEntry,
|
|
820
|
+
routeMiddleware: plan.routeMiddleware,
|
|
821
|
+
};
|
|
822
|
+
const responseOutcome = await withTimeout(
|
|
823
|
+
handleResponseRoute(
|
|
824
|
+
handlerCtx,
|
|
825
|
+
responseMatch,
|
|
826
|
+
request,
|
|
827
|
+
env,
|
|
828
|
+
url,
|
|
829
|
+
variables,
|
|
830
|
+
),
|
|
831
|
+
router.timeouts.renderStartMs,
|
|
832
|
+
"render-start",
|
|
833
|
+
);
|
|
834
|
+
if (responseOutcome.timedOut) {
|
|
835
|
+
return handleTimeoutResponse(
|
|
836
|
+
request,
|
|
837
|
+
env,
|
|
838
|
+
url,
|
|
839
|
+
"render-start",
|
|
840
|
+
responseOutcome.durationMs,
|
|
841
|
+
plan.route.routeKey,
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
const response = responseOutcome.result;
|
|
845
|
+
if (plan.negotiated && !isWebSocketUpgradeResponse(response)) {
|
|
846
|
+
// handleResponseRoute (callHandlerWithVary) already appends Vary: Accept
|
|
847
|
+
// for negotiated responses; dedup so we don't emit Vary: Accept, Accept.
|
|
848
|
+
appendVaryAccept(response);
|
|
849
|
+
}
|
|
850
|
+
return response;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// SSR setup: kick off in parallel for modes that need HTML rendering.
|
|
854
|
+
// Placed after response-route short-circuit so response/mime routes
|
|
855
|
+
// never pay for SSR work.
|
|
856
|
+
//
|
|
857
|
+
// Only kick off when the request will actually render HTML, so the
|
|
858
|
+
// eager loadSSRModule() + user resolveStreaming() are not started (and
|
|
859
|
+
// never consumed) for a request that returns an RSC stream — that wasted
|
|
860
|
+
// work also leaves an orphaned Promise.all that can reject (D7). PE form
|
|
861
|
+
// submissions always render HTML (handleProgressiveEnhancement renders via
|
|
862
|
+
// getSSRSetup regardless of Accept). For full/partial-render and action,
|
|
863
|
+
// the render-time HTML decision is exactly !isRscRequest — mayNeedSSR is
|
|
864
|
+
// the coarse transport pre-filter, isRscRequest is the precise Accept call
|
|
865
|
+
// (it, unlike mayNeedSSR, treats a MISSING Accept as RSC). Both must pass.
|
|
866
|
+
const willRenderHtml =
|
|
867
|
+
plan.mode === "pe-render" ||
|
|
868
|
+
(mayNeedSSR(request, url) &&
|
|
869
|
+
!isRscRequest(request, url, plan.mode === "partial-render"));
|
|
870
|
+
if (plan.mode !== "loader" && willRenderHtml) {
|
|
871
|
+
variables[SSR_SETUP_VAR] = startSSRSetup(
|
|
872
|
+
handlerCtx,
|
|
873
|
+
request,
|
|
874
|
+
env,
|
|
875
|
+
url,
|
|
876
|
+
router.debugPerformance
|
|
877
|
+
? () => getRequestContext()._metricsStore
|
|
878
|
+
: undefined,
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// ---- Loader fetch ----
|
|
883
|
+
if (plan.mode === "loader") {
|
|
884
|
+
return handleLoaderFetch(
|
|
885
|
+
handlerCtx,
|
|
886
|
+
request,
|
|
887
|
+
env,
|
|
888
|
+
url,
|
|
723
889
|
variables,
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
);
|
|
727
|
-
if (progressiveResult) {
|
|
728
|
-
return progressiveResult;
|
|
890
|
+
plan.route.params,
|
|
891
|
+
);
|
|
729
892
|
}
|
|
730
893
|
|
|
731
|
-
//
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
894
|
+
// ---- Progressive enhancement ----
|
|
895
|
+
if (plan.mode === "pe-render") {
|
|
896
|
+
const peResult = await handleProgressiveEnhancement(
|
|
897
|
+
handlerCtx,
|
|
898
|
+
request,
|
|
899
|
+
env,
|
|
900
|
+
url,
|
|
901
|
+
false, // isAction = false for PE
|
|
902
|
+
handleStore,
|
|
903
|
+
nonce,
|
|
904
|
+
{
|
|
905
|
+
routeMiddleware: plan.route.routeMiddleware,
|
|
906
|
+
variables,
|
|
907
|
+
routeReverse,
|
|
908
|
+
},
|
|
909
|
+
);
|
|
910
|
+
if (peResult) return peResult;
|
|
911
|
+
// PE handler returned null (not a PE form) — fall through to render
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// ---- Action: execute action, then revalidate wrapped in route middleware ----
|
|
915
|
+
if (plan.mode === "action") {
|
|
916
|
+
let actionContinuation: ActionContinuation | undefined;
|
|
737
917
|
try {
|
|
918
|
+
// Instrument the action execution as its own phase (action:<actionId> +
|
|
919
|
+
// rango.action), so a POST shows the mutation time AND which action ran,
|
|
920
|
+
// not just the downstream revalidation render. The action's own
|
|
921
|
+
// loaders/fetches nest under rango.action.
|
|
738
922
|
const actionOutcome = await withTimeout(
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
923
|
+
observePhase(PHASES.action(plan.actionId), () =>
|
|
924
|
+
executeServerAction(
|
|
925
|
+
handlerCtx,
|
|
926
|
+
request,
|
|
927
|
+
env,
|
|
928
|
+
url,
|
|
929
|
+
plan.actionId,
|
|
930
|
+
handleStore,
|
|
931
|
+
),
|
|
746
932
|
),
|
|
747
933
|
router.timeouts.actionMs,
|
|
748
934
|
"action",
|
|
@@ -754,8 +940,8 @@ export function createRSCHandler<
|
|
|
754
940
|
url,
|
|
755
941
|
"action",
|
|
756
942
|
actionOutcome.durationMs,
|
|
757
|
-
|
|
758
|
-
actionId,
|
|
943
|
+
plan.route.routeKey,
|
|
944
|
+
plan.actionId,
|
|
759
945
|
);
|
|
760
946
|
}
|
|
761
947
|
const result = actionOutcome.result;
|
|
@@ -767,40 +953,248 @@ export function createRSCHandler<
|
|
|
767
953
|
request,
|
|
768
954
|
url,
|
|
769
955
|
env,
|
|
770
|
-
actionId,
|
|
956
|
+
actionId: plan.actionId,
|
|
771
957
|
handledByBoundary: false,
|
|
772
958
|
});
|
|
773
959
|
console.error(`[RSC] Action error:`, error);
|
|
774
960
|
throw error;
|
|
775
961
|
}
|
|
776
|
-
}
|
|
777
962
|
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
963
|
+
// Revalidation render wrapped in route middleware.
|
|
964
|
+
// Actions from client-side navigation include _rsc_partial — preserve
|
|
965
|
+
// the partial flag so the revalidation returns a Flight stream, not HTML.
|
|
966
|
+
// App-switch is already excluded by classifyRequest (would be full-render).
|
|
967
|
+
const isPartialAction = url.searchParams.has("_rsc_partial");
|
|
968
|
+
return executeRenderWithMiddleware(
|
|
969
|
+
plan.route.routeMiddleware,
|
|
970
|
+
plan.negotiated,
|
|
971
|
+
plan.route.routeKey,
|
|
972
|
+
routeReverse,
|
|
782
973
|
request,
|
|
783
974
|
env,
|
|
784
975
|
url,
|
|
785
976
|
variables,
|
|
786
977
|
nonce,
|
|
787
|
-
preview?.params,
|
|
788
|
-
preview?.routeKey,
|
|
789
978
|
handleStore,
|
|
979
|
+
isPartialAction,
|
|
790
980
|
actionContinuation,
|
|
791
981
|
);
|
|
792
|
-
|
|
793
|
-
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// Full render, partial render, fallen-through PE, and full-page redirect all
|
|
985
|
+
// render through the same middleware-wrapped path. Only full/partial-render
|
|
986
|
+
// carry negotiation + the partial flag; pe/redirect render plainly.
|
|
987
|
+
const isPartial = plan.mode === "partial-render";
|
|
988
|
+
const negotiated =
|
|
989
|
+
plan.mode === "full-render" || plan.mode === "partial-render"
|
|
990
|
+
? plan.negotiated
|
|
991
|
+
: false;
|
|
992
|
+
return executeRenderWithMiddleware(
|
|
993
|
+
plan.route.routeMiddleware,
|
|
994
|
+
negotiated,
|
|
995
|
+
plan.route.routeKey,
|
|
996
|
+
routeReverse,
|
|
997
|
+
request,
|
|
998
|
+
env,
|
|
999
|
+
url,
|
|
1000
|
+
variables,
|
|
1001
|
+
nonce,
|
|
1002
|
+
handleStore,
|
|
1003
|
+
isPartial,
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// Shared render execution: wraps handleRscRendering (or revalidateAfterAction)
|
|
1008
|
+
// in route middleware and timeout handling. Consolidates the pattern used by
|
|
1009
|
+
// action-revalidate, full-render, and partial-render modes.
|
|
1010
|
+
async function executeRenderWithMiddleware(
|
|
1011
|
+
routeMiddleware: import("../router/middleware-types.js").CollectedMiddleware[],
|
|
1012
|
+
negotiated: boolean,
|
|
1013
|
+
routeKey: string,
|
|
1014
|
+
routeReverse: ReturnType<typeof createReverseFunction>,
|
|
1015
|
+
request: Request,
|
|
1016
|
+
env: TEnv,
|
|
1017
|
+
url: URL,
|
|
1018
|
+
variables: Record<string, any>,
|
|
1019
|
+
nonce: string | undefined,
|
|
1020
|
+
handleStore: ReturnType<typeof getRequestContext>["_handleStore"],
|
|
1021
|
+
isPartial: boolean,
|
|
1022
|
+
actionContinuation?: ActionContinuation,
|
|
1023
|
+
): Promise<Response> {
|
|
1024
|
+
const renderHandler = async (): Promise<Response> => {
|
|
1025
|
+
try {
|
|
1026
|
+
let response: Response;
|
|
1027
|
+
if (actionContinuation) {
|
|
1028
|
+
response = await revalidateAfterAction(
|
|
1029
|
+
handlerCtx,
|
|
1030
|
+
request,
|
|
1031
|
+
env,
|
|
1032
|
+
url,
|
|
1033
|
+
handleStore,
|
|
1034
|
+
actionContinuation,
|
|
1035
|
+
);
|
|
1036
|
+
} else {
|
|
1037
|
+
response = await handleRscRendering(
|
|
1038
|
+
handlerCtx,
|
|
1039
|
+
request,
|
|
1040
|
+
env,
|
|
1041
|
+
url,
|
|
1042
|
+
isPartial,
|
|
1043
|
+
handleStore,
|
|
1044
|
+
nonce,
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
if (negotiated && !isWebSocketUpgradeResponse(response)) {
|
|
1048
|
+
// handleRscRendering bakes `accept` into the RSC response's Vary list;
|
|
1049
|
+
// dedup so the negotiated append does not list accept twice.
|
|
1050
|
+
appendVaryAccept(response);
|
|
1051
|
+
}
|
|
1052
|
+
return response;
|
|
1053
|
+
} catch (error) {
|
|
1054
|
+
// Check if middleware/handler returned Response
|
|
1055
|
+
if (error instanceof Response) {
|
|
1056
|
+
// An action revalidation render is delivered to the client over the
|
|
1057
|
+
// same Flight-parsing path as a partial navigation, so a Response
|
|
1058
|
+
// thrown during it must be converted exactly like a partial one
|
|
1059
|
+
// (raw 200 -> hard-nav hint, 3xx -> Flight redirect). Without this,
|
|
1060
|
+
// the no-middleware path returns the raw Response (the with-middleware
|
|
1061
|
+
// path is already covered by the isPartial || actionContinuation
|
|
1062
|
+
// guard below).
|
|
1063
|
+
const treatAsPartial = isPartial || actionContinuation != null;
|
|
1064
|
+
|
|
1065
|
+
// During partial (client-side navigation), a 200 Response from a handler
|
|
1066
|
+
// means the route serves raw content (JSON, text, etc.), not JSX.
|
|
1067
|
+
// Signal the browser to hard-navigate so it renders the raw response.
|
|
1068
|
+
if (treatAsPartial && error.status === 200) {
|
|
1069
|
+
console.warn(
|
|
1070
|
+
`[RSC] Route handler at ${url.pathname} returned a Response during client-side navigation. ` +
|
|
1071
|
+
`Falling back to hard navigation. Use data-external on the <Link> to avoid the extra round-trip.`,
|
|
1072
|
+
);
|
|
1073
|
+
return createResponseWithMergedHeaders(null, {
|
|
1074
|
+
status: 200,
|
|
1075
|
+
headers: {
|
|
1076
|
+
"X-RSC-Reload": stripInternalParams(url).toString(),
|
|
1077
|
+
"content-type": "text/x-component;charset=utf-8",
|
|
1078
|
+
},
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
if (treatAsPartial) {
|
|
1083
|
+
const intercepted = interceptRedirectForPartial(
|
|
1084
|
+
error,
|
|
1085
|
+
createRedirectFlightResponse,
|
|
1086
|
+
);
|
|
1087
|
+
if (intercepted) return intercepted;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
return error;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
// Render 404 page for unmatched routes
|
|
1094
|
+
if (isRouteNotFoundError(error)) {
|
|
1095
|
+
callOnError(error, "routing", {
|
|
1096
|
+
request,
|
|
1097
|
+
url,
|
|
1098
|
+
env,
|
|
1099
|
+
handledByBoundary: true,
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
const notFoundOption = router.notFound;
|
|
1103
|
+
const notFoundComponent =
|
|
1104
|
+
typeof notFoundOption === "function"
|
|
1105
|
+
? notFoundOption({ pathname: url.pathname })
|
|
1106
|
+
: (notFoundOption ?? createElement("h1", null, "Not Found"));
|
|
1107
|
+
|
|
1108
|
+
const notFoundSegment = {
|
|
1109
|
+
id: "notFound",
|
|
1110
|
+
namespace: "notFound",
|
|
1111
|
+
type: "route" as const,
|
|
1112
|
+
index: 0,
|
|
1113
|
+
component: notFoundComponent,
|
|
1114
|
+
params: {},
|
|
1115
|
+
};
|
|
1116
|
+
|
|
1117
|
+
const payload: RscPayload = {
|
|
1118
|
+
metadata: {
|
|
1119
|
+
pathname: url.pathname,
|
|
1120
|
+
routerId: router.id,
|
|
1121
|
+
basename: router.basename,
|
|
1122
|
+
segments: [notFoundSegment],
|
|
1123
|
+
matched: [],
|
|
1124
|
+
diff: [],
|
|
1125
|
+
// Shape parity with buildFullPayload (rsc-rendering.ts): the 404
|
|
1126
|
+
// payload carries params/resolvedIds/prefetchCacheTTL the same way a
|
|
1127
|
+
// matched full render does. resolvedIds mirrors the rendered segment
|
|
1128
|
+
// list (the single notFound segment) like the error-boundary path
|
|
1129
|
+
// (match-api.ts) does for its boundary segment.
|
|
1130
|
+
resolvedIds: [notFoundSegment.id],
|
|
1131
|
+
params: {},
|
|
1132
|
+
isPartial: false,
|
|
1133
|
+
rootLayout: router.rootLayout,
|
|
1134
|
+
handles: handleStore.stream(),
|
|
1135
|
+
version,
|
|
1136
|
+
prefetchCacheTTL: router.prefetchCacheTTL,
|
|
1137
|
+
stateCookieName: router.resolvedStateCookieName,
|
|
1138
|
+
themeConfig: router.themeConfig,
|
|
1139
|
+
warmupEnabled: router.warmupEnabled,
|
|
1140
|
+
strictMode: router.strictMode,
|
|
1141
|
+
initialTheme: getRequestContext().theme,
|
|
1142
|
+
},
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
const rscStream = renderToReadableStream(payload, {
|
|
1146
|
+
onError: (error: unknown) => {
|
|
1147
|
+
callOnError(error, "rendering", { request, url, env });
|
|
1148
|
+
},
|
|
1149
|
+
});
|
|
1150
|
+
|
|
1151
|
+
if (isRscRequest(request, url, isPartial)) {
|
|
1152
|
+
return createResponseWithMergedHeaders(rscStream, {
|
|
1153
|
+
status: 404,
|
|
1154
|
+
headers: {
|
|
1155
|
+
"content-type": "text/x-component;charset=utf-8",
|
|
1156
|
+
// Router identity for the client's pre-decode integrity check; a
|
|
1157
|
+
// same-app 404 matches and applies in place. See response-adapter.
|
|
1158
|
+
"X-RSC-Router-Id": router.id,
|
|
1159
|
+
},
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
const [ssrModule, streamMode] = await getSSRSetup(
|
|
1164
|
+
handlerCtx,
|
|
1165
|
+
request,
|
|
1166
|
+
env,
|
|
1167
|
+
url,
|
|
1168
|
+
getRequestContext()._metricsStore,
|
|
1169
|
+
);
|
|
1170
|
+
const htmlStream = await ssrModule.renderHTML(rscStream, {
|
|
1171
|
+
nonce,
|
|
1172
|
+
streamMode,
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1175
|
+
return createResponseWithMergedHeaders(htmlStream, {
|
|
1176
|
+
status: 404,
|
|
1177
|
+
headers: { "content-type": "text/html;charset=utf-8" },
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
// Report unhandled errors
|
|
1182
|
+
callOnError(error, "routing", {
|
|
1183
|
+
request,
|
|
1184
|
+
url,
|
|
1185
|
+
env,
|
|
1186
|
+
handledByBoundary: false,
|
|
1187
|
+
});
|
|
1188
|
+
console.error(`[RSC] Error:`, error);
|
|
1189
|
+
throw error;
|
|
794
1190
|
}
|
|
795
|
-
return response;
|
|
796
1191
|
};
|
|
797
1192
|
|
|
798
|
-
// Wrap the render path
|
|
799
|
-
// renderStartMs timeout so slow renders are caught before output.
|
|
1193
|
+
// Wrap the render path in a renderStartMs timeout
|
|
800
1194
|
const executeRender = async (): Promise<Response> => {
|
|
801
|
-
if (
|
|
1195
|
+
if (routeMiddleware.length > 0) {
|
|
802
1196
|
const mwResponse = await executeMiddleware(
|
|
803
|
-
buildRouteMiddlewareEntries<TEnv>(
|
|
1197
|
+
buildRouteMiddlewareEntries<TEnv>(routeMiddleware),
|
|
804
1198
|
request,
|
|
805
1199
|
env,
|
|
806
1200
|
variables,
|
|
@@ -808,10 +1202,7 @@ export function createRSCHandler<
|
|
|
808
1202
|
routeReverse,
|
|
809
1203
|
);
|
|
810
1204
|
|
|
811
|
-
if (
|
|
812
|
-
url.searchParams.has("_rsc_partial") ||
|
|
813
|
-
url.searchParams.has("_rsc_action")
|
|
814
|
-
) {
|
|
1205
|
+
if (isPartial || actionContinuation) {
|
|
815
1206
|
const intercepted = interceptRedirectForPartial(
|
|
816
1207
|
mwResponse,
|
|
817
1208
|
createRedirectFlightResponse,
|
|
@@ -822,7 +1213,6 @@ export function createRSCHandler<
|
|
|
822
1213
|
return finalizeResponse(mwResponse);
|
|
823
1214
|
}
|
|
824
1215
|
|
|
825
|
-
// No route middleware, proceed directly
|
|
826
1216
|
return renderHandler();
|
|
827
1217
|
};
|
|
828
1218
|
|
|
@@ -838,273 +1228,9 @@ export function createRSCHandler<
|
|
|
838
1228
|
url,
|
|
839
1229
|
"render-start",
|
|
840
1230
|
renderOutcome.durationMs,
|
|
841
|
-
|
|
1231
|
+
routeKey,
|
|
842
1232
|
);
|
|
843
1233
|
}
|
|
844
1234
|
return renderOutcome.result;
|
|
845
1235
|
}
|
|
846
|
-
|
|
847
|
-
// Inner request handler: rendering logic wrapped by route middleware.
|
|
848
|
-
// Handles action revalidation (when actionContinuation is present),
|
|
849
|
-
// loader fetches, and regular RSC rendering.
|
|
850
|
-
async function coreRequestHandlerInner(
|
|
851
|
-
request: Request,
|
|
852
|
-
env: TEnv,
|
|
853
|
-
url: URL,
|
|
854
|
-
variables: Record<string, any>,
|
|
855
|
-
nonce: string | undefined,
|
|
856
|
-
routeParams?: Record<string, string>,
|
|
857
|
-
routeKey?: string,
|
|
858
|
-
handleStore?: ReturnType<typeof requireRequestContext>["_handleStore"],
|
|
859
|
-
actionContinuation?: ActionContinuation,
|
|
860
|
-
): Promise<Response> {
|
|
861
|
-
const isPartial = url.searchParams.has("_rsc_partial");
|
|
862
|
-
const isAction =
|
|
863
|
-
request.headers.has("rsc-action") || url.searchParams.has("_rsc_action");
|
|
864
|
-
|
|
865
|
-
// Version mismatch detection - client may have stale code after HMR/deployment
|
|
866
|
-
// If versions don't match, tell the client to reload
|
|
867
|
-
const clientVersion = url.searchParams.get("_rsc_v");
|
|
868
|
-
if (version && clientVersion && clientVersion !== version) {
|
|
869
|
-
console.log(
|
|
870
|
-
`[RSC] Version mismatch: client=${clientVersion}, server=${version}. Forcing reload.`,
|
|
871
|
-
);
|
|
872
|
-
|
|
873
|
-
// For actions, reload current page (referer) if same origin.
|
|
874
|
-
// For navigation, load the target URL.
|
|
875
|
-
// Validate referer origin to prevent open redirect via crafted header.
|
|
876
|
-
let reloadUrl = stripInternalParams(url).toString();
|
|
877
|
-
if (isAction) {
|
|
878
|
-
const referer = request.headers.get("referer");
|
|
879
|
-
if (referer) {
|
|
880
|
-
try {
|
|
881
|
-
const refererUrl = new URL(referer);
|
|
882
|
-
if (refererUrl.origin === url.origin) {
|
|
883
|
-
reloadUrl = referer;
|
|
884
|
-
}
|
|
885
|
-
} catch {
|
|
886
|
-
// Malformed referer, fall back to cleanUrl
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
// Return special response that tells client to reload
|
|
892
|
-
return createResponseWithMergedHeaders(null, {
|
|
893
|
-
status: 200,
|
|
894
|
-
headers: {
|
|
895
|
-
"X-RSC-Reload": reloadUrl,
|
|
896
|
-
"content-type": "text/x-component;charset=utf-8",
|
|
897
|
-
},
|
|
898
|
-
});
|
|
899
|
-
}
|
|
900
|
-
// Debug manifest endpoint: ?__debug_manifest on any route.
|
|
901
|
-
// Always available in dev, requires allowDebugManifest option in production.
|
|
902
|
-
const isDev = process.env.NODE_ENV !== "production";
|
|
903
|
-
if (
|
|
904
|
-
url.searchParams.has("__debug_manifest") &&
|
|
905
|
-
(isDev || router.allowDebugManifest)
|
|
906
|
-
) {
|
|
907
|
-
const trie = getRouterTrie(router.id) ?? getRouteTrie();
|
|
908
|
-
const routeManifest = getRequiredRouteMap();
|
|
909
|
-
const { extractAncestryFromTrie } =
|
|
910
|
-
await import("../build/route-trie.js");
|
|
911
|
-
return new Response(
|
|
912
|
-
JSON.stringify(
|
|
913
|
-
{
|
|
914
|
-
routerId: router.id,
|
|
915
|
-
routeManifest,
|
|
916
|
-
routeAncestry: trie ? extractAncestryFromTrie(trie) : {},
|
|
917
|
-
routeTrie: trie,
|
|
918
|
-
precomputedEntries: getPrecomputedEntries(),
|
|
919
|
-
},
|
|
920
|
-
null,
|
|
921
|
-
2,
|
|
922
|
-
),
|
|
923
|
-
{
|
|
924
|
-
headers: { "Content-Type": "application/json" },
|
|
925
|
-
},
|
|
926
|
-
);
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
const store = handleStore ?? requireRequestContext()._handleStore;
|
|
930
|
-
|
|
931
|
-
try {
|
|
932
|
-
// Route params were already set in coreRequestHandler, but set again
|
|
933
|
-
// for callers that enter coreRequestHandlerInner directly.
|
|
934
|
-
if (routeParams) {
|
|
935
|
-
setRequestContextParams(routeParams, routeKey);
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
// ============================================================================
|
|
939
|
-
// ACTION REVALIDATION (action already executed, revalidate segments)
|
|
940
|
-
// ============================================================================
|
|
941
|
-
if (actionContinuation) {
|
|
942
|
-
return await revalidateAfterAction(
|
|
943
|
-
handlerCtx,
|
|
944
|
-
request,
|
|
945
|
-
env,
|
|
946
|
-
url,
|
|
947
|
-
store,
|
|
948
|
-
actionContinuation,
|
|
949
|
-
);
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
// ============================================================================
|
|
953
|
-
// LOADER FETCH EXECUTION (data fetching with RSC serialization)
|
|
954
|
-
// ============================================================================
|
|
955
|
-
const isLoaderRequest = url.searchParams.has("_rsc_loader");
|
|
956
|
-
if (isLoaderRequest) {
|
|
957
|
-
return handleLoaderFetch(
|
|
958
|
-
handlerCtx,
|
|
959
|
-
request,
|
|
960
|
-
env,
|
|
961
|
-
url,
|
|
962
|
-
variables,
|
|
963
|
-
routeParams,
|
|
964
|
-
);
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
// ============================================================================
|
|
968
|
-
// REGULAR RSC RENDERING (Navigation)
|
|
969
|
-
// ============================================================================
|
|
970
|
-
// Note: Must use "return await" for try/catch to catch async rejections
|
|
971
|
-
return await handleRscRendering(
|
|
972
|
-
handlerCtx,
|
|
973
|
-
request,
|
|
974
|
-
env,
|
|
975
|
-
url,
|
|
976
|
-
isPartial,
|
|
977
|
-
store,
|
|
978
|
-
nonce,
|
|
979
|
-
);
|
|
980
|
-
} catch (error) {
|
|
981
|
-
// Check if middleware/handler returned Response
|
|
982
|
-
if (error instanceof Response) {
|
|
983
|
-
// During partial (client-side navigation), a 200 Response from a handler
|
|
984
|
-
// means the route serves raw content (JSON, text, etc.), not JSX.
|
|
985
|
-
// Signal the browser to hard-navigate so it renders the raw response.
|
|
986
|
-
// Only for 200 — redirects (3xx) work already because the browser follows
|
|
987
|
-
// them automatically to a URL that serves Flight data.
|
|
988
|
-
if (isPartial && error.status === 200) {
|
|
989
|
-
console.warn(
|
|
990
|
-
`[RSC] Route handler at ${url.pathname} returned a Response during client-side navigation. ` +
|
|
991
|
-
`Falling back to hard navigation. Use data-external on the <Link> to avoid the extra round-trip.`,
|
|
992
|
-
);
|
|
993
|
-
return createResponseWithMergedHeaders(null, {
|
|
994
|
-
status: 200,
|
|
995
|
-
headers: {
|
|
996
|
-
"X-RSC-Reload": stripInternalParams(url).toString(),
|
|
997
|
-
"content-type": "text/x-component;charset=utf-8",
|
|
998
|
-
},
|
|
999
|
-
});
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
if (isPartial) {
|
|
1003
|
-
const intercepted = interceptRedirectForPartial(
|
|
1004
|
-
error,
|
|
1005
|
-
createRedirectFlightResponse,
|
|
1006
|
-
);
|
|
1007
|
-
if (intercepted) return intercepted;
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
return error;
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
// Render 404 page for unmatched routes
|
|
1014
|
-
// Check both instanceof and error.name for cross-bundle compatibility
|
|
1015
|
-
const isRouteNotFound =
|
|
1016
|
-
error instanceof RouteNotFoundError ||
|
|
1017
|
-
(error instanceof Error && error.name === "RouteNotFoundError");
|
|
1018
|
-
if (isRouteNotFound) {
|
|
1019
|
-
callOnError(error, "routing", {
|
|
1020
|
-
request,
|
|
1021
|
-
url,
|
|
1022
|
-
env,
|
|
1023
|
-
handledByBoundary: true, // Handled by notFound component
|
|
1024
|
-
});
|
|
1025
|
-
|
|
1026
|
-
// Get notFound component from router options or use default
|
|
1027
|
-
const notFoundOption = router.notFound;
|
|
1028
|
-
const notFoundComponent =
|
|
1029
|
-
typeof notFoundOption === "function"
|
|
1030
|
-
? notFoundOption({ pathname: url.pathname })
|
|
1031
|
-
: (notFoundOption ?? createElement("h1", null, "Not Found"));
|
|
1032
|
-
|
|
1033
|
-
// Create a simple segment for the 404 page
|
|
1034
|
-
const notFoundSegment = {
|
|
1035
|
-
id: "notFound",
|
|
1036
|
-
namespace: "notFound",
|
|
1037
|
-
type: "route" as const,
|
|
1038
|
-
index: 0,
|
|
1039
|
-
component: notFoundComponent,
|
|
1040
|
-
params: {},
|
|
1041
|
-
};
|
|
1042
|
-
|
|
1043
|
-
const payload: RscPayload = {
|
|
1044
|
-
metadata: {
|
|
1045
|
-
pathname: url.pathname,
|
|
1046
|
-
segments: [notFoundSegment],
|
|
1047
|
-
matched: [],
|
|
1048
|
-
diff: [],
|
|
1049
|
-
isPartial: false,
|
|
1050
|
-
rootLayout: router.rootLayout,
|
|
1051
|
-
handles: store.stream(),
|
|
1052
|
-
version,
|
|
1053
|
-
themeConfig: router.themeConfig,
|
|
1054
|
-
warmupEnabled: router.warmupEnabled,
|
|
1055
|
-
initialTheme: requireRequestContext().theme,
|
|
1056
|
-
// No routeName for not-found routes
|
|
1057
|
-
},
|
|
1058
|
-
};
|
|
1059
|
-
|
|
1060
|
-
const debugChannel = requireRequestContext()._debugChannel;
|
|
1061
|
-
const rscStream = renderToReadableStream(payload, {
|
|
1062
|
-
...(debugChannel && { debugChannel }),
|
|
1063
|
-
});
|
|
1064
|
-
|
|
1065
|
-
// Determine if this is an RSC request or HTML request.
|
|
1066
|
-
// Partial requests are always RSC (see main isRscRequest comment).
|
|
1067
|
-
const isRscRequest =
|
|
1068
|
-
isPartial ||
|
|
1069
|
-
(!request.headers.get("accept")?.includes("text/html") &&
|
|
1070
|
-
!url.searchParams.has("__html")) ||
|
|
1071
|
-
url.searchParams.has("__rsc");
|
|
1072
|
-
|
|
1073
|
-
if (isRscRequest) {
|
|
1074
|
-
return createResponseWithMergedHeaders(rscStream, {
|
|
1075
|
-
status: 404,
|
|
1076
|
-
headers: { "content-type": "text/x-component;charset=utf-8" },
|
|
1077
|
-
});
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
// Delegate to SSR for HTML response (reuse early setup if available)
|
|
1081
|
-
const [ssrModule, streamMode] = await getSSRSetup(
|
|
1082
|
-
handlerCtx,
|
|
1083
|
-
request,
|
|
1084
|
-
env,
|
|
1085
|
-
url,
|
|
1086
|
-
requireRequestContext()._metricsStore,
|
|
1087
|
-
);
|
|
1088
|
-
const htmlStream = await ssrModule.renderHTML(rscStream, {
|
|
1089
|
-
nonce,
|
|
1090
|
-
streamMode,
|
|
1091
|
-
});
|
|
1092
|
-
|
|
1093
|
-
return createResponseWithMergedHeaders(htmlStream, {
|
|
1094
|
-
status: 404,
|
|
1095
|
-
headers: { "content-type": "text/html;charset=utf-8" },
|
|
1096
|
-
});
|
|
1097
|
-
}
|
|
1098
|
-
|
|
1099
|
-
// Report unhandled errors
|
|
1100
|
-
callOnError(error, "routing", {
|
|
1101
|
-
request,
|
|
1102
|
-
url,
|
|
1103
|
-
env,
|
|
1104
|
-
handledByBoundary: false,
|
|
1105
|
-
});
|
|
1106
|
-
console.error(`[RSC] Error:`, error);
|
|
1107
|
-
throw error;
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
1236
|
}
|