@rangojs/router 0.10.1 → 0.11.0
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/dist/types/browser/partial-update.d.ts +1 -0
- package/dist/types/client-urls/navigation.d.ts +11 -0
- package/dist/types/client-urls/revalidate-chain.d.ts +33 -0
- package/dist/types/client-urls/types.d.ts +41 -14
- package/dist/types/client.d.ts +2 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.rsc.d.ts +1 -1
- package/dist/types/router/is-action.d.ts +34 -0
- package/dist/types/testing/index.d.ts +3 -1
- package/dist/types/testing/run-client-revalidate.d.ts +43 -0
- package/dist/types/testing/to-url.d.ts +2 -0
- package/dist/types/types/handler-context.d.ts +12 -5
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/vite/plugins/expose-action-id.d.ts +14 -0
- package/dist/vite/index.js +14 -2
- package/package.json +1 -1
- package/skills/client-urls/SKILL.md +26 -4
- package/skills/loader/SKILL.md +1 -0
- package/skills/testing/SKILL.md +1 -0
- package/skills/typesafety/route-types.md +1 -0
- package/src/browser/partial-update.ts +11 -2
- package/src/browser/server-action-bridge.ts +9 -2
- package/src/client-urls/navigation.ts +40 -42
- package/src/client-urls/revalidate-chain.ts +83 -0
- package/src/client-urls/types.ts +41 -13
- package/src/client.tsx +5 -0
- package/src/index.rsc.ts +1 -0
- package/src/index.ts +1 -0
- package/src/router/is-action.ts +100 -0
- package/src/router/revalidation.ts +5 -48
- package/src/rsc/server-action.ts +2 -4
- package/src/testing/index.ts +4 -1
- package/src/testing/run-client-revalidate.ts +108 -0
- package/src/testing/run-transition-when.ts +1 -3
- package/src/testing/to-url.ts +5 -0
- package/src/types/handler-context.ts +13 -5
- package/src/types/index.ts +1 -0
- package/src/vite/plugins/expose-action-id.ts +29 -1
|
@@ -31,6 +31,17 @@ export declare function beginClientUrlNavigation(targetUrl: URL, signal: AbortSi
|
|
|
31
31
|
export declare function collectClientRevalidationDecisions(options: {
|
|
32
32
|
currentUrl: URL;
|
|
33
33
|
nextUrl: URL;
|
|
34
|
+
/**
|
|
35
|
+
* True only when the decisions ride the action POST itself — the one
|
|
36
|
+
* request the server evaluates with actionContext (locked default true).
|
|
37
|
+
* Action-triggered refetch GETs (partial-update terminals) pass false:
|
|
38
|
+
* the server gives those navigation defaults, and the delta gate below
|
|
39
|
+
* must diff against the default the SERVER will use, or a force decision
|
|
40
|
+
* on the refetch would be silently swallowed as "equals default".
|
|
41
|
+
*/
|
|
42
|
+
actionRequest: boolean;
|
|
43
|
+
/** Action TRUTH for the predicates' isAction() matcher; may be true on
|
|
44
|
+
* refetch GETs where actionRequest is false. */
|
|
34
45
|
isAction: boolean;
|
|
35
46
|
actionId?: string;
|
|
36
47
|
stale: boolean;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The clientUrls revalidate() chain evaluator, shared by the browser
|
|
3
|
+
* collector (navigation.ts) and the public testing primitive
|
|
4
|
+
* (testing/run-client-revalidate.ts) so the two can never drift.
|
|
5
|
+
*
|
|
6
|
+
* Semantics mirror the server's evaluateRevalidation
|
|
7
|
+
* (src/router/revalidation.ts): a boolean verdict is a hard decision and
|
|
8
|
+
* short-circuits the rest of the chain; a `{ defaultShouldRevalidate }`
|
|
9
|
+
* object updates the running suggestion, which later predicates receive as
|
|
10
|
+
* their `defaultShouldRevalidate`; null/undefined defers; a throwing
|
|
11
|
+
* predicate fails open to the current suggestion (logged). One deliberate
|
|
12
|
+
* divergence: the object form is accepted only with a boolean value — the
|
|
13
|
+
* server is laxer, but never re-compares the value, while this decision
|
|
14
|
+
* feeds a strict-equality delta gate and the wire encoding
|
|
15
|
+
* (navigation.ts), where a truthy non-boolean would invert intent.
|
|
16
|
+
*/
|
|
17
|
+
import type { ClientRevalidateArgs, ClientRevalidateFn } from "./types.js";
|
|
18
|
+
/**
|
|
19
|
+
* The locked default the server will apply to the request these decisions
|
|
20
|
+
* ride on. `actionRequest` is about the REQUEST, not the user gesture: only
|
|
21
|
+
* the action POST itself is evaluated server-side with actionContext
|
|
22
|
+
* (default `true`); the follow-up refetch GETs an action can trigger carry
|
|
23
|
+
* no actionContext and get navigation defaults — even though their
|
|
24
|
+
* predicates still see `isAction()` as true.
|
|
25
|
+
*/
|
|
26
|
+
export declare function lockedClientDefault(options: {
|
|
27
|
+
actionRequest: boolean;
|
|
28
|
+
currentParams: Record<string, string>;
|
|
29
|
+
nextParams: Record<string, string>;
|
|
30
|
+
currentUrl: URL;
|
|
31
|
+
nextUrl: URL;
|
|
32
|
+
}): boolean;
|
|
33
|
+
export declare function runClientRevalidateChain(fns: readonly ClientRevalidateFn[], baseArgs: Omit<ClientRevalidateArgs, "defaultShouldRevalidate">, lockedDefault: boolean, label: string): boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ComponentType, ReactNode } from "react";
|
|
2
|
-
import type { LoaderDefinition, LoaderOptions, TransitionConfig } from "../types.js";
|
|
2
|
+
import type { IsActionFn, LoaderDefinition, LoaderOptions, TransitionConfig } from "../types.js";
|
|
3
3
|
import type { TrieMatchResult } from "../router/trie-matching.js";
|
|
4
4
|
import type { PathOptions } from "../urls/pattern-types.js";
|
|
5
5
|
import type { SearchSchema } from "../search-params.js";
|
|
@@ -23,7 +23,8 @@ export type ClientLayoutFn = <const TItems extends ClientUrlItems>(component: Co
|
|
|
23
23
|
* subset of the server ShouldRevalidateFn args — the predicate RUNS IN THE
|
|
24
24
|
* BROWSER (it is declared in a "use client" module and never crosses the
|
|
25
25
|
* projection boundary); only its decision is sent to the server. There is no
|
|
26
|
-
* `context` — no server handler context exists where this executes.
|
|
26
|
+
* `context` — no server handler context exists where this executes. `isAction`
|
|
27
|
+
* is the same callable matcher as on the server, not a boolean.
|
|
27
28
|
*/
|
|
28
29
|
export interface ClientRevalidateArgs {
|
|
29
30
|
/** Full URL of the page being navigated away from (current location). */
|
|
@@ -35,20 +36,45 @@ export interface ClientRevalidateArgs {
|
|
|
35
36
|
/** Route params for the navigation target (definition-local match). */
|
|
36
37
|
readonly nextParams: Record<string, string>;
|
|
37
38
|
/**
|
|
38
|
-
* The
|
|
39
|
-
* the same rules the server
|
|
40
|
-
*
|
|
41
|
-
*
|
|
39
|
+
* The current default decision for this loader, computed client-side with
|
|
40
|
+
* the same rules the server applies to the request the decisions ride on:
|
|
41
|
+
* `true` when they ride the action POST itself, otherwise `true` when
|
|
42
|
+
* params/search changed. (An action-triggered refetch GET gets navigation
|
|
43
|
+
* defaults — matching the server — even though `isAction()` is true.)
|
|
44
|
+
* Earlier predicates' `{ defaultShouldRevalidate }` verdicts thread into
|
|
45
|
+
* this value, exactly like the server chain. Return it for default
|
|
46
|
+
* behavior plus your own conditions.
|
|
42
47
|
*/
|
|
43
48
|
readonly defaultShouldRevalidate: boolean;
|
|
44
49
|
/** True when this is a stale history-entry background revalidation. */
|
|
45
50
|
readonly stale: boolean;
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Same {@link IsActionFn} the server `revalidate()` predicate receives.
|
|
53
|
+
* In the browser the match is against the action stub's hashed `$$id`
|
|
54
|
+
* (the id the action request carries) — not the RSC file-path `$id`.
|
|
55
|
+
*/
|
|
56
|
+
readonly isAction: IsActionFn;
|
|
57
|
+
/**
|
|
58
|
+
* The triggering server action's id, when this is an action. In the
|
|
59
|
+
* browser this is the hashed `hash#export` form (`$$id`), not the RSC
|
|
60
|
+
* file-path `src/...#export`. Prefer `isAction(ref)` — a substring of
|
|
61
|
+
* `path#export` will not match here in production.
|
|
62
|
+
*/
|
|
49
63
|
readonly actionId?: string;
|
|
50
64
|
}
|
|
51
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Client-run per-loader predicate, with the same chain semantics as the
|
|
67
|
+
* server's `revalidate()` (src/router/revalidation.ts): a boolean is a HARD
|
|
68
|
+
* decision that short-circuits the rest of the chain; a
|
|
69
|
+
* `{ defaultShouldRevalidate }` object updates the running suggestion, which
|
|
70
|
+
* later predicates receive as their `defaultShouldRevalidate`; `void` /
|
|
71
|
+
* `null` / `undefined` defers to the current suggestion — so
|
|
72
|
+
* `isAction(CartActions) || undefined` defers to the locked default.
|
|
73
|
+
* Predicates must be synchronous; the object form requires a boolean value.
|
|
74
|
+
*/
|
|
75
|
+
export type ClientRevalidateFn = (args: ClientRevalidateArgs) => boolean | {
|
|
76
|
+
defaultShouldRevalidate: boolean;
|
|
77
|
+
} | null | void;
|
|
52
78
|
export interface ClientUrlLoaderRecord {
|
|
53
79
|
readonly loader: LoaderDefinition<any, any>;
|
|
54
80
|
/** Client-run per-loader revalidation predicates; empty = locked defaults. */
|
|
@@ -114,10 +140,11 @@ export interface ClientUrlHelpers {
|
|
|
114
140
|
readonly loading: (component: ReactNode) => ClientUrlItem;
|
|
115
141
|
/**
|
|
116
142
|
* Per-loader revalidation predicate, valid inside a loader() use callback
|
|
117
|
-
* only. Runs IN THE BROWSER with client-computable args
|
|
118
|
-
* re-run the loader,
|
|
119
|
-
*
|
|
120
|
-
* follow the locked server
|
|
143
|
+
* only. Runs IN THE BROWSER with client-computable args (including the
|
|
144
|
+
* callable `isAction(...refs)` matcher); return true to re-run the loader,
|
|
145
|
+
* false to keep held data. Absent predicates (and requests that carry no
|
|
146
|
+
* decisions: no-JS, PE, prefetch, document loads) follow the locked server
|
|
147
|
+
* defaults.
|
|
121
148
|
*/
|
|
122
149
|
readonly revalidate: (fn: ClientRevalidateFn) => ClientUrlItem;
|
|
123
150
|
/**
|
package/dist/types/client.d.ts
CHANGED
|
@@ -190,3 +190,5 @@ export { useHref } from "./browser/react/use-href.js";
|
|
|
190
190
|
export { useReverse } from "./browser/react/use-reverse.js";
|
|
191
191
|
export type { ScopedReverseFunction, LocalReverseFunction } from "./reverse.js";
|
|
192
192
|
export type { LoaderDefinition } from "./types.js";
|
|
193
|
+
export type { ActionRef, IsActionFn } from "./types.js";
|
|
194
|
+
export type { ClientRevalidateArgs, ClientRevalidateFn, } from "./client-urls/types.js";
|
package/dist/types/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export { RouteNotFoundError, DataNotFoundError, notFound, MiddlewareError, HandlerError, BuildError, DslContextError, InvalidHandlerError, RouterError, Skip, isSkip, } from "./errors.js";
|
|
13
13
|
export type { DocumentProps, DefaultEnv, RouteDefinition, RouteConfig, RouteDefinitionOptions, TrailingSlashMode, Handler, // Supports params object, path pattern, or route name
|
|
14
|
-
HandlerContext, ExtractParams, GenericParams, Middleware, RevalidateParams, Revalidate, ActionRef, RouteKeys, LoaderDefinition, LoaderFn, LoaderContext, LoaderOptions, FetchableLoaderOptions, LoadOptions, ErrorInfo, ErrorBoundaryFallbackProps, ErrorBoundaryHandler, ClientErrorBoundaryFallbackProps, NotFoundInfo, NotFoundBoundaryFallbackProps, NotFoundBoundaryHandler, ErrorPhase, OnErrorContext, OnErrorCallback, } from "./types.js";
|
|
14
|
+
HandlerContext, ExtractParams, GenericParams, Middleware, RevalidateParams, Revalidate, ActionRef, IsActionFn, RouteKeys, LoaderDefinition, LoaderFn, LoaderContext, LoaderOptions, FetchableLoaderOptions, LoadOptions, ErrorInfo, ErrorBoundaryFallbackProps, ErrorBoundaryHandler, ClientErrorBoundaryFallbackProps, NotFoundInfo, NotFoundBoundaryFallbackProps, NotFoundBoundaryHandler, ErrorPhase, OnErrorContext, OnErrorCallback, } from "./types.js";
|
|
15
15
|
export type { SearchSchema, SearchSchemaValue, ResolveSearchSchema, RouteSearchParams, RouteParams, } from "./search-params.js";
|
|
16
16
|
export { TRACKING_SEARCH_PARAMS, type CacheSearchParams, } from "./cache/search-params-filter.js";
|
|
17
17
|
export { createLoader } from "./loader.js";
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* in RSC context, while the regular index.ts is used in client components.
|
|
10
10
|
*/
|
|
11
11
|
export { RouteNotFoundError, DataNotFoundError, notFound, MiddlewareError, HandlerError, BuildError, DslContextError, InvalidHandlerError, RouterError, Skip, isSkip, } from "./index.js";
|
|
12
|
-
export type { DocumentProps, DefaultEnv, RouteDefinition, RouteConfig, RouteDefinitionOptions, TrailingSlashMode, Handler, HandlerContext, ExtractParams, GenericParams, Middleware, RevalidateParams, Revalidate, ActionRef, RouteKeys, LoaderDefinition, LoaderFn, LoaderContext, FetchableLoaderOptions, LoadOptions, ErrorInfo, ErrorBoundaryFallbackProps, ErrorBoundaryHandler, ClientErrorBoundaryFallbackProps, NotFoundInfo, NotFoundBoundaryFallbackProps, NotFoundBoundaryHandler, ErrorPhase, OnErrorContext, OnErrorCallback, TransitionConfig, TransitionWhenFn, TransitionWhenContext, ViewTransitionClass, } from "./types.js";
|
|
12
|
+
export type { DocumentProps, DefaultEnv, RouteDefinition, RouteConfig, RouteDefinitionOptions, TrailingSlashMode, Handler, HandlerContext, ExtractParams, GenericParams, Middleware, RevalidateParams, Revalidate, ActionRef, IsActionFn, RouteKeys, LoaderDefinition, LoaderFn, LoaderContext, FetchableLoaderOptions, LoadOptions, ErrorInfo, ErrorBoundaryFallbackProps, ErrorBoundaryHandler, ClientErrorBoundaryFallbackProps, NotFoundInfo, NotFoundBoundaryFallbackProps, NotFoundBoundaryHandler, ErrorPhase, OnErrorContext, OnErrorCallback, TransitionConfig, TransitionWhenFn, TransitionWhenContext, ViewTransitionClass, } from "./types.js";
|
|
13
13
|
export type { RangoOptions, SSRStreamMode, SSROptions, ResolveStreamingContext, } from "./router.js";
|
|
14
14
|
export type { OriginCheckConfig, OriginCheckContext, OriginCheckPhase, } from "./rsc/origin-guard.js";
|
|
15
15
|
export type { ShellCaptureDebug, ShellCaptureDebugEvent, } from "./rsc/shell-capture.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared `isAction()` matcher for revalidate predicates (server + clientUrls).
|
|
3
|
+
*
|
|
4
|
+
* Action identity is `actionId`. The helper resolves an imported reference the
|
|
5
|
+
* same way the action boundary derives that id (`$id ?? $$id`), so a
|
|
6
|
+
* rename-safe `isAction(fn)` / `isAction(namespace)` match works in both the
|
|
7
|
+
* RSC environment (file-path `$id`) and the browser (hashed `$$id`).
|
|
8
|
+
*/
|
|
9
|
+
import type { IsActionFn } from "../types.js";
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a server-action reference's stable id, mirroring how the action
|
|
12
|
+
* boundary derives `actionContext.actionId` in `rsc/server-action.ts`
|
|
13
|
+
* (`$id ?? $$id`): the file-path `$id` set by the expose-action-id plugin in a
|
|
14
|
+
* production RSC build when present, otherwise React's `$$id`. Resolving both
|
|
15
|
+
* the incoming `actionId` and the reference with the same precedence makes
|
|
16
|
+
* `isAction()` form-agnostic across dev and production.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveActionRefId(ref: unknown): string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Build the `isAction()` helper bound to the current action. Called with no
|
|
21
|
+
* arguments it answers "is this request an action at all?" — `true` during
|
|
22
|
+
* action handling (including action-triggered refetches that carry no id
|
|
23
|
+
* yet), `false` on plain navigation. Called with one or more action
|
|
24
|
+
* references it narrows to those: a single imported action, several
|
|
25
|
+
* (variadic), a namespace import (`import * as Mod`), an object literal of
|
|
26
|
+
* actions (`{ addToCart, removeFromCart }`), or a grouped namespace object.
|
|
27
|
+
* Returns `false` when there is no action or nothing matches.
|
|
28
|
+
*
|
|
29
|
+
* `inAction` is the request kind. It defaults to "an id is present" so
|
|
30
|
+
* existing server call sites stay a single argument. The client passes the
|
|
31
|
+
* explicit flag so a refetch terminal with `isAction: true` still answers
|
|
32
|
+
* bare `isAction()` even if `actionId` was not threaded.
|
|
33
|
+
*/
|
|
34
|
+
export declare function makeIsAction(currentActionId: string | undefined, inAction?: boolean): IsActionFn;
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
* condition and would throw if pulled into this barrel.
|
|
27
27
|
*
|
|
28
28
|
* Layers:
|
|
29
|
-
* - Unit: runMiddleware, runLoader
|
|
29
|
+
* - Unit: runMiddleware, runLoader, runClientRevalidate
|
|
30
30
|
* - Integration: dispatch (request -> Response)
|
|
31
31
|
* - Cross-cut: assertCacheStatus, assertShellStatus, assertGeneratedRoutesMatch
|
|
32
32
|
* - Component: see @rangojs/router/testing/dom (renderRoute)
|
|
@@ -39,6 +39,8 @@ export { runLoader, runLoaderResult } from "./run-loader.js";
|
|
|
39
39
|
export type { RunLoaderOptions, RunLoaderResult, UseResolver, TestLoaderContext, } from "./run-loader.js";
|
|
40
40
|
export { runTransitionWhen } from "./run-transition-when.js";
|
|
41
41
|
export type { RunTransitionWhenOptions, RunTransitionWhenResult, } from "./run-transition-when.js";
|
|
42
|
+
export { runClientRevalidate } from "./run-client-revalidate.js";
|
|
43
|
+
export type { RunClientRevalidateOptions } from "./run-client-revalidate.js";
|
|
42
44
|
export { dispatch } from "./dispatch.js";
|
|
43
45
|
export type { DispatchOptions } from "./dispatch.js";
|
|
44
46
|
export { assertCacheStatus, assertCacheDecision, parseCacheHeader, createCacheSink, filterCacheDecisions, } from "./cache-status.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* runClientRevalidate — unit-test clientUrls() revalidate() predicates.
|
|
3
|
+
*
|
|
4
|
+
* Builds the same {@link ClientRevalidateArgs} the browser collector passes
|
|
5
|
+
* and evaluates the predicate(s) through the SAME chain evaluator production
|
|
6
|
+
* uses (client-urls/revalidate-chain.ts) — locked default, boolean
|
|
7
|
+
* short-circuit, soft-verdict threading, and fail-open are the production
|
|
8
|
+
* code paths, not a re-implementation. Pass an array to test a chain.
|
|
9
|
+
*
|
|
10
|
+
* Synchronous: client revalidate functions must be sync.
|
|
11
|
+
*/
|
|
12
|
+
import type { ClientRevalidateFn } from "../client-urls/types.js";
|
|
13
|
+
/**
|
|
14
|
+
* Options for {@link runClientRevalidate}. Defaults model a same-URL
|
|
15
|
+
* navigation with no action (locked default `false`).
|
|
16
|
+
*/
|
|
17
|
+
export interface RunClientRevalidateOptions {
|
|
18
|
+
currentUrl?: string | URL;
|
|
19
|
+
nextUrl?: string | URL;
|
|
20
|
+
currentParams?: Record<string, string>;
|
|
21
|
+
nextParams?: Record<string, string>;
|
|
22
|
+
stale?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* The triggering action: a single imported reference (id resolved via
|
|
25
|
+
* `$id ?? $$id`; throws if the function carries neither) or a raw actionId
|
|
26
|
+
* string. A namespace/object is rejected — it cannot identify the ONE
|
|
27
|
+
* action that triggered the request. Omit for a plain navigation.
|
|
28
|
+
*/
|
|
29
|
+
action?: ((...args: never[]) => unknown) | string;
|
|
30
|
+
/**
|
|
31
|
+
* Model an action-triggered refetch GET: predicates see `isAction()` as
|
|
32
|
+
* true, but the locked default stays the navigation default, matching how
|
|
33
|
+
* the server evaluates that request (no actionContext). Defaults to
|
|
34
|
+
* treating a provided `action` as the action POST itself.
|
|
35
|
+
*/
|
|
36
|
+
actionRequest?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Run one clientUrls `revalidate()` predicate — or a chain, in declaration
|
|
40
|
+
* order — against production-built args. Returns the final boolean decision
|
|
41
|
+
* (locked default if every predicate defers or throws).
|
|
42
|
+
*/
|
|
43
|
+
export declare function runClientRevalidate(fn: ClientRevalidateFn | readonly ClientRevalidateFn[], opts?: RunClientRevalidateOptions): boolean;
|
|
@@ -418,13 +418,17 @@ export type RevalidateParams<TParams = GenericParams, TEnv = any> = Parameters<S
|
|
|
418
418
|
/**
|
|
419
419
|
* A reference to a server action, used by `isAction()` in a revalidate predicate.
|
|
420
420
|
*
|
|
421
|
-
* Either a directly imported action (`import { addToCart }`)
|
|
422
|
-
* import of an action module (`import * as CartActions`)
|
|
421
|
+
* Either a directly imported action (`import { addToCart }`), a namespace
|
|
422
|
+
* import of an action module (`import * as CartActions`), an object
|
|
423
|
+
* literal of actions (`{ addToCart, removeFromCart }`), or a grouped
|
|
424
|
+
* namespace (`{ Cart: CartActions, Order: OrderActions }`). Matching resolves the
|
|
423
425
|
* action's build-injected id (`path#export`) — the same identity the router uses
|
|
424
426
|
* for `actionId` — so a renamed or moved action breaks at compile time instead
|
|
425
427
|
* of silently failing to match.
|
|
426
428
|
*/
|
|
427
429
|
export type ActionRef = ((...args: never[]) => unknown) | Record<string, unknown>;
|
|
430
|
+
/** The `isAction()` matcher passed to server and client `revalidate()` predicates. */
|
|
431
|
+
export type IsActionFn = (...actions: ActionRef[]) => boolean;
|
|
428
432
|
/**
|
|
429
433
|
* Revalidation function called during client-side navigation to decide whether
|
|
430
434
|
* a segment (layout, route, parallel slot, or loader) should be re-rendered.
|
|
@@ -511,8 +515,10 @@ export type ShouldRevalidateFn<TParams = GenericParams, TEnv = any> = (args: {
|
|
|
511
515
|
/**
|
|
512
516
|
* Typed, rename-safe action matching. Returns `true` when the action that
|
|
513
517
|
* triggered this revalidation is one of the given references — or, for a
|
|
514
|
-
* namespace import (`import * as CartActions`),
|
|
515
|
-
*
|
|
518
|
+
* namespace import (`import * as CartActions`), object literal
|
|
519
|
+
* (`{ addToCart, removeFromCart }`), or grouped namespaces
|
|
520
|
+
* (`{ Cart: CartActions }`), any of those exports — and `false`
|
|
521
|
+
* otherwise (including plain navigation with no action).
|
|
516
522
|
*
|
|
517
523
|
* Called with NO arguments it answers "is this request an action at all?":
|
|
518
524
|
* `true` for any action, `false` on plain navigation. Use the bare form when
|
|
@@ -536,9 +542,10 @@ export type ShouldRevalidateFn<TParams = GenericParams, TEnv = any> = (args: {
|
|
|
536
542
|
* revalidate((ctx) => ctx.isAction(addToCart) || undefined); // one action
|
|
537
543
|
* revalidate((ctx) => ctx.isAction(addToCart, removeFromCart) || undefined); // several
|
|
538
544
|
* revalidate((ctx) => ctx.isAction(CartActions) || undefined); // any in the module
|
|
545
|
+
* revalidate((ctx) => ctx.isAction({ addToCart, removeFromCart }) || undefined); // object form
|
|
539
546
|
* ```
|
|
540
547
|
*/
|
|
541
|
-
isAction:
|
|
548
|
+
isAction: IsActionFn;
|
|
542
549
|
/** URL where the action was executed (the page the user was on when they triggered the action). */
|
|
543
550
|
actionUrl?: URL;
|
|
544
551
|
/** Return value from the action execution. Can be used to conditionally revalidate based on the action's outcome. */
|
|
@@ -3,7 +3,7 @@ import "./global-namespace.js";
|
|
|
3
3
|
export type { DocumentProps, ExtractParams, TrailingSlashMode, RouteConfig, RouteDefinitionOptions, RouteDefinition, ResolvedRouteMap, } from "./route-config.js";
|
|
4
4
|
export type { ErrorInfo, ErrorBoundaryFallbackProps, ErrorBoundaryHandler, ClientErrorBoundaryFallbackProps, LoaderDataResult, NotFoundInfo, NotFoundBoundaryFallbackProps, NotFoundBoundaryHandler, } from "./boundaries.js";
|
|
5
5
|
export { isLoaderDataResult } from "./boundaries.js";
|
|
6
|
-
export type { MiddlewareFn, ScopedRouteMap, Handler, HandlerContext, InternalHandlerContext, GenericParams, RevalidateParams, ShouldRevalidateFn, ActionRef, RouteKeys, ExtractRouteParams, HandlersForRouteMap, Revalidate, Middleware, } from "./handler-context.js";
|
|
6
|
+
export type { MiddlewareFn, ScopedRouteMap, Handler, HandlerContext, InternalHandlerContext, GenericParams, RevalidateParams, ShouldRevalidateFn, ActionRef, IsActionFn, RouteKeys, ExtractRouteParams, HandlersForRouteMap, Revalidate, Middleware, } from "./handler-context.js";
|
|
7
7
|
export type { ViewTransitionClass, TransitionConfig, TransitionWhenFn, TransitionWhenContext, ResolvedSegment, SegmentMetadata, SlotState, RootLayoutProps, MatchResult, } from "./segments.js";
|
|
8
8
|
export type { LazyIncludeContext, RouteEntry } from "./route-entry.js";
|
|
9
9
|
export type { LoaderContext, LoaderFn, FetchableLoaderOptions, LoaderOptions, LoadOptions, LoaderDefinition, } from "./loader-types.js";
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import type { Plugin } from "vite";
|
|
2
|
+
/**
|
|
3
|
+
* Per-reference own `bind` injected next to `$$id`. React's client.browser
|
|
4
|
+
* build carries no server-reference metadata across `.bind()` (the
|
|
5
|
+
* edge/node/server builds install an own `bind` on each reference that
|
|
6
|
+
* does), so `isAction(boundStub)` would silently miss in the browser only.
|
|
7
|
+
* Installing the same per-reference own `bind` here — scoped to the stubs
|
|
8
|
+
* this plugin already wraps, guarded to never override an existing own
|
|
9
|
+
* `bind` — closes that without mutating the global Function.prototype
|
|
10
|
+
* (which would re-wrap once per Vite environment/HMR pass and break
|
|
11
|
+
* native-function detection for co-loaded code). The helper re-installs
|
|
12
|
+
* itself on the bound result so chained binds keep the metadata too.
|
|
13
|
+
*/
|
|
14
|
+
export declare const ACTION_BIND_HELPER_NAME: string;
|
|
15
|
+
export declare const ACTION_BIND_HELPER_SOURCE: string;
|
|
2
16
|
/**
|
|
3
17
|
* Vite plugin that exposes action IDs on server reference functions.
|
|
4
18
|
*
|
package/dist/vite/index.js
CHANGED
|
@@ -1401,6 +1401,14 @@ function isUseServerModule(filePath) {
|
|
|
1401
1401
|
return false;
|
|
1402
1402
|
}
|
|
1403
1403
|
}
|
|
1404
|
+
var ACTION_BIND_HELPER_NAME = "__rangoActionBind";
|
|
1405
|
+
var ACTION_BIND_HELPER_SOURCE = `var ${ACTION_BIND_HELPER_NAME} = function () {
|
|
1406
|
+
var bound = Function.prototype.bind.apply(this, arguments);
|
|
1407
|
+
if (typeof this.$id === "string") bound.$id = this.$id;
|
|
1408
|
+
if (typeof this.$$id === "string") bound.$$id = this.$$id;
|
|
1409
|
+
bound.bind = ${ACTION_BIND_HELPER_NAME};
|
|
1410
|
+
return bound;
|
|
1411
|
+
};`;
|
|
1404
1412
|
function applyServerReferenceWrapping(code, s, hashToFileMap) {
|
|
1405
1413
|
if (!code.includes("createServerReference(")) {
|
|
1406
1414
|
return false;
|
|
@@ -1425,9 +1433,13 @@ function applyServerReferenceWrapping(code, s, hashToFileMap) {
|
|
|
1425
1433
|
}
|
|
1426
1434
|
}
|
|
1427
1435
|
}
|
|
1428
|
-
const replacement = `(function(fn) { fn.$$id = ${finalIdArg}; return fn; })(${fnCall}(${idArg}${rest}))`;
|
|
1436
|
+
const replacement = `(function(fn) { fn.$$id = ${finalIdArg}; if (!Object.prototype.hasOwnProperty.call(fn, "bind")) fn.bind = ${ACTION_BIND_HELPER_NAME}; return fn; })(${fnCall}(${idArg}${rest}))`;
|
|
1429
1437
|
s.overwrite(start, end, replacement);
|
|
1430
1438
|
}
|
|
1439
|
+
if (hasChanges) {
|
|
1440
|
+
s.prepend(`${ACTION_BIND_HELPER_SOURCE}
|
|
1441
|
+
`);
|
|
1442
|
+
}
|
|
1431
1443
|
return hasChanges;
|
|
1432
1444
|
}
|
|
1433
1445
|
function transformServerReferences(code, sourceId, hashToFileMap) {
|
|
@@ -3698,7 +3710,7 @@ import { resolve } from "node:path";
|
|
|
3698
3710
|
// package.json
|
|
3699
3711
|
var package_default = {
|
|
3700
3712
|
name: "@rangojs/router",
|
|
3701
|
-
version: "0.
|
|
3713
|
+
version: "0.11.0",
|
|
3702
3714
|
description: "Django-inspired RSC router with composable URL patterns",
|
|
3703
3715
|
keywords: [
|
|
3704
3716
|
"react",
|
package/package.json
CHANGED
|
@@ -91,7 +91,7 @@ export default clientUrls(({ path, layout, loader, revalidate }) => [
|
|
|
91
91
|
nextParams,
|
|
92
92
|
defaultShouldRevalidate,
|
|
93
93
|
}) => {
|
|
94
|
-
if (isAction) return false;
|
|
94
|
+
if (isAction()) return false;
|
|
95
95
|
return currentParams.slug !== nextParams.slug
|
|
96
96
|
? defaultShouldRevalidate
|
|
97
97
|
: false;
|
|
@@ -184,14 +184,36 @@ only its _decision_ crosses the wire with the navigation request. Requests that
|
|
|
184
184
|
carry no decisions (no-JS, progressive enhancement, prefetch, document loads)
|
|
185
185
|
follow the locked server defaults.
|
|
186
186
|
|
|
187
|
+
`isAction` is the same callable matcher as the server predicate, not a
|
|
188
|
+
boolean. Action identity is `actionId`, whose FORM differs per environment:
|
|
189
|
+
file-path `$id` (`path#export`) in the RSC env, hashed `$$id` in the
|
|
190
|
+
browser — which is why the matcher (resolving an imported reference's
|
|
191
|
+
`$id ?? $$id`) is the supported surface and a file-path substring on
|
|
192
|
+
`actionId` is not:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
import { addToCart, removeFromCart } from "./actions/cart";
|
|
196
|
+
import * as CartActions from "./actions/cart";
|
|
197
|
+
|
|
198
|
+
revalidate(({ isAction }) => isAction()); // any action
|
|
199
|
+
revalidate(({ isAction }) => isAction(addToCart)); // one action
|
|
200
|
+
revalidate(({ isAction }) => isAction(addToCart, removeFromCart)); // several
|
|
201
|
+
revalidate(({ isAction }) => isAction(CartActions)); // import * as
|
|
202
|
+
revalidate(({ isAction }) => isAction({ addToCart, removeFromCart })); // object
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Bare `isAction()` is "is this an action at all?". `actionId` stays as the
|
|
206
|
+
string escape hatch. Return `isAction(refs) || undefined` to defer to the
|
|
207
|
+
locked default on a non-match (same idiom as the server).
|
|
208
|
+
|
|
187
209
|
Two scars worth copying:
|
|
188
210
|
|
|
189
211
|
- A blunt `() => false` keeps serving the OLD product on product→product
|
|
190
212
|
navigations (same route, new param). Make predicates param-sensitive:
|
|
191
213
|
return `defaultShouldRevalidate` when the identifying param changed.
|
|
192
|
-
- One action, per-loader outcomes: a cart badge loader revalidates on
|
|
193
|
-
(`isAction
|
|
194
|
-
|
|
214
|
+
- One action, per-loader outcomes: a cart badge loader revalidates on cart
|
|
215
|
+
actions (`isAction(CartActions)`) while product/related loaders hold —
|
|
216
|
+
three freshness outcomes in a single commit, decided per loader.
|
|
195
217
|
|
|
196
218
|
## Loaders are full citizens: signals and handles
|
|
197
219
|
|
package/skills/loader/SKILL.md
CHANGED
|
@@ -359,6 +359,7 @@ loader(CartLoader, () => [
|
|
|
359
359
|
]);
|
|
360
360
|
revalidate((ctx) => ctx.isAction(addToCart, removeFromCart) || undefined); // several
|
|
361
361
|
revalidate((ctx) => ctx.isAction(CartActions) || undefined); // any action in the module
|
|
362
|
+
revalidate((ctx) => ctx.isAction({ addToCart, removeFromCart }) || undefined); // object form
|
|
362
363
|
```
|
|
363
364
|
|
|
364
365
|
`isAction()` is a method on the revalidate predicate's **context argument** —
|
package/skills/testing/SKILL.md
CHANGED
|
@@ -89,6 +89,7 @@ Each primitive links to its sub-file (API + recipe + caveats).
|
|
|
89
89
|
| one middleware's ordering / short-circuit / cookie+header merge | unit (node) | [`runMiddleware`](./middleware.md) | `@rangojs/router/testing` |
|
|
90
90
|
| a `"use server"` action's cookie / header / flash output (even on `throw redirect()`) | unit (node) | [`runInRequestContext`](./server-actions.md) | `@rangojs/router/testing` |
|
|
91
91
|
| a `transition({ when })` gate (keep/drop) against nav source / target / action metadata | unit (node) | `runTransitionWhen` (`{ kept, whenContext }`; pass `{ ppr: true }` for pre-handler timing) | `@rangojs/router/testing` |
|
|
92
|
+
| a `clientUrls()` `revalidate()` / `isAction(ref)` predicate | unit (node) | `runClientRevalidate` (production `makeIsAction` + locked defaults) | `@rangojs/router/testing` |
|
|
92
93
|
| a handle's `collect`/accumulator, a seeded handle read, or a loader handle write | unit | [`collectHandle` / seeded `handles` / `handlePushes`](./handles.md) | `@rangojs/router/testing` |
|
|
93
94
|
| a CLIENT component reading router context (`useParams`/`useReverse`/`Outlet`/`useNavigation`/`useLoader`) | unit (DOM) | [`renderRoute`](./client-components.md) | `@rangojs/router/testing/dom` |
|
|
94
95
|
| a redirect / status / headers / cookies / **response route** (json/text/html/xml/md), no Flight | integration | [`dispatch`](./response-routes.md) | `@rangojs/router/testing` |
|
|
@@ -204,6 +204,7 @@ import * as CartActions from "./actions/cart";
|
|
|
204
204
|
revalidate((ctx) => ctx.isAction(addToCart) || undefined); // one action
|
|
205
205
|
revalidate((ctx) => ctx.isAction(addToCart, removeFromCart) || undefined); // several
|
|
206
206
|
revalidate((ctx) => ctx.isAction(CartActions) || undefined); // any action in the module
|
|
207
|
+
revalidate((ctx) => ctx.isAction({ addToCart, removeFromCart }) || undefined); // object form
|
|
207
208
|
```
|
|
208
209
|
|
|
209
210
|
`ctx.isAction()` (only available on the revalidate predicate's context) returns a
|
|
@@ -101,7 +101,7 @@ export type UpdateMode =
|
|
|
101
101
|
}
|
|
102
102
|
| { type: "leave-intercept"; interceptSourceUrl?: string }
|
|
103
103
|
| { type: "stale-revalidation"; interceptSourceUrl?: string }
|
|
104
|
-
| { type: "action"; interceptSourceUrl?: string };
|
|
104
|
+
| { type: "action"; interceptSourceUrl?: string; actionId?: string };
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
107
|
* Type for the fetchPartialUpdate function
|
|
@@ -200,7 +200,16 @@ export function createPartialUpdater(
|
|
|
200
200
|
clientRevalidation = collectClientRevalidationDecisions({
|
|
201
201
|
currentUrl: new URL(previousUrl, window.location.origin),
|
|
202
202
|
nextUrl: new URL(url, window.location.origin),
|
|
203
|
-
|
|
203
|
+
// This partial fetch is a GET the server evaluates WITHOUT
|
|
204
|
+
// actionContext (navigation defaults) even when it is an
|
|
205
|
+
// action-triggered refetch — so the decision baseline is never the
|
|
206
|
+
// action default here. Predicates still see isAction()/actionId
|
|
207
|
+
// truthfully for matching.
|
|
208
|
+
actionRequest: false,
|
|
209
|
+
isAction: mode.type === "action",
|
|
210
|
+
...(mode.type === "action" && mode.actionId !== undefined
|
|
211
|
+
? { actionId: mode.actionId }
|
|
212
|
+
: {}),
|
|
204
213
|
stale: mode.type === "stale-revalidation",
|
|
205
214
|
});
|
|
206
215
|
} catch {
|
|
@@ -145,6 +145,7 @@ export function createServerActionBridge(
|
|
|
145
145
|
async function refetchRoute(opts?: {
|
|
146
146
|
segments?: string[];
|
|
147
147
|
interceptSourceUrl?: string | null;
|
|
148
|
+
actionId?: string;
|
|
148
149
|
}): Promise<void> {
|
|
149
150
|
const src = opts?.interceptSourceUrl ?? null;
|
|
150
151
|
const navTx = createNavigationTransaction(
|
|
@@ -167,6 +168,7 @@ export function createServerActionBridge(
|
|
|
167
168
|
{
|
|
168
169
|
type: "action" as const,
|
|
169
170
|
...(src ? { interceptSourceUrl: src } : {}),
|
|
171
|
+
...(opts?.actionId !== undefined ? { actionId: opts.actionId } : {}),
|
|
170
172
|
},
|
|
171
173
|
);
|
|
172
174
|
} finally {
|
|
@@ -309,6 +311,9 @@ export function createServerActionBridge(
|
|
|
309
311
|
clientRevalidation = collectClientRevalidationDecisions({
|
|
310
312
|
currentUrl: actionPageUrl,
|
|
311
313
|
nextUrl: actionPageUrl,
|
|
314
|
+
// Decisions ride the action POST itself — the server evaluates it
|
|
315
|
+
// with actionContext, so the locked default is the action default.
|
|
316
|
+
actionRequest: true,
|
|
312
317
|
isAction: true,
|
|
313
318
|
actionId: id,
|
|
314
319
|
stale: false,
|
|
@@ -707,7 +712,7 @@ export function createServerActionBridge(
|
|
|
707
712
|
// Invalidation is deferred to finalizeAction(); here we only trigger
|
|
708
713
|
// the revalidation refetch of the new route (suppressed on keep).
|
|
709
714
|
if (!scenario.onInterceptRoute && !keepCache) {
|
|
710
|
-
refetchRoute().catch((error) => {
|
|
715
|
+
refetchRoute({ actionId: id }).catch((error) => {
|
|
711
716
|
if (isBackgroundSuppressible(error)) return;
|
|
712
717
|
console.error(
|
|
713
718
|
"[Browser] Background revalidation failed:",
|
|
@@ -724,6 +729,7 @@ export function createServerActionBridge(
|
|
|
724
729
|
if (!keepCache) {
|
|
725
730
|
await refetchRoute({
|
|
726
731
|
interceptSourceUrl: store.getInterceptSourceUrl(),
|
|
732
|
+
actionId: id,
|
|
727
733
|
});
|
|
728
734
|
}
|
|
729
735
|
break;
|
|
@@ -737,7 +743,7 @@ export function createServerActionBridge(
|
|
|
737
743
|
// resolving last must discharge a directive-free sibling's repair.
|
|
738
744
|
// See the keep row in docs/design/rango-state-cookie.md (the all-keep
|
|
739
745
|
// edge, and the benign re-mark-stale-after-refetch end-state delta).
|
|
740
|
-
await refetchRoute({ interceptSourceUrl });
|
|
746
|
+
await refetchRoute({ interceptSourceUrl, actionId: id });
|
|
741
747
|
break;
|
|
742
748
|
}
|
|
743
749
|
|
|
@@ -759,6 +765,7 @@ export function createServerActionBridge(
|
|
|
759
765
|
await refetchRoute({
|
|
760
766
|
segments: segmentsToSend,
|
|
761
767
|
interceptSourceUrl,
|
|
768
|
+
actionId: id,
|
|
762
769
|
});
|
|
763
770
|
break;
|
|
764
771
|
}
|