@rangojs/router 0.0.0-experimental.002d056c
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 +9 -0
- package/README.md +899 -0
- package/dist/bin/rango.js +1606 -0
- package/dist/vite/index.js +5153 -0
- package/package.json +177 -0
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +262 -0
- package/skills/caching/SKILL.md +253 -0
- package/skills/composability/SKILL.md +172 -0
- package/skills/debug-manifest/SKILL.md +112 -0
- package/skills/document-cache/SKILL.md +182 -0
- package/skills/fonts/SKILL.md +167 -0
- package/skills/hooks/SKILL.md +704 -0
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +313 -0
- package/skills/layout/SKILL.md +310 -0
- package/skills/links/SKILL.md +239 -0
- package/skills/loader/SKILL.md +596 -0
- package/skills/middleware/SKILL.md +339 -0
- package/skills/mime-routes/SKILL.md +128 -0
- package/skills/parallel/SKILL.md +305 -0
- package/skills/prerender/SKILL.md +643 -0
- package/skills/rango/SKILL.md +118 -0
- package/skills/response-routes/SKILL.md +411 -0
- package/skills/route/SKILL.md +385 -0
- package/skills/router-setup/SKILL.md +439 -0
- package/skills/tailwind/SKILL.md +129 -0
- package/skills/theme/SKILL.md +79 -0
- package/skills/typesafety/SKILL.md +623 -0
- package/skills/use-cache/SKILL.md +324 -0
- package/src/__internal.ts +273 -0
- package/src/bin/rango.ts +321 -0
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/action-response-classifier.ts +99 -0
- package/src/browser/event-controller.ts +899 -0
- package/src/browser/history-state.ts +80 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/link-interceptor.ts +141 -0
- package/src/browser/logging.ts +55 -0
- package/src/browser/merge-segment-loaders.ts +134 -0
- package/src/browser/navigation-bridge.ts +638 -0
- package/src/browser/navigation-client.ts +261 -0
- package/src/browser/navigation-store.ts +806 -0
- package/src/browser/navigation-transaction.ts +297 -0
- package/src/browser/network-error-handler.ts +61 -0
- package/src/browser/partial-update.ts +582 -0
- package/src/browser/prefetch/cache.ts +206 -0
- package/src/browser/prefetch/fetch.ts +145 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +48 -0
- package/src/browser/prefetch/queue.ts +128 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +368 -0
- package/src/browser/react/NavigationProvider.tsx +413 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +59 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +162 -0
- package/src/browser/react/location-state.ts +107 -0
- package/src/browser/react/mount-context.ts +37 -0
- 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 +218 -0
- package/src/browser/react/use-client-cache.ts +58 -0
- package/src/browser/react/use-handle.ts +162 -0
- package/src/browser/react/use-href.tsx +40 -0
- package/src/browser/react/use-link-status.ts +135 -0
- package/src/browser/react/use-mount.ts +31 -0
- package/src/browser/react/use-navigation.ts +99 -0
- package/src/browser/react/use-params.ts +65 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-router.ts +63 -0
- package/src/browser/react/use-search-params.ts +56 -0
- package/src/browser/react/use-segments.ts +171 -0
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +464 -0
- package/src/browser/scroll-restoration.ts +397 -0
- package/src/browser/segment-reconciler.ts +216 -0
- package/src/browser/segment-structure-assert.ts +83 -0
- package/src/browser/server-action-bridge.ts +667 -0
- package/src/browser/shallow.ts +40 -0
- package/src/browser/types.ts +547 -0
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +438 -0
- package/src/build/generate-route-types.ts +36 -0
- package/src/build/index.ts +35 -0
- package/src/build/route-trie.ts +265 -0
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +98 -0
- package/src/build/route-types/codegen.ts +102 -0
- package/src/build/route-types/include-resolution.ts +411 -0
- package/src/build/route-types/param-extraction.ts +48 -0
- package/src/build/route-types/per-module-writer.ts +128 -0
- package/src/build/route-types/router-processing.ts +479 -0
- package/src/build/route-types/scan-filter.ts +78 -0
- package/src/build/runtime-discovery.ts +231 -0
- 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 +338 -0
- package/src/cache/cache-scope.ts +382 -0
- package/src/cache/cf/cf-cache-store.ts +982 -0
- package/src/cache/cf/index.ts +29 -0
- package/src/cache/document-cache.ts +369 -0
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +41 -0
- package/src/cache/index.ts +44 -0
- package/src/cache/memory-segment-store.ts +328 -0
- package/src/cache/profile-registry.ts +73 -0
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +256 -0
- package/src/cache/taint.ts +98 -0
- package/src/cache/types.ts +342 -0
- package/src/client.rsc.tsx +85 -0
- package/src/client.tsx +601 -0
- package/src/component-utils.ts +76 -0
- package/src/components/DefaultDocument.tsx +27 -0
- package/src/context-var.ts +86 -0
- package/src/debug.ts +243 -0
- package/src/default-error-boundary.tsx +88 -0
- package/src/deps/browser.ts +8 -0
- package/src/deps/html-stream-client.ts +2 -0
- package/src/deps/html-stream-server.ts +2 -0
- package/src/deps/rsc.ts +10 -0
- package/src/deps/ssr.ts +2 -0
- package/src/errors.ts +365 -0
- package/src/handle.ts +135 -0
- package/src/handles/MetaTags.tsx +246 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +7 -0
- package/src/handles/meta.ts +264 -0
- package/src/host/cookie-handler.ts +165 -0
- package/src/host/errors.ts +97 -0
- package/src/host/index.ts +53 -0
- package/src/host/pattern-matcher.ts +214 -0
- package/src/host/router.ts +352 -0
- package/src/host/testing.ts +79 -0
- package/src/host/types.ts +146 -0
- package/src/host/utils.ts +25 -0
- package/src/href-client.ts +222 -0
- package/src/index.rsc.ts +233 -0
- package/src/index.ts +277 -0
- package/src/internal-debug.ts +11 -0
- package/src/loader.rsc.ts +89 -0
- package/src/loader.ts +64 -0
- package/src/network-error-thrower.tsx +23 -0
- package/src/outlet-context.ts +15 -0
- package/src/outlet-provider.tsx +45 -0
- package/src/prerender/param-hash.ts +37 -0
- package/src/prerender/store.ts +185 -0
- package/src/prerender.ts +463 -0
- package/src/reverse.ts +330 -0
- package/src/root-error-boundary.tsx +289 -0
- package/src/route-content-wrapper.tsx +196 -0
- package/src/route-definition/dsl-helpers.ts +934 -0
- package/src/route-definition/helper-factories.ts +200 -0
- package/src/route-definition/helpers-types.ts +430 -0
- package/src/route-definition/index.ts +52 -0
- package/src/route-definition/redirect.ts +93 -0
- package/src/route-definition.ts +1 -0
- package/src/route-map-builder.ts +281 -0
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +259 -0
- package/src/router/content-negotiation.ts +116 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/find-match.ts +160 -0
- package/src/router/handler-context.ts +451 -0
- package/src/router/intercept-resolution.ts +397 -0
- package/src/router/lazy-includes.ts +236 -0
- package/src/router/loader-resolution.ts +420 -0
- package/src/router/logging.ts +251 -0
- package/src/router/manifest.ts +269 -0
- package/src/router/match-api.ts +620 -0
- package/src/router/match-context.ts +266 -0
- package/src/router/match-handlers.ts +440 -0
- package/src/router/match-middleware/background-revalidation.ts +223 -0
- package/src/router/match-middleware/cache-lookup.ts +634 -0
- package/src/router/match-middleware/cache-store.ts +295 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +306 -0
- package/src/router/match-middleware/segment-resolution.ts +193 -0
- package/src/router/match-pipelines.ts +179 -0
- package/src/router/match-result.ts +219 -0
- package/src/router/metrics.ts +282 -0
- package/src/router/middleware-cookies.ts +55 -0
- package/src/router/middleware-types.ts +222 -0
- package/src/router/middleware.ts +749 -0
- package/src/router/pattern-matching.ts +563 -0
- package/src/router/prerender-match.ts +402 -0
- package/src/router/preview-match.ts +170 -0
- package/src/router/revalidation.ts +289 -0
- package/src/router/router-context.ts +320 -0
- package/src/router/router-interfaces.ts +452 -0
- package/src/router/router-options.ts +592 -0
- package/src/router/router-registry.ts +24 -0
- package/src/router/segment-resolution/fresh.ts +570 -0
- package/src/router/segment-resolution/helpers.ts +263 -0
- package/src/router/segment-resolution/loader-cache.ts +198 -0
- package/src/router/segment-resolution/revalidation.ts +1242 -0
- package/src/router/segment-resolution/static-store.ts +67 -0
- package/src/router/segment-resolution.ts +21 -0
- package/src/router/segment-wrappers.ts +291 -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 +239 -0
- package/src/router/types.ts +170 -0
- package/src/router.ts +1006 -0
- package/src/rsc/handler-context.ts +45 -0
- package/src/rsc/handler.ts +1089 -0
- package/src/rsc/helpers.ts +198 -0
- package/src/rsc/index.ts +36 -0
- package/src/rsc/loader-fetch.ts +209 -0
- package/src/rsc/manifest-init.ts +86 -0
- package/src/rsc/nonce.ts +32 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +379 -0
- package/src/rsc/response-error.ts +37 -0
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +237 -0
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +348 -0
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +263 -0
- package/src/search-params.ts +230 -0
- package/src/segment-system.tsx +454 -0
- package/src/server/context.ts +591 -0
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +37 -0
- package/src/server/handle-store.ts +308 -0
- package/src/server/loader-registry.ts +133 -0
- package/src/server/request-context.ts +920 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +51 -0
- package/src/ssr/index.tsx +365 -0
- package/src/static-handler.ts +114 -0
- package/src/theme/ThemeProvider.tsx +297 -0
- package/src/theme/ThemeScript.tsx +61 -0
- package/src/theme/constants.ts +62 -0
- package/src/theme/index.ts +48 -0
- package/src/theme/theme-context.ts +44 -0
- package/src/theme/theme-script.ts +155 -0
- package/src/theme/types.ts +182 -0
- package/src/theme/use-theme.ts +44 -0
- package/src/types/boundaries.ts +158 -0
- package/src/types/cache-types.ts +198 -0
- package/src/types/error-types.ts +192 -0
- package/src/types/global-namespace.ts +100 -0
- package/src/types/handler-context.ts +687 -0
- package/src/types/index.ts +88 -0
- package/src/types/loader-types.ts +183 -0
- package/src/types/route-config.ts +170 -0
- package/src/types/route-entry.ts +109 -0
- package/src/types/segments.ts +148 -0
- package/src/types.ts +1 -0
- package/src/urls/include-helper.ts +197 -0
- package/src/urls/index.ts +53 -0
- package/src/urls/path-helper-types.ts +339 -0
- package/src/urls/path-helper.ts +329 -0
- package/src/urls/pattern-types.ts +95 -0
- package/src/urls/response-types.ts +106 -0
- package/src/urls/type-extraction.ts +372 -0
- package/src/urls/urls-function.ts +98 -0
- package/src/urls.ts +1 -0
- package/src/use-loader.tsx +354 -0
- package/src/vite/discovery/bundle-postprocess.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +344 -0
- package/src/vite/discovery/prerender-collection.ts +385 -0
- package/src/vite/discovery/route-types-writer.ts +258 -0
- package/src/vite/discovery/self-gen-tracking.ts +47 -0
- package/src/vite/discovery/state.ts +108 -0
- package/src/vite/discovery/virtual-module-codegen.ts +203 -0
- package/src/vite/index.ts +16 -0
- package/src/vite/plugin-types.ts +48 -0
- package/src/vite/plugins/cjs-to-esm.ts +93 -0
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +105 -0
- package/src/vite/plugins/expose-action-id.ts +363 -0
- package/src/vite/plugins/expose-id-utils.ts +287 -0
- package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +179 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +569 -0
- package/src/vite/plugins/refresh-cmd.ts +65 -0
- package/src/vite/plugins/use-cache-transform.ts +323 -0
- package/src/vite/plugins/version-injector.ts +83 -0
- package/src/vite/plugins/version-plugin.ts +266 -0
- package/src/vite/plugins/version.d.ts +12 -0
- package/src/vite/plugins/virtual-entries.ts +123 -0
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +445 -0
- package/src/vite/router-discovery.ts +777 -0
- package/src/vite/utils/ast-handler-extract.ts +517 -0
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +137 -0
- package/src/vite/utils/manifest-utils.ts +70 -0
- package/src/vite/utils/package-resolution.ts +121 -0
- package/src/vite/utils/prerender-utils.ts +189 -0
- package/src/vite/utils/shared-utils.ts +169 -0
|
@@ -0,0 +1,934 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
PartialCacheOptions,
|
|
4
|
+
Handler,
|
|
5
|
+
LoaderDefinition,
|
|
6
|
+
MiddlewareFn,
|
|
7
|
+
ShouldRevalidateFn,
|
|
8
|
+
TransitionConfig,
|
|
9
|
+
} from "../types.js";
|
|
10
|
+
import {
|
|
11
|
+
getContext,
|
|
12
|
+
getNamePrefix,
|
|
13
|
+
getUrlPrefix,
|
|
14
|
+
type EntryData,
|
|
15
|
+
type InterceptEntry,
|
|
16
|
+
} from "../server/context";
|
|
17
|
+
import { invariant } from "../errors";
|
|
18
|
+
import { isCachedFunction } from "../cache/taint.js";
|
|
19
|
+
import { RSCRouterContext } from "../server/context";
|
|
20
|
+
import { isStaticHandler } from "../static-handler.js";
|
|
21
|
+
import RootLayout from "../server/root-layout";
|
|
22
|
+
import type {
|
|
23
|
+
AllUseItems,
|
|
24
|
+
RouteItem,
|
|
25
|
+
ParallelItem,
|
|
26
|
+
InterceptItem,
|
|
27
|
+
MiddlewareItem,
|
|
28
|
+
RevalidateItem,
|
|
29
|
+
LoaderItem,
|
|
30
|
+
LoadingItem,
|
|
31
|
+
ErrorBoundaryItem,
|
|
32
|
+
NotFoundBoundaryItem,
|
|
33
|
+
LayoutItem,
|
|
34
|
+
WhenItem,
|
|
35
|
+
CacheItem,
|
|
36
|
+
TransitionItem,
|
|
37
|
+
UseItems,
|
|
38
|
+
} from "../route-types.js";
|
|
39
|
+
import type { RouteHelpers } from "./helpers-types.js";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Check if an item contains routes (directly or inside nested structures like cache).
|
|
43
|
+
* Used to determine if a layout or cache should be treated as an orphan.
|
|
44
|
+
*/
|
|
45
|
+
const hasRoutesInItem = (item: AllUseItems): boolean => {
|
|
46
|
+
if (item.type === "route") return true;
|
|
47
|
+
// Lazy includes contain deferred routes — treat them as having routes
|
|
48
|
+
// to prevent the parent layout from being misclassified as orphan,
|
|
49
|
+
// which would clear its parent pointer and break the middleware chain.
|
|
50
|
+
if (item.type === "include") return true;
|
|
51
|
+
if (item.type === "cache" && item.uses) {
|
|
52
|
+
return item.uses.some((child) => hasRoutesInItem(child));
|
|
53
|
+
}
|
|
54
|
+
if (item.type === "layout" && item.uses) {
|
|
55
|
+
return item.uses.some((child) => hasRoutesInItem(child));
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const revalidate: RouteHelpers<any, any>["revalidate"] = (fn) => {
|
|
61
|
+
const ctx = getContext().getStore();
|
|
62
|
+
if (!ctx) throw new Error("revalidate() must be called inside map()");
|
|
63
|
+
|
|
64
|
+
// Attach to last entry in stack
|
|
65
|
+
const parent = ctx.parent;
|
|
66
|
+
if (!parent || !("revalidate" in parent)) {
|
|
67
|
+
invariant(false, "No parent entry available for revalidate()");
|
|
68
|
+
}
|
|
69
|
+
const name = `$${getContext().getNextIndex("revalidate")}`;
|
|
70
|
+
parent.revalidate.push(fn);
|
|
71
|
+
return { name, type: "revalidate" } as RevalidateItem;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Error boundary helper - attaches an error fallback to the current entry
|
|
76
|
+
*
|
|
77
|
+
* When an error occurs during rendering of this segment or its children,
|
|
78
|
+
* the fallback will be rendered instead. The fallback can be:
|
|
79
|
+
* - A static ReactNode (e.g., <ErrorPage />)
|
|
80
|
+
* - A handler function that receives error info and reset function
|
|
81
|
+
*
|
|
82
|
+
* Error boundaries catch errors from:
|
|
83
|
+
* - Middleware execution
|
|
84
|
+
* - Loader execution
|
|
85
|
+
* - Handler/component rendering
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* layout(<ShopLayout />, () => [
|
|
90
|
+
* errorBoundary(<ShopErrorFallback />),
|
|
91
|
+
* route("products.detail", ProductDetail),
|
|
92
|
+
* ])
|
|
93
|
+
*
|
|
94
|
+
* // Or with handler for dynamic error UI:
|
|
95
|
+
* route("products.detail", ProductDetail, () => [
|
|
96
|
+
* errorBoundary(({ error, reset }) => (
|
|
97
|
+
* <div>
|
|
98
|
+
* <h2>Product failed to load</h2>
|
|
99
|
+
* <p>{error.message}</p>
|
|
100
|
+
* <button onClick={reset}>Retry</button>
|
|
101
|
+
* </div>
|
|
102
|
+
* )),
|
|
103
|
+
* ])
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
const errorBoundary: RouteHelpers<any, any>["errorBoundary"] = (fallback) => {
|
|
107
|
+
const ctx = getContext().getStore();
|
|
108
|
+
if (!ctx) throw new Error("errorBoundary() must be called inside map()");
|
|
109
|
+
|
|
110
|
+
// Attach to parent entry in stack
|
|
111
|
+
const parent = ctx.parent;
|
|
112
|
+
if (!parent || !("errorBoundary" in parent)) {
|
|
113
|
+
invariant(false, "No parent entry available for errorBoundary()");
|
|
114
|
+
}
|
|
115
|
+
const name = `$${getContext().getNextIndex("errorBoundary")}`;
|
|
116
|
+
parent.errorBoundary.push(fallback);
|
|
117
|
+
return { name, type: "errorBoundary" } as ErrorBoundaryItem;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* NotFound boundary helper - attaches a not-found fallback to the current entry
|
|
122
|
+
*
|
|
123
|
+
* When a DataNotFoundError is thrown (via notFound()) during rendering of this
|
|
124
|
+
* segment or its children, the fallback will be rendered instead. The fallback can be:
|
|
125
|
+
* - A static ReactNode (e.g., <ProductNotFound />)
|
|
126
|
+
* - A handler function that receives not found info
|
|
127
|
+
*
|
|
128
|
+
* NotFound boundaries catch DataNotFoundError from:
|
|
129
|
+
* - Loader execution
|
|
130
|
+
* - Handler/component rendering
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* layout(<ShopLayout />, () => [
|
|
135
|
+
* notFoundBoundary(<ProductNotFound />),
|
|
136
|
+
* route("products.detail", ProductDetail),
|
|
137
|
+
* ])
|
|
138
|
+
*
|
|
139
|
+
* // Or with handler for dynamic not found UI:
|
|
140
|
+
* route("products.detail", ProductDetail, () => [
|
|
141
|
+
* notFoundBoundary(({ notFound }) => (
|
|
142
|
+
* <div>
|
|
143
|
+
* <h2>Product not found</h2>
|
|
144
|
+
* <p>{notFound.message}</p>
|
|
145
|
+
* <a href="/products">Browse all products</a>
|
|
146
|
+
* </div>
|
|
147
|
+
* )),
|
|
148
|
+
* ])
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
const notFoundBoundary: RouteHelpers<any, any>["notFoundBoundary"] = (
|
|
152
|
+
fallback,
|
|
153
|
+
) => {
|
|
154
|
+
const ctx = getContext().getStore();
|
|
155
|
+
if (!ctx) throw new Error("notFoundBoundary() must be called inside map()");
|
|
156
|
+
|
|
157
|
+
// Attach to parent entry in stack
|
|
158
|
+
const parent = ctx.parent;
|
|
159
|
+
if (!parent || !("notFoundBoundary" in parent)) {
|
|
160
|
+
invariant(false, "No parent entry available for notFoundBoundary()");
|
|
161
|
+
}
|
|
162
|
+
const name = `$${getContext().getNextIndex("notFoundBoundary")}`;
|
|
163
|
+
parent.notFoundBoundary.push(fallback);
|
|
164
|
+
return { name, type: "notFoundBoundary" } as NotFoundBoundaryItem;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* When helper - defines a condition for intercept activation
|
|
169
|
+
*
|
|
170
|
+
* Only valid inside intercept() use() callback. The when() function
|
|
171
|
+
* is captured by the intercept and stored in its `when` array.
|
|
172
|
+
* During soft navigation, all when() conditions must return true
|
|
173
|
+
* for the intercept to activate.
|
|
174
|
+
*/
|
|
175
|
+
const when: RouteHelpers<any, any>["when"] = (fn) => {
|
|
176
|
+
const ctx = getContext().getStore();
|
|
177
|
+
if (!ctx) throw new Error("when() must be called inside intercept()");
|
|
178
|
+
|
|
179
|
+
// The when() function needs to be captured by the intercept's tempParent
|
|
180
|
+
// which should have a `when` array. If not present, we're not inside intercept()
|
|
181
|
+
const parent = ctx.parent as any;
|
|
182
|
+
if (!parent || !("when" in parent)) {
|
|
183
|
+
invariant(
|
|
184
|
+
false,
|
|
185
|
+
"when() can only be used inside intercept() use() callback",
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const name = `$${getContext().getNextIndex("when")}`;
|
|
190
|
+
parent.when.push(fn);
|
|
191
|
+
return { name, type: "when" } as WhenItem;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Cache helper - defines caching configuration for segments
|
|
196
|
+
*
|
|
197
|
+
* Creates a cache boundary that applies to all children unless overridden.
|
|
198
|
+
* When used without children, attaches cache config to the parent entry
|
|
199
|
+
* (e.g., for loader-specific caching).
|
|
200
|
+
*
|
|
201
|
+
* Supports these call signatures:
|
|
202
|
+
* - cache() - no args, uses app-level defaults (for loader caching)
|
|
203
|
+
* - cache(() => [...]) - wraps children with app-level defaults
|
|
204
|
+
* - cache('profileName') - uses a named cache profile
|
|
205
|
+
* - cache('profileName', () => [...]) - named profile with children
|
|
206
|
+
* - cache({ ttl: 60 }, () => [...]) - with explicit options
|
|
207
|
+
*/
|
|
208
|
+
const cache: RouteHelpers<any, any>["cache"] = (
|
|
209
|
+
optionsOrChildren?:
|
|
210
|
+
| PartialCacheOptions
|
|
211
|
+
| false
|
|
212
|
+
| string
|
|
213
|
+
| (() => UseItems<AllUseItems>),
|
|
214
|
+
maybeChildren?: () => UseItems<AllUseItems>,
|
|
215
|
+
) => {
|
|
216
|
+
const store = getContext();
|
|
217
|
+
const ctx = store.getStore();
|
|
218
|
+
if (!ctx) throw new Error("cache() must be called inside map()");
|
|
219
|
+
|
|
220
|
+
// Handle overloaded signature
|
|
221
|
+
let options: PartialCacheOptions | false;
|
|
222
|
+
let children: (() => UseItems<AllUseItems>) | undefined;
|
|
223
|
+
|
|
224
|
+
if (optionsOrChildren === undefined) {
|
|
225
|
+
// cache() - no args, use defaults
|
|
226
|
+
options = {};
|
|
227
|
+
children = undefined;
|
|
228
|
+
} else if (typeof optionsOrChildren === "string") {
|
|
229
|
+
// cache('profileName') or cache('profileName', () => [...])
|
|
230
|
+
// Resolve from context-scoped profiles (set per-router via HelperContext).
|
|
231
|
+
const ctxStore = RSCRouterContext.getStore();
|
|
232
|
+
const profile = ctxStore?.cacheProfiles?.[optionsOrChildren];
|
|
233
|
+
invariant(
|
|
234
|
+
profile,
|
|
235
|
+
`cache("${optionsOrChildren}"): unknown cache profile. ` +
|
|
236
|
+
`Define it in createRouter({ cacheProfiles: { "${optionsOrChildren}": { ttl: ... } } }).`,
|
|
237
|
+
);
|
|
238
|
+
options = { ttl: profile.ttl, swr: profile.swr, tags: profile.tags };
|
|
239
|
+
children = maybeChildren;
|
|
240
|
+
} else if (typeof optionsOrChildren === "function") {
|
|
241
|
+
// cache(() => [...]) - use empty options (will use defaults)
|
|
242
|
+
options = {};
|
|
243
|
+
children = optionsOrChildren;
|
|
244
|
+
} else {
|
|
245
|
+
// cache(options, children) - explicit options
|
|
246
|
+
options = optionsOrChildren;
|
|
247
|
+
children = maybeChildren;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Allocate a single index for this cache() call (used in all paths)
|
|
251
|
+
const cacheIndex = store.getNextIndex("cache");
|
|
252
|
+
const name = `$${cacheIndex}`;
|
|
253
|
+
const cacheConfig = { options };
|
|
254
|
+
|
|
255
|
+
// If no children, create an orphan cache entry (like orphan layouts)
|
|
256
|
+
// This allows cache() to wrap subsequent siblings
|
|
257
|
+
if (!children) {
|
|
258
|
+
const parent = ctx.parent as any;
|
|
259
|
+
|
|
260
|
+
// Check if we're inside a loader() use() callback - special case for loader caching
|
|
261
|
+
if (parent && parent.type === "loader") {
|
|
262
|
+
// Direct assignment to loader entry's cache field
|
|
263
|
+
parent.cache = cacheConfig;
|
|
264
|
+
return { name, type: "cache" } as CacheItem;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Create orphan cache entry (like orphan layout)
|
|
268
|
+
// Subsequent siblings in the same array will attach to this entry
|
|
269
|
+
const namespace = `${ctx.namespace}.${cacheIndex}`;
|
|
270
|
+
const cacheUrlPrefix = getUrlPrefix();
|
|
271
|
+
|
|
272
|
+
const entry = {
|
|
273
|
+
id: namespace,
|
|
274
|
+
shortCode: store.getShortCode("cache"),
|
|
275
|
+
type: "cache",
|
|
276
|
+
parent: parent, // link to current parent for hierarchy
|
|
277
|
+
cache: cacheConfig,
|
|
278
|
+
handler: RootLayout,
|
|
279
|
+
loading: undefined, // Allow loading() to attach loading state
|
|
280
|
+
middleware: [],
|
|
281
|
+
revalidate: [],
|
|
282
|
+
errorBoundary: [],
|
|
283
|
+
notFoundBoundary: [],
|
|
284
|
+
layout: [],
|
|
285
|
+
parallel: [],
|
|
286
|
+
intercept: [],
|
|
287
|
+
loader: [],
|
|
288
|
+
...(cacheUrlPrefix ? { mountPath: cacheUrlPrefix } : {}),
|
|
289
|
+
} as EntryData;
|
|
290
|
+
|
|
291
|
+
// Attach to parent's layout array (cache entries are structural like layouts)
|
|
292
|
+
if (parent && "layout" in parent) {
|
|
293
|
+
parent.layout.push(entry);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Update context parent so subsequent siblings attach to this cache entry
|
|
297
|
+
// This makes cache() act as sugar for cache(() => [...])
|
|
298
|
+
ctx.parent = entry;
|
|
299
|
+
|
|
300
|
+
return { name: namespace, type: "cache" } as CacheItem;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// With children: create a cache entry (like layout with caching semantics)
|
|
304
|
+
const namespace = `${ctx.namespace}.${cacheIndex}`;
|
|
305
|
+
const cacheShortCode = store.getShortCode("cache");
|
|
306
|
+
|
|
307
|
+
const cacheUrlPrefix2 = getUrlPrefix();
|
|
308
|
+
|
|
309
|
+
const entry = {
|
|
310
|
+
id: namespace,
|
|
311
|
+
shortCode: cacheShortCode,
|
|
312
|
+
type: "cache",
|
|
313
|
+
parent: ctx.parent,
|
|
314
|
+
cache: cacheConfig,
|
|
315
|
+
// Cache entries render like layouts (with Outlet as default handler)
|
|
316
|
+
handler: RootLayout, // RootLayout just renders <Outlet />
|
|
317
|
+
loading: undefined, // Allow loading() to attach loading state
|
|
318
|
+
middleware: [],
|
|
319
|
+
revalidate: [],
|
|
320
|
+
errorBoundary: [],
|
|
321
|
+
notFoundBoundary: [],
|
|
322
|
+
layout: [],
|
|
323
|
+
parallel: [],
|
|
324
|
+
intercept: [],
|
|
325
|
+
loader: [],
|
|
326
|
+
...(cacheUrlPrefix2 ? { mountPath: cacheUrlPrefix2 } : {}),
|
|
327
|
+
} as EntryData;
|
|
328
|
+
|
|
329
|
+
// Run children with cache entry as parent
|
|
330
|
+
const result = store.run(namespace, entry, children)?.flat(3);
|
|
331
|
+
|
|
332
|
+
invariant(
|
|
333
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
334
|
+
`cache() children callback must return an array of use items [${namespace}]`,
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
// Check if this cache has routes (including nested caches/layouts)
|
|
338
|
+
const hasRoutes =
|
|
339
|
+
result &&
|
|
340
|
+
Array.isArray(result) &&
|
|
341
|
+
result.some((item) => hasRoutesInItem(item));
|
|
342
|
+
|
|
343
|
+
if (!hasRoutes) {
|
|
344
|
+
const parent = ctx.parent;
|
|
345
|
+
if (parent && "layout" in parent) {
|
|
346
|
+
// Attach to parent's layout array (cache entries are structural like layouts)
|
|
347
|
+
entry.parent = null;
|
|
348
|
+
parent.layout.push(entry);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return { name: namespace, type: "cache", uses: result } as CacheItem;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const middleware: RouteHelpers<any, any>["middleware"] = (...fn) => {
|
|
356
|
+
// Prevent "use cache" functions from being used as middleware.
|
|
357
|
+
// Checked before context validation — this is a static invariant.
|
|
358
|
+
for (const f of fn) {
|
|
359
|
+
if (isCachedFunction(f)) {
|
|
360
|
+
throw new Error(
|
|
361
|
+
`A "use cache" function cannot be used as middleware. ` +
|
|
362
|
+
`Cached functions return data and do not participate in the ` +
|
|
363
|
+
`middleware chain. Remove the "use cache" directive or use a ` +
|
|
364
|
+
`regular middleware function instead.`,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const ctx = getContext().getStore();
|
|
370
|
+
if (!ctx) throw new Error("middleware() must be called inside map()");
|
|
371
|
+
|
|
372
|
+
// Attach to last entry in stack
|
|
373
|
+
const parent = ctx.parent;
|
|
374
|
+
if (!parent || !("middleware" in parent)) {
|
|
375
|
+
invariant(false, "No parent entry available for middleware()");
|
|
376
|
+
}
|
|
377
|
+
const name = `$${getContext().getNextIndex("middleware")}`;
|
|
378
|
+
parent.middleware.push(...fn);
|
|
379
|
+
return { name, type: "middleware" } as MiddlewareItem;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
|
|
383
|
+
const store = getContext();
|
|
384
|
+
const ctx = store.getStore();
|
|
385
|
+
if (!ctx) throw new Error("parallel() must be called inside map()");
|
|
386
|
+
|
|
387
|
+
if (!ctx.parent || !ctx.parent?.parallel) {
|
|
388
|
+
invariant(false, "No parent entry available for parallel()");
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
invariant(
|
|
392
|
+
ctx.parent.type !== "parallel",
|
|
393
|
+
"parallel() cannot be nested inside another parallel()",
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
const namespace = `${ctx.namespace}.$${store.getNextIndex("parallel")}`;
|
|
397
|
+
|
|
398
|
+
// Unwrap any static handler definitions in parallel slots
|
|
399
|
+
const unwrappedSlots: Record<string, any> = {};
|
|
400
|
+
let hasStaticSlot = false;
|
|
401
|
+
const staticSlotIds: Record<string, string> = {};
|
|
402
|
+
for (const [slotName, slotHandler] of Object.entries(
|
|
403
|
+
slots as Record<string, any>,
|
|
404
|
+
)) {
|
|
405
|
+
if (isStaticHandler(slotHandler)) {
|
|
406
|
+
hasStaticSlot = true;
|
|
407
|
+
unwrappedSlots[slotName] = slotHandler.handler;
|
|
408
|
+
if (slotHandler.$$id) {
|
|
409
|
+
staticSlotIds[slotName] = slotHandler.$$id;
|
|
410
|
+
// Capture namespace prefix for build-time reverse() resolution
|
|
411
|
+
if (ctx.namePrefix) {
|
|
412
|
+
(slotHandler as any).$$routePrefix = ctx.namePrefix;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
unwrappedSlots[slotName] = slotHandler;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Create full EntryData for parallel with its own loaders/revalidate/loading
|
|
421
|
+
const parallelUrlPrefix = getUrlPrefix();
|
|
422
|
+
const entry = {
|
|
423
|
+
id: namespace,
|
|
424
|
+
shortCode: store.getShortCode("parallel"),
|
|
425
|
+
type: "parallel",
|
|
426
|
+
parent: null, // Parallels don't participate in parent chain traversal
|
|
427
|
+
handler: unwrappedSlots,
|
|
428
|
+
loading: undefined, // Allow loading() to attach loading state
|
|
429
|
+
middleware: [],
|
|
430
|
+
revalidate: [],
|
|
431
|
+
errorBoundary: [],
|
|
432
|
+
notFoundBoundary: [],
|
|
433
|
+
layout: [],
|
|
434
|
+
parallel: [],
|
|
435
|
+
intercept: [],
|
|
436
|
+
loader: [],
|
|
437
|
+
...(parallelUrlPrefix ? { mountPath: parallelUrlPrefix } : {}),
|
|
438
|
+
...(hasStaticSlot
|
|
439
|
+
? {
|
|
440
|
+
isStaticPrerender: true as const,
|
|
441
|
+
...(Object.keys(staticSlotIds).length > 0
|
|
442
|
+
? { staticHandlerIds: staticSlotIds }
|
|
443
|
+
: {}),
|
|
444
|
+
}
|
|
445
|
+
: {}),
|
|
446
|
+
} satisfies EntryData;
|
|
447
|
+
|
|
448
|
+
// Run use callback if provided to collect loaders, revalidate, loading
|
|
449
|
+
if (use && typeof use === "function") {
|
|
450
|
+
const result = store.run(namespace, entry, use)?.flat(3);
|
|
451
|
+
invariant(
|
|
452
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
453
|
+
`parallel() use() callback must return an array of use items [${namespace}]`,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
ctx.parent.parallel.push(entry);
|
|
458
|
+
return { name: namespace, type: "parallel" } as ParallelItem;
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Intercept helper - defines an intercepting route for soft navigation
|
|
463
|
+
*/
|
|
464
|
+
const intercept = (
|
|
465
|
+
slotName: `@${string}`,
|
|
466
|
+
routeName: string,
|
|
467
|
+
handler: any,
|
|
468
|
+
use?: () => any[],
|
|
469
|
+
) => {
|
|
470
|
+
const store = getContext();
|
|
471
|
+
const ctx = store.getStore();
|
|
472
|
+
if (!ctx) throw new Error("intercept() must be called inside map()");
|
|
473
|
+
|
|
474
|
+
if (!ctx.parent || !ctx.parent?.intercept) {
|
|
475
|
+
invariant(false, "No parent entry available for intercept()");
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
invariant(
|
|
479
|
+
ctx.parent.type !== "parallel",
|
|
480
|
+
"intercept() cannot be used inside parallel()",
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
const namespace = `${ctx.namespace}.$${store.getNextIndex("intercept")}.${slotName}`;
|
|
484
|
+
|
|
485
|
+
// Dot-prefixed = local (add include prefix), unprefixed = global (use as-is)
|
|
486
|
+
const isLocal = typeof routeName === "string" && routeName.startsWith(".");
|
|
487
|
+
const bareRouteName = isLocal ? routeName.slice(1) : routeName;
|
|
488
|
+
const namePrefix = getNamePrefix();
|
|
489
|
+
const prefixedRouteName =
|
|
490
|
+
isLocal && namePrefix ? `${namePrefix}.${bareRouteName}` : bareRouteName;
|
|
491
|
+
|
|
492
|
+
// Create intercept entry with its own loaders/revalidate/middleware/when
|
|
493
|
+
const entry: InterceptEntry = {
|
|
494
|
+
slotName: slotName as `@${string}`,
|
|
495
|
+
routeName: prefixedRouteName,
|
|
496
|
+
handler,
|
|
497
|
+
middleware: [],
|
|
498
|
+
revalidate: [],
|
|
499
|
+
errorBoundary: [],
|
|
500
|
+
notFoundBoundary: [],
|
|
501
|
+
loader: [],
|
|
502
|
+
when: [], // Selector conditions for conditional interception
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// Run use callback if provided to collect loaders, revalidate, middleware, etc.
|
|
506
|
+
if (use && typeof use === "function") {
|
|
507
|
+
// Create a temporary parent context for the use() callback
|
|
508
|
+
// so that middleware, loader, revalidate attach to the intercept entry
|
|
509
|
+
const originalParent = ctx.parent;
|
|
510
|
+
|
|
511
|
+
// Capture layouts in a temporary array
|
|
512
|
+
const capturedLayouts: EntryData[] = [];
|
|
513
|
+
|
|
514
|
+
const tempParent = {
|
|
515
|
+
...originalParent,
|
|
516
|
+
middleware: entry.middleware,
|
|
517
|
+
revalidate: entry.revalidate,
|
|
518
|
+
errorBoundary: entry.errorBoundary,
|
|
519
|
+
notFoundBoundary: entry.notFoundBoundary,
|
|
520
|
+
loader: entry.loader,
|
|
521
|
+
layout: capturedLayouts, // Capture layout() calls
|
|
522
|
+
when: entry.when, // Capture when() conditions
|
|
523
|
+
// Use getter/setter to capture loading on the entry
|
|
524
|
+
get loading() {
|
|
525
|
+
return entry.loading;
|
|
526
|
+
},
|
|
527
|
+
set loading(value: ReactNode | false | undefined) {
|
|
528
|
+
entry.loading = value;
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
ctx.parent = tempParent as EntryData;
|
|
532
|
+
|
|
533
|
+
const result = use()?.flat(3);
|
|
534
|
+
|
|
535
|
+
// Restore original parent
|
|
536
|
+
ctx.parent = originalParent;
|
|
537
|
+
|
|
538
|
+
// Extract layout from captured layouts (use first one if multiple)
|
|
539
|
+
// Layout inside intercept should always be ReactNode or Handler, not Record slots
|
|
540
|
+
if (capturedLayouts.length > 0 && capturedLayouts[0].type === "layout") {
|
|
541
|
+
entry.layout = capturedLayouts[0].handler as
|
|
542
|
+
| ReactNode
|
|
543
|
+
| Handler<any, any, any>;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
invariant(
|
|
547
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
548
|
+
`intercept() use() callback must return an array of use items [${namespace}]`,
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
ctx.parent.intercept.push(entry);
|
|
553
|
+
return { name: namespace, type: "intercept" } as InterceptItem;
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Loader helper - attaches a loader to the current entry
|
|
558
|
+
*/
|
|
559
|
+
const loaderFn: RouteHelpers<any, any>["loader"] = (loaderDef, use) => {
|
|
560
|
+
const store = getContext();
|
|
561
|
+
const ctx = store.getStore();
|
|
562
|
+
if (!ctx) throw new Error("loader() must be called inside map()");
|
|
563
|
+
|
|
564
|
+
// Attach to last entry in stack
|
|
565
|
+
if (!ctx.parent || !ctx.parent?.loader) {
|
|
566
|
+
invariant(false, "No parent entry available for loader()");
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
const name = `${ctx.namespace}.$${store.getNextIndex("loader")}`;
|
|
570
|
+
|
|
571
|
+
// Create loader entry with empty revalidate array
|
|
572
|
+
const loaderEntry = {
|
|
573
|
+
loader: loaderDef,
|
|
574
|
+
revalidate: [] as ShouldRevalidateFn<any, any>[],
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
// If use() callback provided, run it to collect revalidation rules and cache config
|
|
578
|
+
if (use && typeof use === "function") {
|
|
579
|
+
// Temporarily set context for revalidate()/cache() calls to target this loader
|
|
580
|
+
const originalParent = ctx.parent;
|
|
581
|
+
// Create a temporary "parent" with type "loader" so cache() can detect it.
|
|
582
|
+
// Save existing .cache to distinguish inherited config from newly set config.
|
|
583
|
+
const parentCache = (originalParent as any).cache;
|
|
584
|
+
const tempParent = {
|
|
585
|
+
...originalParent,
|
|
586
|
+
type: "loader",
|
|
587
|
+
revalidate: loaderEntry.revalidate,
|
|
588
|
+
};
|
|
589
|
+
ctx.parent = tempParent as EntryData;
|
|
590
|
+
|
|
591
|
+
const result = use()?.flat(3);
|
|
592
|
+
|
|
593
|
+
// Copy cache config only if cache() was called during the use() callback.
|
|
594
|
+
// The spread from originalParent may carry an inherited .cache from
|
|
595
|
+
// a parent cache() boundary — only copy if it was newly set.
|
|
596
|
+
if (
|
|
597
|
+
(tempParent as any).cache &&
|
|
598
|
+
(tempParent as any).cache !== parentCache
|
|
599
|
+
) {
|
|
600
|
+
(loaderEntry as any).cache = (tempParent as any).cache;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// Restore original parent
|
|
604
|
+
ctx.parent = originalParent;
|
|
605
|
+
|
|
606
|
+
invariant(
|
|
607
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
608
|
+
`loader() use() callback must return an array of use items [${name}]`,
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
ctx.parent.loader.push(loaderEntry);
|
|
613
|
+
return { name, type: "loader" } as LoaderItem;
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Loading helper - attaches a loading component to the current entry
|
|
618
|
+
* Loading components are static (no context) and shown during navigation
|
|
619
|
+
*/
|
|
620
|
+
const loadingFn: RouteHelpers<any, any>["loading"] = (component, options) => {
|
|
621
|
+
const store = getContext();
|
|
622
|
+
const ctx = store.getStore();
|
|
623
|
+
if (!ctx) throw new Error("loading() must be called inside map()");
|
|
624
|
+
|
|
625
|
+
const parent = ctx.parent;
|
|
626
|
+
if (!parent || !("loading" in parent)) {
|
|
627
|
+
invariant(false, "No parent entry available for loading()");
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// If ssr: false and we're in SSR, set loading to false
|
|
631
|
+
if (options?.ssr === false && ctx.isSSR) {
|
|
632
|
+
parent.loading = false;
|
|
633
|
+
} else {
|
|
634
|
+
parent.loading = component;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const name = `$${store.getNextIndex("loading")}`;
|
|
638
|
+
return { name, type: "loading" } as LoadingItem;
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Transition helper - attaches a ViewTransition config to the current entry
|
|
643
|
+
* or wraps a group of routes in a transparent layout with ViewTransition
|
|
644
|
+
*/
|
|
645
|
+
const transitionFn = (
|
|
646
|
+
configOrChildren?: TransitionConfig | (() => UseItems<AllUseItems>),
|
|
647
|
+
maybeChildren?: () => UseItems<AllUseItems>,
|
|
648
|
+
): TransitionItem => {
|
|
649
|
+
// Resolve overloaded arguments:
|
|
650
|
+
// transition() -> config={}, children=undefined
|
|
651
|
+
// transition(config) -> config=config, children=undefined
|
|
652
|
+
// transition(children) -> config={}, children=children
|
|
653
|
+
// transition(config, children) -> config=config, children=children
|
|
654
|
+
const config: TransitionConfig =
|
|
655
|
+
typeof configOrChildren === "function" ? {} : (configOrChildren ?? {});
|
|
656
|
+
const children: (() => UseItems<AllUseItems>) | undefined =
|
|
657
|
+
typeof configOrChildren === "function" ? configOrChildren : maybeChildren;
|
|
658
|
+
|
|
659
|
+
const store = getContext();
|
|
660
|
+
const ctx = store.getStore();
|
|
661
|
+
if (!ctx) throw new Error("transition() must be called inside map()");
|
|
662
|
+
|
|
663
|
+
const name = `$${store.getNextIndex("transition")}`;
|
|
664
|
+
|
|
665
|
+
if (!children) {
|
|
666
|
+
// Position 1: child of path() — attach to parent entry
|
|
667
|
+
const parent = ctx.parent;
|
|
668
|
+
if (!parent || !("loading" in parent)) {
|
|
669
|
+
invariant(false, "No parent entry available for transition()");
|
|
670
|
+
}
|
|
671
|
+
parent.transition = config;
|
|
672
|
+
return { name, type: "transition" } as TransitionItem;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Position 2: wrapper — create a transparent layout with transition config
|
|
676
|
+
const namespace = `${ctx.namespace}.${store.getNextIndex("transition")}`;
|
|
677
|
+
const entry = {
|
|
678
|
+
id: namespace,
|
|
679
|
+
shortCode: store.getShortCode("layout"),
|
|
680
|
+
type: "layout",
|
|
681
|
+
parent: ctx.parent,
|
|
682
|
+
handler: RootLayout,
|
|
683
|
+
loading: undefined,
|
|
684
|
+
transition: config,
|
|
685
|
+
middleware: [],
|
|
686
|
+
revalidate: [],
|
|
687
|
+
errorBoundary: [],
|
|
688
|
+
notFoundBoundary: [],
|
|
689
|
+
layout: [],
|
|
690
|
+
parallel: [],
|
|
691
|
+
intercept: [],
|
|
692
|
+
loader: [],
|
|
693
|
+
} as EntryData;
|
|
694
|
+
|
|
695
|
+
const result = store.run(namespace, entry, children)?.flat(3);
|
|
696
|
+
|
|
697
|
+
invariant(
|
|
698
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
699
|
+
`transition() children callback must return an array of use items [${namespace}]`,
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
const hasRoutes =
|
|
703
|
+
result &&
|
|
704
|
+
Array.isArray(result) &&
|
|
705
|
+
result.some((item) => hasRoutesInItem(item));
|
|
706
|
+
|
|
707
|
+
if (!hasRoutes) {
|
|
708
|
+
const parent = ctx.parent;
|
|
709
|
+
if (parent && "layout" in parent) {
|
|
710
|
+
entry.parent = null;
|
|
711
|
+
parent.layout.push(entry);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
return { name: namespace, type: "transition" } as TransitionItem;
|
|
716
|
+
};
|
|
717
|
+
|
|
718
|
+
const routeFn: RouteHelpers<any, any>["route"] = (name, handler, use) => {
|
|
719
|
+
const store = getContext();
|
|
720
|
+
const ctx = store.getStore();
|
|
721
|
+
if (!ctx) throw new Error("route() must be called inside map()");
|
|
722
|
+
|
|
723
|
+
const namespace = `${ctx.namespace}.${store.getNextIndex("route")}.${name}`;
|
|
724
|
+
|
|
725
|
+
const entry = {
|
|
726
|
+
id: namespace,
|
|
727
|
+
shortCode: store.getShortCode("route"),
|
|
728
|
+
type: "route",
|
|
729
|
+
parent: ctx.parent,
|
|
730
|
+
handler,
|
|
731
|
+
loading: undefined, // Allow loading() to attach loading state
|
|
732
|
+
middleware: [],
|
|
733
|
+
revalidate: [],
|
|
734
|
+
errorBoundary: [],
|
|
735
|
+
notFoundBoundary: [],
|
|
736
|
+
layout: [],
|
|
737
|
+
parallel: [],
|
|
738
|
+
intercept: [],
|
|
739
|
+
loader: [],
|
|
740
|
+
} satisfies EntryData;
|
|
741
|
+
|
|
742
|
+
/* We will throw if user is registring same route name twice */
|
|
743
|
+
invariant(
|
|
744
|
+
ctx.manifest.get(name) === undefined,
|
|
745
|
+
`Duplicate route name: ${name} at ${namespace}`,
|
|
746
|
+
);
|
|
747
|
+
/* Register route entry */
|
|
748
|
+
ctx.manifest.set(name, entry);
|
|
749
|
+
/* Run use and attach handlers */
|
|
750
|
+
if (use && typeof use === "function") {
|
|
751
|
+
const result = store.run(namespace, entry, use)?.flat(3);
|
|
752
|
+
invariant(
|
|
753
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
754
|
+
`route() use() callback must return an array of use items [${namespace}]`,
|
|
755
|
+
);
|
|
756
|
+
return { name: namespace, type: "route", uses: result } as RouteItem;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/* typesafe item */
|
|
760
|
+
return { name: namespace, type: "route" } as RouteItem;
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
const layout: RouteHelpers<any, any>["layout"] = (handler, use) => {
|
|
764
|
+
const store = getContext();
|
|
765
|
+
const ctx = store.getStore();
|
|
766
|
+
if (!ctx) throw new Error("layout() must be called inside map()");
|
|
767
|
+
|
|
768
|
+
invariant(
|
|
769
|
+
!ctx.parent || ctx.parent.type !== "parallel",
|
|
770
|
+
"layout() cannot be used inside parallel()",
|
|
771
|
+
);
|
|
772
|
+
|
|
773
|
+
const isRoot = !ctx.parent || ctx.parent === null;
|
|
774
|
+
const nextIndex = isRoot ? "$root" : store.getNextIndex("layout");
|
|
775
|
+
const namespace = `${ctx.namespace}.${nextIndex}`;
|
|
776
|
+
const shortCode = store.getShortCode("layout");
|
|
777
|
+
|
|
778
|
+
// Unwrap static handler definition, extract the actual handler function
|
|
779
|
+
const isStatic = isStaticHandler(handler);
|
|
780
|
+
const unwrappedHandler = isStatic ? handler.handler : handler;
|
|
781
|
+
|
|
782
|
+
const urlPrefix = getUrlPrefix();
|
|
783
|
+
const entry = {
|
|
784
|
+
id: namespace,
|
|
785
|
+
shortCode,
|
|
786
|
+
type: "layout",
|
|
787
|
+
parent: ctx.parent,
|
|
788
|
+
handler: unwrappedHandler,
|
|
789
|
+
loading: undefined, // Allow loading() to attach loading state
|
|
790
|
+
middleware: [],
|
|
791
|
+
revalidate: [],
|
|
792
|
+
errorBoundary: [],
|
|
793
|
+
notFoundBoundary: [],
|
|
794
|
+
parallel: [],
|
|
795
|
+
intercept: [],
|
|
796
|
+
layout: [],
|
|
797
|
+
loader: [],
|
|
798
|
+
...(urlPrefix ? { mountPath: urlPrefix } : {}),
|
|
799
|
+
...(isStatic
|
|
800
|
+
? {
|
|
801
|
+
isStaticPrerender: true as const,
|
|
802
|
+
...(handler.$$id ? { staticHandlerId: handler.$$id } : {}),
|
|
803
|
+
}
|
|
804
|
+
: {}),
|
|
805
|
+
} satisfies EntryData;
|
|
806
|
+
|
|
807
|
+
// Capture namespace prefix on static handler for build-time reverse() resolution
|
|
808
|
+
if (isStatic && handler.$$id && ctx.namePrefix) {
|
|
809
|
+
(handler as any).$$routePrefix = ctx.namePrefix;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// Run use callback if provided
|
|
813
|
+
let result: AllUseItems[] | undefined;
|
|
814
|
+
if (use && typeof use === "function") {
|
|
815
|
+
result = store.run(namespace, entry, use)?.flat(3);
|
|
816
|
+
|
|
817
|
+
invariant(
|
|
818
|
+
Array.isArray(result) && result.every((item) => isValidUseItem(item)),
|
|
819
|
+
`layout() use() callback must return an array of use items [${namespace}]`,
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// Check if this is an orphan layout (no routes in children, including nested caches)
|
|
824
|
+
const hasRoutes =
|
|
825
|
+
result &&
|
|
826
|
+
Array.isArray(result) &&
|
|
827
|
+
result.some((item) => hasRoutesInItem(item));
|
|
828
|
+
|
|
829
|
+
if (!hasRoutes) {
|
|
830
|
+
// Orphan layouts must not contain other layouts as children.
|
|
831
|
+
// If we're here, all child layouts are also orphan (if any had routes,
|
|
832
|
+
// hasRoutesInItem would have returned true). Nested orphan chains are
|
|
833
|
+
// confusing — use sibling orphan layouts instead.
|
|
834
|
+
if (result) {
|
|
835
|
+
invariant(
|
|
836
|
+
!result.some((item) => item?.type === "layout"),
|
|
837
|
+
`orphan layout cannot contain other layouts as children [${namespace}]`,
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
const parent = ctx.parent;
|
|
842
|
+
|
|
843
|
+
// Allow orphan layouts at root level if they're part of map() builder result
|
|
844
|
+
if (!parent || parent === null) {
|
|
845
|
+
if (!isRoot) {
|
|
846
|
+
invariant(
|
|
847
|
+
false,
|
|
848
|
+
`Orphan layout cannot be used at non-root level without parent [${namespace}]`,
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
// Root-level orphan is allowed (e.g., sibling layouts in map() builder)
|
|
852
|
+
} else {
|
|
853
|
+
// Has parent - register as orphan layout
|
|
854
|
+
invariant(
|
|
855
|
+
parent.type === "route" ||
|
|
856
|
+
parent.type === "layout" ||
|
|
857
|
+
parent.type === "cache",
|
|
858
|
+
`Orphan layouts can only be defined inside route or layout > check [${namespace}]`,
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
// Clear parent pointer for orphan layouts to prevent duplicate processing
|
|
862
|
+
entry.parent = null;
|
|
863
|
+
parent.layout.push(entry);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
if (result) {
|
|
868
|
+
return { name: namespace, type: "layout", uses: result } as LayoutItem;
|
|
869
|
+
}
|
|
870
|
+
return {
|
|
871
|
+
name: namespace,
|
|
872
|
+
type: "layout",
|
|
873
|
+
} as LayoutItem;
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
const isValidUseItem = (item: any): item is AllUseItems | undefined | null => {
|
|
877
|
+
return (
|
|
878
|
+
typeof item === "undefined" ||
|
|
879
|
+
item === null ||
|
|
880
|
+
(item &&
|
|
881
|
+
typeof item === "object" &&
|
|
882
|
+
"type" in item &&
|
|
883
|
+
[
|
|
884
|
+
"layout",
|
|
885
|
+
"route",
|
|
886
|
+
"middleware",
|
|
887
|
+
"revalidate",
|
|
888
|
+
"parallel",
|
|
889
|
+
"intercept",
|
|
890
|
+
"loader",
|
|
891
|
+
"loading",
|
|
892
|
+
"errorBoundary",
|
|
893
|
+
"notFoundBoundary",
|
|
894
|
+
"when",
|
|
895
|
+
"cache",
|
|
896
|
+
"transition",
|
|
897
|
+
"include", // For urls() include() helper
|
|
898
|
+
].includes(item.type))
|
|
899
|
+
);
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
// Global helper exports for direct import from @rangojs/router
|
|
903
|
+
export {
|
|
904
|
+
layout,
|
|
905
|
+
cache,
|
|
906
|
+
middleware,
|
|
907
|
+
revalidate,
|
|
908
|
+
parallel,
|
|
909
|
+
intercept,
|
|
910
|
+
when,
|
|
911
|
+
errorBoundary,
|
|
912
|
+
notFoundBoundary,
|
|
913
|
+
loaderFn as loader,
|
|
914
|
+
loadingFn as loading,
|
|
915
|
+
transitionFn as transition,
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
const isOrphanLayout = (item: AllUseItems): boolean => {
|
|
919
|
+
return (
|
|
920
|
+
item.type === "layout" &&
|
|
921
|
+
!item.uses?.some((child) => hasRoutesInItem(child))
|
|
922
|
+
);
|
|
923
|
+
};
|
|
924
|
+
|
|
925
|
+
// Internal exports used by helper-factories.ts
|
|
926
|
+
export {
|
|
927
|
+
routeFn,
|
|
928
|
+
loaderFn,
|
|
929
|
+
loadingFn,
|
|
930
|
+
transitionFn,
|
|
931
|
+
hasRoutesInItem,
|
|
932
|
+
isValidUseItem,
|
|
933
|
+
isOrphanLayout,
|
|
934
|
+
};
|