@rangojs/router 0.0.0-experimental.97 → 0.0.0-experimental.98914650
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -9
- package/dist/bin/rango.js +157 -63
- package/dist/testing/vitest.js +82 -0
- package/dist/vite/index.js +1584 -639
- package/package.json +71 -21
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +60 -0
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +222 -30
- package/skills/caching/SKILL.md +263 -8
- package/skills/composability/SKILL.md +27 -2
- package/skills/css/SKILL.md +76 -0
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +3 -1
- package/skills/hooks/SKILL.md +235 -28
- package/skills/host-router/SKILL.md +122 -22
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +29 -5
- package/skills/layout/SKILL.md +13 -9
- package/skills/links/SKILL.md +173 -17
- package/skills/loader/SKILL.md +170 -23
- package/skills/middleware/SKILL.md +16 -10
- package/skills/migrate-nextjs/SKILL.md +38 -16
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +11 -7
- package/skills/prerender/SKILL.md +14 -33
- package/skills/rango/SKILL.md +250 -25
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +114 -47
- package/skills/route/SKILL.md +42 -5
- package/skills/router-setup/SKILL.md +3 -3
- package/skills/server-actions/SKILL.md +78 -42
- package/skills/tailwind/SKILL.md +27 -3
- package/skills/testing/SKILL.md +129 -0
- package/skills/testing/bindings.md +89 -0
- package/skills/testing/cache-prerender.md +124 -0
- package/skills/testing/client-components.md +122 -0
- package/skills/testing/e2e-parity.md +125 -0
- package/skills/testing/flight.md +92 -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 +121 -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 +120 -0
- package/skills/typesafety/SKILL.md +316 -26
- package/skills/use-cache/SKILL.md +36 -5
- package/skills/vercel/SKILL.md +107 -0
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/__internal.ts +0 -65
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/action-fence.ts +47 -0
- package/src/browser/app-shell.ts +14 -27
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/event-controller.ts +37 -143
- 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 +30 -59
- package/src/browser/navigation-client.ts +96 -84
- package/src/browser/navigation-store-handle.ts +38 -0
- package/src/browser/navigation-store.ts +32 -82
- package/src/browser/navigation-transaction.ts +9 -59
- package/src/browser/partial-update.ts +60 -127
- package/src/browser/prefetch/cache.ts +82 -72
- package/src/browser/prefetch/fetch.ts +108 -33
- package/src/browser/prefetch/queue.ts +6 -3
- package/src/browser/rango-state.ts +157 -115
- package/src/browser/react/Link.tsx +0 -2
- package/src/browser/react/NavigationProvider.tsx +41 -48
- package/src/browser/react/ScrollRestoration.tsx +10 -6
- package/src/browser/react/filter-segment-order.ts +0 -2
- package/src/browser/react/index.ts +0 -48
- package/src/browser/react/location-state-shared.ts +166 -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 +17 -14
- package/src/browser/react/use-link-status.ts +0 -4
- package/src/browser/react/use-navigation.ts +0 -3
- package/src/browser/react/use-params.ts +11 -11
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +20 -5
- package/src/browser/react/use-search-params.ts +0 -5
- package/src/browser/react/use-segments.ts +0 -13
- package/src/browser/response-adapter.ts +52 -1
- package/src/browser/rsc-router.tsx +70 -34
- package/src/browser/scroll-restoration.ts +22 -14
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +168 -44
- package/src/browser/types.ts +36 -21
- package/src/browser/validate-redirect-origin.ts +43 -16
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +60 -35
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/index.ts +8 -2
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +89 -10
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +1 -1
- package/src/build/route-types/param-extraction.ts +6 -3
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +122 -22
- package/src/build/route-types/scan-filter.ts +1 -1
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-policy.ts +68 -28
- package/src/cache/cache-runtime.ts +134 -32
- package/src/cache/cache-scope.ts +100 -74
- package/src/cache/cache-tag.ts +98 -0
- package/src/cache/cf/cf-cache-store.ts +2255 -238
- package/src/cache/cf/index.ts +6 -16
- package/src/cache/document-cache.ts +61 -20
- package/src/cache/handle-snapshot.ts +63 -0
- package/src/cache/index.ts +22 -20
- package/src/cache/memory-segment-store.ts +136 -37
- package/src/cache/profile-registry.ts +6 -30
- package/src/cache/read-through-swr.ts +41 -11
- package/src/cache/segment-codec.ts +0 -16
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/types.ts +33 -100
- package/src/cache/vercel/index.ts +11 -0
- package/src/cache/vercel/vercel-cache-store.ts +799 -0
- package/src/client.rsc.tsx +6 -21
- package/src/client.tsx +25 -61
- package/src/component-utils.ts +19 -0
- package/src/context-var.ts +17 -5
- package/src/decode-loader-results.ts +36 -0
- package/src/defer.ts +196 -0
- package/src/deps/ssr.ts +0 -1
- package/src/errors.ts +30 -4
- package/src/handle.ts +31 -23
- package/src/handles/MetaTags.tsx +0 -14
- package/src/handles/breadcrumbs.ts +16 -5
- package/src/handles/meta.ts +0 -39
- package/src/host/cookie-handler.ts +0 -36
- package/src/host/errors.ts +0 -24
- package/src/host/index.ts +8 -2
- package/src/host/pattern-matcher.ts +7 -50
- 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 +63 -9
- package/src/index.ts +64 -9
- package/src/internal-debug.ts +2 -4
- 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/network-error-thrower.tsx +1 -6
- package/src/outlet-provider.tsx +1 -5
- package/src/prerender/param-hash.ts +10 -11
- package/src/prerender/store.ts +32 -37
- package/src/prerender.ts +61 -6
- package/src/redirect-origin.ts +100 -0
- package/src/response-utils.ts +9 -0
- package/src/reverse.ts +65 -40
- package/src/root-error-boundary.tsx +1 -19
- package/src/route-content-wrapper.tsx +7 -72
- package/src/route-definition/dsl-helpers.ts +244 -281
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +40 -17
- package/src/route-definition/redirect.ts +43 -9
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-map-builder.ts +0 -16
- package/src/route-types.ts +19 -41
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +15 -15
- package/src/router/error-handling.ts +13 -17
- package/src/router/find-match.ts +44 -23
- package/src/router/handler-context.ts +4 -41
- package/src/router/intercept-resolution.ts +14 -19
- package/src/router/lazy-includes.ts +9 -46
- package/src/router/loader-resolution.ts +91 -46
- package/src/router/logging.ts +0 -6
- package/src/router/manifest.ts +18 -29
- package/src/router/match-api.ts +0 -20
- package/src/router/match-context.ts +0 -22
- package/src/router/match-handlers.ts +57 -58
- package/src/router/match-middleware/background-revalidation.ts +0 -7
- package/src/router/match-middleware/cache-lookup.ts +150 -271
- package/src/router/match-middleware/cache-store.ts +3 -33
- package/src/router/match-middleware/intercept-resolution.ts +0 -22
- package/src/router/match-middleware/segment-resolution.ts +0 -22
- package/src/router/match-pipelines.ts +1 -42
- package/src/router/match-result.ts +31 -80
- package/src/router/metrics.ts +0 -34
- package/src/router/middleware-types.ts +5 -112
- package/src/router/middleware.ts +118 -133
- package/src/router/navigation-snapshot.ts +0 -51
- package/src/router/params-util.ts +23 -0
- package/src/router/pattern-matching.ts +62 -67
- package/src/router/prerender-match.ts +99 -63
- package/src/router/preview-match.ts +3 -1
- package/src/router/request-classification.ts +28 -62
- package/src/router/revalidation.ts +50 -56
- package/src/router/route-snapshot.ts +0 -1
- package/src/router/router-context.ts +0 -27
- package/src/router/router-interfaces.ts +68 -35
- package/src/router/router-options.ts +55 -1
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +44 -63
- package/src/router/segment-resolution/helpers.ts +34 -0
- package/src/router/segment-resolution/loader-cache.ts +40 -37
- package/src/router/segment-resolution/revalidation.ts +203 -285
- 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 +36 -0
- package/src/router/segment-resolution.ts +4 -1
- package/src/router/segment-wrappers.ts +0 -3
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry-otel.ts +0 -20
- package/src/router/telemetry.ts +96 -19
- package/src/router/timeout.ts +0 -20
- package/src/router/trie-matching.ts +87 -48
- package/src/router/types.ts +9 -63
- package/src/router/url-params.ts +0 -5
- package/src/router.ts +80 -41
- package/src/rsc/handler-context.ts +3 -2
- package/src/rsc/handler.ts +83 -78
- package/src/rsc/helpers.ts +93 -5
- package/src/rsc/index.ts +1 -1
- package/src/rsc/json-route-result.ts +38 -0
- package/src/rsc/manifest-init.ts +28 -41
- package/src/rsc/origin-guard.ts +39 -25
- package/src/rsc/progressive-enhancement.ts +12 -1
- package/src/rsc/redirect-guard.ts +99 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +76 -62
- package/src/rsc/rsc-rendering.ts +41 -60
- package/src/rsc/runtime-warnings.ts +23 -10
- package/src/rsc/server-action.ts +62 -67
- package/src/rsc/ssr-setup.ts +16 -0
- package/src/rsc/types.ts +10 -5
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +4 -20
- package/src/segment-loader-promise.ts +14 -2
- package/src/segment-system.tsx +199 -142
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +150 -51
- package/src/server/cookie-store.ts +80 -5
- package/src/server/handle-store.ts +7 -24
- package/src/server/loader-registry.ts +5 -24
- package/src/server/request-context.ts +165 -87
- package/src/ssr/index.tsx +14 -14
- package/src/static-handler.ts +10 -13
- package/src/testing/cache-status.ts +162 -0
- package/src/testing/collect-handle.ts +40 -0
- package/src/testing/dispatch.ts +618 -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 +232 -0
- package/src/testing/generated-routes.ts +183 -0
- package/src/testing/index.ts +99 -0
- package/src/testing/internal/context.ts +348 -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 +330 -0
- package/src/testing/render-route.tsx +566 -0
- package/src/testing/run-loader.ts +378 -0
- package/src/testing/run-middleware.ts +205 -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 +0 -52
- package/src/theme/ThemeScript.tsx +0 -6
- package/src/theme/constants.ts +0 -12
- package/src/theme/index.ts +0 -7
- package/src/theme/theme-context.ts +1 -5
- package/src/theme/theme-script.ts +0 -14
- package/src/theme/use-theme.ts +0 -3
- package/src/types/boundaries.ts +0 -35
- package/src/types/cache-types.ts +13 -4
- package/src/types/error-types.ts +30 -90
- package/src/types/global-namespace.ts +54 -41
- package/src/types/handler-context.ts +97 -22
- package/src/types/index.ts +1 -10
- package/src/types/loader-types.ts +6 -3
- package/src/types/request-scope.ts +0 -19
- package/src/types/route-config.ts +6 -50
- package/src/types/route-entry.ts +0 -6
- package/src/types/segments.ts +18 -14
- package/src/urls/include-helper.ts +9 -56
- package/src/urls/index.ts +1 -11
- package/src/urls/path-helper-types.ts +19 -5
- package/src/urls/path-helper.ts +17 -106
- package/src/urls/pattern-types.ts +36 -19
- package/src/urls/response-types.ts +20 -19
- package/src/urls/type-extraction.ts +58 -139
- package/src/urls/urls-function.ts +1 -18
- package/src/use-loader.tsx +292 -107
- package/src/vite/debug.ts +1 -0
- package/src/vite/discovery/bundle-postprocess.ts +8 -7
- package/src/vite/discovery/discover-routers.ts +95 -82
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/prerender-collection.ts +26 -34
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/state.ts +39 -1
- package/src/vite/discovery/virtual-module-codegen.ts +14 -34
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +185 -10
- package/src/vite/plugins/cjs-to-esm.ts +3 -18
- package/src/vite/plugins/client-ref-dedup.ts +0 -11
- package/src/vite/plugins/client-ref-hashing.ts +12 -11
- package/src/vite/plugins/cloudflare-protocol-stub.ts +1 -21
- package/src/vite/plugins/expose-action-id.ts +4 -75
- package/src/vite/plugins/expose-id-utils.ts +3 -54
- package/src/vite/plugins/expose-ids/export-analysis.ts +76 -34
- package/src/vite/plugins/expose-ids/handler-transform.ts +6 -74
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -20
- package/src/vite/plugins/expose-ids/router-transform.ts +0 -13
- package/src/vite/plugins/expose-internal-ids.ts +57 -67
- package/src/vite/plugins/performance-tracks.ts +9 -16
- package/src/vite/plugins/refresh-cmd.ts +1 -1
- package/src/vite/plugins/use-cache-transform.ts +26 -49
- package/src/vite/plugins/vercel-output.ts +258 -0
- package/src/vite/plugins/version-injector.ts +2 -32
- package/src/vite/plugins/version-plugin.ts +32 -23
- package/src/vite/plugins/virtual-entries.ts +35 -17
- package/src/vite/rango.ts +148 -115
- package/src/vite/router-discovery.ts +220 -68
- package/src/vite/utils/ast-handler-extract.ts +15 -31
- package/src/vite/utils/bundle-analysis.ts +10 -15
- package/src/vite/utils/client-chunks.ts +184 -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 +1 -73
- package/src/vite/utils/prerender-utils.ts +0 -34
- package/src/vite/utils/shared-utils.ts +95 -43
- package/src/browser/action-response-classifier.ts +0 -99
- 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/router/middleware-cookies.ts +0 -55
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: css
|
|
3
|
+
description: Import and apply CSS in a Rango app. Render document/app stylesheets in the Document `<head>` with Vite's `?url` import plus a `precedence`-managed `<link rel="stylesheet">` (React 19 resource model — deduped, ordered, loaded before paint). Use when wiring global/app CSS or a Document `<head>` stylesheet, or when deciding between `?url` + `<link>` and side-effect imports. Cross-app (host-router) navigation is a full document load, so each app's document CSS is always re-established by its own load.
|
|
4
|
+
argument-hint:
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# CSS imports
|
|
8
|
+
|
|
9
|
+
Document/app CSS in Rango lives in the Document `<head>`, loaded with Vite's
|
|
10
|
+
`?url` import and a `precedence`-managed `<link rel="stylesheet">`. This page is
|
|
11
|
+
the why and the one cross-app caveat; `/tailwind` is the concrete setup, `/theme`
|
|
12
|
+
is dark mode, `/fonts` is fonts.
|
|
13
|
+
|
|
14
|
+
## The pattern
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
// document.tsx
|
|
18
|
+
"use client";
|
|
19
|
+
|
|
20
|
+
import type { ReactNode } from "react";
|
|
21
|
+
import { MetaTags } from "@rangojs/router/client";
|
|
22
|
+
import styles from "./index.css?url";
|
|
23
|
+
|
|
24
|
+
export function Document({ children }: { children: ReactNode }) {
|
|
25
|
+
return (
|
|
26
|
+
<html lang="en">
|
|
27
|
+
<head>
|
|
28
|
+
<link rel="preload" href={styles} as="style" precedence="default" />
|
|
29
|
+
<link rel="stylesheet" href={styles} precedence="default" />
|
|
30
|
+
<MetaTags />
|
|
31
|
+
</head>
|
|
32
|
+
<body>{children}</body>
|
|
33
|
+
</html>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- **`?url`** returns the processed file's hashed URL instead of injecting it as a
|
|
39
|
+
side effect, giving a stable asset path that works in dev and production.
|
|
40
|
+
- **`precedence`** opts the `<link rel="stylesheet">` into React 19's managed
|
|
41
|
+
stylesheet model: React de-duplicates it by `href`, orders it by precedence
|
|
42
|
+
(any string value — it only decides cascade order relative to other managed
|
|
43
|
+
sheets), and loads it **before paint**, so there is no flash of unstyled
|
|
44
|
+
content. This is the recommended way to render a stylesheet link.
|
|
45
|
+
|
|
46
|
+
## Cross-app (host-router) navigation
|
|
47
|
+
|
|
48
|
+
You do **not** need to coordinate CSS across apps mounted under one host router.
|
|
49
|
+
A client-side navigation that crosses an app boundary is a **full document load**
|
|
50
|
+
(the server returns `X-RSC-Reload` on an app switch — see `/host-router`), so the
|
|
51
|
+
target app's entire document — its stylesheets, theme, meta — is re-established
|
|
52
|
+
by the target app's own load. Each app owns its document; how one app renders a
|
|
53
|
+
stylesheet has no effect on another.
|
|
54
|
+
|
|
55
|
+
(This replaced an earlier soft cross-app swap. Under it, a stylesheet shared
|
|
56
|
+
across apps by `href` — classically every app's `@import "tailwindcss"` compiling
|
|
57
|
+
to one hashed asset — could be silently dropped by React's by-`href` resource
|
|
58
|
+
dedup when the apps disagreed on `precedence` (one unmanaged, one managed). The
|
|
59
|
+
full reload removes that footgun entirely, which is the main reason cross-app
|
|
60
|
+
navigation is a hard boundary.)
|
|
61
|
+
|
|
62
|
+
## Side-effect imports vs `?url`
|
|
63
|
+
|
|
64
|
+
A bare `import "./index.css"` (no `?url`, no `<link>`) also produces _managed_ CSS
|
|
65
|
+
— `@vitejs/plugin-rsc` collects it via `import.meta.viteRsc.loadCss` and injects
|
|
66
|
+
it with a precedence. It is fine for **component-local** CSS that loads with its
|
|
67
|
+
client chunk. For **document-level** CSS, prefer the `?url` + `<link precedence>`
|
|
68
|
+
form above: a side-effect import is not guaranteed to be in the initial streamed
|
|
69
|
+
`<head>` (an SSR-streaming caveat), whereas the explicit `<link>` is.
|
|
70
|
+
|
|
71
|
+
## Related
|
|
72
|
+
|
|
73
|
+
- `/tailwind` — Tailwind v4 setup using this pattern.
|
|
74
|
+
- `/host-router` — multi-app routing; why cross-app navigation is a full reload.
|
|
75
|
+
- `/theme` — dark mode / theme attribute.
|
|
76
|
+
- `/fonts` — self-hosted fonts via `@fontsource`.
|
|
@@ -10,73 +10,82 @@ Caches complete HTTP responses (HTML/RSC) at the edge based on Cache-Control hea
|
|
|
10
10
|
|
|
11
11
|
## Setup
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Document caching is a middleware. Add `createDocumentCacheMiddleware()` to the
|
|
14
|
+
router with `.use()`. The cache store it reads from is the app-level store you
|
|
15
|
+
configure on `createRouter({ cache })` (available on the request context as
|
|
16
|
+
`requestCtx._cacheStore`), not a store passed to the middleware.
|
|
14
17
|
|
|
15
18
|
```typescript
|
|
16
19
|
import { createRouter } from "@rangojs/router";
|
|
17
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
createDocumentCacheMiddleware,
|
|
22
|
+
CFCacheStore,
|
|
23
|
+
} from "@rangojs/router/cache";
|
|
18
24
|
import { urlpatterns } from "./urls";
|
|
19
25
|
|
|
20
26
|
const router = createRouter<AppBindings>({
|
|
21
27
|
document: Document,
|
|
22
28
|
urls: urlpatterns,
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
// App-level cache store. The document cache middleware uses this store's
|
|
30
|
+
// getResponse/putResponse methods.
|
|
31
|
+
cache: (_env, ctx) => new CFCacheStore({ ctx: ctx! }),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
router.use(
|
|
35
|
+
createDocumentCacheMiddleware({
|
|
25
36
|
skipPaths: ["/api", "/admin"],
|
|
26
37
|
debug: process.env.NODE_ENV === "development",
|
|
27
38
|
}),
|
|
28
|
-
|
|
39
|
+
);
|
|
29
40
|
|
|
30
41
|
export default router;
|
|
31
42
|
```
|
|
32
43
|
|
|
33
|
-
## Route Opt-In with
|
|
44
|
+
## Route Opt-In with Cache-Control
|
|
34
45
|
|
|
35
|
-
Routes opt-in to document caching
|
|
46
|
+
Routes opt-in to document caching by setting a `Cache-Control` response header
|
|
47
|
+
with `s-maxage`. The middleware caches responses whose `Cache-Control` includes
|
|
48
|
+
`s-maxage`; `stale-while-revalidate` enables background revalidation (SWR).
|
|
36
49
|
|
|
37
50
|
```typescript
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// No cache for dashboard (no documentCache option)
|
|
52
|
-
path("/dashboard", Dashboard, { name: "dashboard" }),
|
|
53
|
-
]);
|
|
51
|
+
// Cache full page for 5 min, serve stale for 1 hour
|
|
52
|
+
function BlogIndexHandler(ctx) {
|
|
53
|
+
ctx.headers.set("Cache-Control", "s-maxage=300, stale-while-revalidate=3600");
|
|
54
|
+
return <BlogIndex />;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Long cache for individual posts
|
|
58
|
+
function BlogPostHandler(ctx) {
|
|
59
|
+
ctx.headers.set("Cache-Control", "s-maxage=3600, stale-while-revalidate=86400");
|
|
60
|
+
return <BlogPost />;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Dashboard sets no Cache-Control header, so it is never document-cached.
|
|
54
64
|
```
|
|
55
65
|
|
|
56
66
|
## Document Cache Options
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
createRouter({
|
|
60
|
-
// ...
|
|
61
|
-
documentCache: (_env, ctx) => ({
|
|
62
|
-
// Cache store (required)
|
|
63
|
-
store: new CFCacheStore({ ctx: ctx! }),
|
|
68
|
+
`createDocumentCacheMiddleware(options?)` accepts:
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
```typescript
|
|
71
|
+
createDocumentCacheMiddleware({
|
|
72
|
+
// Skip specific paths (matched by pathname prefix)
|
|
73
|
+
skipPaths: ["/api", "/admin"],
|
|
67
74
|
|
|
68
|
-
|
|
69
|
-
|
|
75
|
+
// Custom cache key generator
|
|
76
|
+
keyGenerator: (url) => url.pathname,
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
// Conditional caching, evaluated per request
|
|
79
|
+
isEnabled: (ctx) => !ctx.request.headers.has("x-preview"),
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}),
|
|
81
|
+
// Debug logging (HIT, MISS, STALE, REVALIDATED)
|
|
82
|
+
debug: true,
|
|
77
83
|
});
|
|
78
84
|
```
|
|
79
85
|
|
|
86
|
+
The cache store is not a middleware option — it comes from the app-level
|
|
87
|
+
`createRouter({ cache })` store.
|
|
88
|
+
|
|
80
89
|
## How It Works
|
|
81
90
|
|
|
82
91
|
```
|
|
@@ -89,7 +98,7 @@ Request → Check Cache
|
|
|
89
98
|
↓ ↓
|
|
90
99
|
Fresh? Run handler
|
|
91
100
|
│ │
|
|
92
|
-
Yes → Return Has
|
|
101
|
+
Yes → Return Has s-maxage?
|
|
93
102
|
│ │
|
|
94
103
|
No (stale) Yes → Cache + Return
|
|
95
104
|
│ │
|
|
@@ -120,13 +129,14 @@ Segment hash ensures different cached responses for navigations from different s
|
|
|
120
129
|
|
|
121
130
|
- Full HTML responses (document requests)
|
|
122
131
|
- RSC payloads (client navigation)
|
|
123
|
-
- Only 200 OK responses
|
|
132
|
+
- Only 200 OK responses whose `Cache-Control` includes `s-maxage`
|
|
124
133
|
|
|
125
134
|
## What's NOT Cached
|
|
126
135
|
|
|
127
136
|
- Server actions (`_rsc_action`)
|
|
128
137
|
- Loader requests (`_rsc_loader`)
|
|
129
|
-
-
|
|
138
|
+
- Non-GET requests
|
|
139
|
+
- Responses without an `s-maxage` `Cache-Control` directive
|
|
130
140
|
- Non-200 responses
|
|
131
141
|
|
|
132
142
|
## Complete Example
|
|
@@ -134,40 +144,53 @@ Segment hash ensures different cached responses for navigations from different s
|
|
|
134
144
|
```typescript
|
|
135
145
|
// router.tsx
|
|
136
146
|
import { createRouter } from "@rangojs/router";
|
|
137
|
-
import { CFCacheStore } from "@rangojs/router/cache";
|
|
147
|
+
import { createDocumentCacheMiddleware, CFCacheStore } from "@rangojs/router/cache";
|
|
138
148
|
import { urlpatterns } from "./urls";
|
|
139
149
|
|
|
140
150
|
const router = createRouter<AppBindings>({
|
|
141
151
|
document: Document,
|
|
142
152
|
urls: urlpatterns,
|
|
143
|
-
|
|
144
|
-
|
|
153
|
+
cache: (_env, ctx) => new CFCacheStore({ ctx: ctx! }),
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
router.use(
|
|
157
|
+
createDocumentCacheMiddleware({
|
|
145
158
|
skipPaths: ["/api"],
|
|
146
159
|
debug: process.env.NODE_ENV === "development",
|
|
147
160
|
}),
|
|
148
|
-
|
|
161
|
+
);
|
|
149
162
|
|
|
150
163
|
export default router;
|
|
151
164
|
|
|
152
165
|
// urls.tsx
|
|
153
166
|
import { urls } from "@rangojs/router";
|
|
154
167
|
|
|
155
|
-
export const urlpatterns = urls(({ path, layout,
|
|
156
|
-
// Blog
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
]),
|
|
168
|
+
export const urlpatterns = urls(({ path, layout, loader }) => [
|
|
169
|
+
// Blog pages opt into document caching via Cache-Control headers set in
|
|
170
|
+
// their handlers (see BlogIndex / BlogPost below).
|
|
171
|
+
layout(<BlogLayout />, () => [
|
|
172
|
+
path("/blog", BlogIndex, { name: "blog" }),
|
|
173
|
+
path("/blog/:slug", BlogPost, { name: "blogPost" }, () => [
|
|
174
|
+
loader(BlogPostLoader),
|
|
163
175
|
]),
|
|
164
176
|
]),
|
|
165
177
|
|
|
166
|
-
// Dashboard
|
|
178
|
+
// Dashboard sets no Cache-Control header, so it is never document-cached.
|
|
167
179
|
layout(<DashboardLayout />, () => [
|
|
168
180
|
path("/dashboard", Dashboard, { name: "dashboard" }),
|
|
169
181
|
]),
|
|
170
182
|
]);
|
|
183
|
+
|
|
184
|
+
// Blog handlers set s-maxage to opt into the document cache.
|
|
185
|
+
function BlogIndex(ctx) {
|
|
186
|
+
ctx.headers.set("Cache-Control", "s-maxage=300, stale-while-revalidate=3600");
|
|
187
|
+
return <BlogIndexPage />;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function BlogPost(ctx) {
|
|
191
|
+
ctx.headers.set("Cache-Control", "s-maxage=300, stale-while-revalidate=3600");
|
|
192
|
+
return <BlogPostPage />;
|
|
193
|
+
}
|
|
171
194
|
```
|
|
172
195
|
|
|
173
196
|
## Document Cache vs Segment Cache
|
|
@@ -175,7 +198,7 @@ export const urlpatterns = urls(({ path, layout, cache, loader }) => [
|
|
|
175
198
|
| Feature | Document Cache | Segment Cache |
|
|
176
199
|
| ------------ | -------------------------- | --------------------- |
|
|
177
200
|
| Granularity | Full response | Individual segments |
|
|
178
|
-
| Opt-in | `
|
|
201
|
+
| Opt-in | `Cache-Control` `s-maxage` | `cache({ ttl, swr })` |
|
|
179
202
|
| Use case | Static pages | Dynamic compositions |
|
|
180
203
|
| Key includes | URL + segment hash | Route params |
|
|
181
204
|
|
|
@@ -59,6 +59,8 @@ Now `ProductPage` carries its loader, loading state, and response-header middlew
|
|
|
59
59
|
| `intercept()` | `middleware`, `revalidate`, `loader`, `loading`, `errorBoundary`, `notFoundBoundary`, `layout`, `route`, `when`, `transition` |
|
|
60
60
|
| Response routes (`path.json()`, `path.text()`, …) | `middleware`, `cache` |
|
|
61
61
|
|
|
62
|
+
For per-item semantics see the dedicated skills: [middleware](../middleware/SKILL.md), [loader](../loader/SKILL.md), [parallel](../parallel/SKILL.md), [intercept](../intercept/SKILL.md), [layout](../layout/SKILL.md), [view-transitions](../view-transitions/SKILL.md).
|
|
63
|
+
|
|
62
64
|
If `handler.use()` returns a disallowed item for a mount site, registration throws:
|
|
63
65
|
|
|
64
66
|
```
|
|
@@ -295,7 +297,7 @@ QuickViewModal.use = () => [
|
|
|
295
297
|
|
|
296
298
|
## `loading()` is a single-assignment item — scope it correctly
|
|
297
299
|
|
|
298
|
-
Most `use` items accumulate when merged: `handler.use` `middleware()` runs _and_ explicit `middleware()` runs; both `loader()` registrations apply. `loading()` is different — it mutates `entry.loading` directly, last call wins ([dsl-helpers.ts `
|
|
300
|
+
Most `use` items accumulate when merged: `handler.use` `middleware()` runs _and_ explicit `middleware()` runs; both `loader()` registrations apply. `loading()` is different — it mutates `entry.loading` directly, last call wins ([dsl-helpers.ts `loading`](../../src/route-definition/dsl-helpers.ts)).
|
|
299
301
|
|
|
300
302
|
For pages, layouts, and intercepts that's straightforward: explicit `loading()` at the mount site replaces any `loading()` from `handler.use`. The merge order is `handler.use → explicit`, so the explicit one is the last writer and wins.
|
|
301
303
|
|
package/skills/hooks/SKILL.md
CHANGED
|
@@ -89,8 +89,8 @@ import { useSegments } from "@rangojs/router/client";
|
|
|
89
89
|
function Breadcrumbs() {
|
|
90
90
|
const { path, segmentIds, location } = useSegments();
|
|
91
91
|
|
|
92
|
-
// path: ["
|
|
93
|
-
// segmentIds: ["
|
|
92
|
+
// path: ["shop", "products", "123"] (split on "/", no leading slash on any element)
|
|
93
|
+
// segmentIds: ["L0", "L0L1", "L0L1R0"] (opaque internal short-codes, not route names)
|
|
94
94
|
// location: URL object
|
|
95
95
|
|
|
96
96
|
return <nav>{path.join(" > ")}</nav>;
|
|
@@ -190,6 +190,141 @@ function SearchResults() {
|
|
|
190
190
|
}
|
|
191
191
|
```
|
|
192
192
|
|
|
193
|
+
**Shared refetch behavior**:
|
|
194
|
+
|
|
195
|
+
When the loader is registered on the route via `loader()`, a plain
|
|
196
|
+
`load()` call (no options, or a trivially-defaulted GET with no
|
|
197
|
+
`params` and no `body`) broadcasts its result to every component
|
|
198
|
+
reading the same loader id. Layout, page, and parallel-slot reads
|
|
199
|
+
all converge on the new value:
|
|
200
|
+
|
|
201
|
+
```tsx
|
|
202
|
+
// Layout button calls load() — the page read below sees the update too.
|
|
203
|
+
function Layout() {
|
|
204
|
+
const { data, load } = useLoader(CartLoader);
|
|
205
|
+
return <button onClick={() => load()}>Refresh ({data.count})</button>;
|
|
206
|
+
}
|
|
207
|
+
function Page() {
|
|
208
|
+
const { data } = useLoader(CartLoader); // updates with the layout's load()
|
|
209
|
+
return <span>{data.count} items</span>;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`isLoading` and `error` follow the same scope. `throwOnError: true`
|
|
214
|
+
render-throws are scoped to the **originating** hook — sibling readers
|
|
215
|
+
see the error in their `error` state but their boundaries are not
|
|
216
|
+
triggered by someone else's failure. A successful follow-up `load()`
|
|
217
|
+
clears the shared error.
|
|
218
|
+
|
|
219
|
+
**`load()` calls that stay local** (no broadcast, per-hook state, same
|
|
220
|
+
semantics as the old per-component `useState`):
|
|
221
|
+
|
|
222
|
+
- `load({ params: { ... } })` — explicit params.
|
|
223
|
+
- `load({ method: "POST", body })` — mutations.
|
|
224
|
+
- Any `load()` on a `useFetchLoader(loader)` whose loader is **not**
|
|
225
|
+
registered on the current route. Two unrelated components calling
|
|
226
|
+
`load()` on the same fetchable-but-unregistered loader keep
|
|
227
|
+
independent results.
|
|
228
|
+
|
|
229
|
+
So the search/list pattern still works — two components calling
|
|
230
|
+
`load({ params: { q } })` with different `q` values each keep their
|
|
231
|
+
own result; they do not collapse to last-write-wins through a shared
|
|
232
|
+
store.
|
|
233
|
+
|
|
234
|
+
**Scoping refetch with a `key`**:
|
|
235
|
+
|
|
236
|
+
Pass a `key` to partition the shared refresh store. Only hooks using the
|
|
237
|
+
**same** `key` refresh together when one of them calls `load()`. This is a
|
|
238
|
+
client-side refresh identity only — it never changes the request sent to the
|
|
239
|
+
server, and is unrelated to the server `cache({ key })` option and to
|
|
240
|
+
`revalidate()`.
|
|
241
|
+
|
|
242
|
+
```tsx
|
|
243
|
+
// Two independent dashboards using the same loader. Without a key, one
|
|
244
|
+
// dashboard's load() would flip the other's spinner and value. With a key,
|
|
245
|
+
// they refresh independently.
|
|
246
|
+
function Dashboard({ id }: { id: string }) {
|
|
247
|
+
const { data, load } = useLoader(StatsLoader, { key: `dashboard:${id}` });
|
|
248
|
+
return <button onClick={() => load()}>Refresh {data.total}</button>;
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
The `key` widens sharing in two ways the default cannot:
|
|
253
|
+
|
|
254
|
+
- **Parameterized GETs share.** `useFetchLoader(SearchLoader, { key: q })`
|
|
255
|
+
with the same `q` in two components share one result and refresh together —
|
|
256
|
+
a keyed `load({ params: { q } })` broadcasts to the group instead of staying
|
|
257
|
+
local. (Mutations — non-GET or `body` — stay local even with a key.)
|
|
258
|
+
- **Unregistered loaders share.** A `key` makes `useFetchLoader` of a loader
|
|
259
|
+
that is **not** registered on the route share too, letting unrelated
|
|
260
|
+
components opt into a common refresh group.
|
|
261
|
+
|
|
262
|
+
Lifecycle: a keyed read of an unregistered loader is reference-counted — its
|
|
263
|
+
shared value lives as long as at least one component using that key is mounted.
|
|
264
|
+
A persistent component (e.g. a header) keeps the value across navigations; a
|
|
265
|
+
route-scoped component's value is reclaimed when it unmounts. Registered-loader
|
|
266
|
+
reads (keyed or not) reset on navigation from fresh route data, as before.
|
|
267
|
+
|
|
268
|
+
**Refreshing multiple loaders together (`refreshGroup` + `useRefreshLoaders`)**:
|
|
269
|
+
|
|
270
|
+
`key` groups readers of one loader. To refresh **different** loaders together,
|
|
271
|
+
tag them with a shared `refreshGroup` name and trigger them with
|
|
272
|
+
`useRefreshLoaders()`. The hook takes no argument; you pass the group(s) to the
|
|
273
|
+
function it returns, so one `useRefreshLoaders()` can refresh different groups
|
|
274
|
+
depending on context. A read may carry **several** tags — pass an array — and is
|
|
275
|
+
refreshed when **any** of its groups is refreshed:
|
|
276
|
+
|
|
277
|
+
```tsx
|
|
278
|
+
function Profile() {
|
|
279
|
+
const { data } = useLoader(ProfileLoader, {
|
|
280
|
+
key: userId,
|
|
281
|
+
refreshGroup: "account",
|
|
282
|
+
});
|
|
283
|
+
return <span>{data.name}</span>;
|
|
284
|
+
}
|
|
285
|
+
function Orders() {
|
|
286
|
+
// Tagged into two groups: refreshed by "account" (the whole set) or the
|
|
287
|
+
// finer "orders" tag.
|
|
288
|
+
const { data } = useLoader(OrdersLoader, {
|
|
289
|
+
key: userId,
|
|
290
|
+
refreshGroup: ["account", "orders"],
|
|
291
|
+
});
|
|
292
|
+
return <span>{data.count} orders</span>;
|
|
293
|
+
}
|
|
294
|
+
function RefreshButtons() {
|
|
295
|
+
const refresh = useRefreshLoaders();
|
|
296
|
+
return (
|
|
297
|
+
<>
|
|
298
|
+
<button onClick={() => refresh("account")}>Refresh account</button>
|
|
299
|
+
<button onClick={() => refresh("orders")}>Refresh orders only</button>
|
|
300
|
+
<button onClick={() => refresh(["account", "orders"])}>
|
|
301
|
+
Refresh both
|
|
302
|
+
</button>
|
|
303
|
+
</>
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
`refresh(groups)` accepts one name or an array and re-runs every currently-mounted
|
|
309
|
+
member tagged with **any** of them, with a **plain GET** against the current route
|
|
310
|
+
URL — no params, no body, no mutation methods, because a group spans loaders with
|
|
311
|
+
different shapes. A member that sits in two of the requested groups is fetched
|
|
312
|
+
once (members are unioned and deduped by read). It returns a promise that resolves
|
|
313
|
+
when all members settle and **rejects with an `AggregateError`** if any fail;
|
|
314
|
+
group refresh never render-throws, so handle failures at the await site
|
|
315
|
+
(`await refresh("account").catch(...)`). Each failing member also exposes its
|
|
316
|
+
error via its own read's `error`.
|
|
317
|
+
|
|
318
|
+
Multiple tags give you granular vs. whole-set refresh from one place: a coarse
|
|
319
|
+
tag (`"account"`) covers everything, while a finer tag (`"orders"`) targets a
|
|
320
|
+
subset. Sharing within a group is opt-in via `key`: members that share a `key`
|
|
321
|
+
share one value (and one fetch); a grouped reader **without** a `key` gets its own
|
|
322
|
+
private bucket, so a group refresh updates only that read and never leaks into
|
|
323
|
+
unrelated unkeyed reads of the same loader. A bucket may belong to several groups
|
|
324
|
+
at once (one read tagged with multiple names, or different reads tagging the same
|
|
325
|
+
keyed bucket with different names). Keep parameterized loaders on the single-loader
|
|
326
|
+
`key` — a plain-GET group refresh sends no params.
|
|
327
|
+
|
|
193
328
|
**Load options**:
|
|
194
329
|
|
|
195
330
|
```tsx
|
|
@@ -511,33 +646,81 @@ const flash = FlashMessage.read();
|
|
|
511
646
|
const product = ProductState.read();
|
|
512
647
|
```
|
|
513
648
|
|
|
514
|
-
|
|
649
|
+
> **Hydration:** `.read()` returns `undefined` on the server but may return
|
|
650
|
+
> a real value on the first client render (history state survives reload).
|
|
651
|
+
> Do not call `.read()` directly during the initial render of a component;
|
|
652
|
+
> call it from an event handler or inside a `useEffect` post-mount. For
|
|
653
|
+
> reactive hydration-safe access, use `useLocationState()` instead.
|
|
654
|
+
|
|
655
|
+
### .write() / .delete() (static, non-reactive)
|
|
515
656
|
|
|
516
|
-
|
|
657
|
+
Static counterparts to `.read()`. Both mutate the current history entry's
|
|
658
|
+
`history.state` via `replaceState`, preserving any other keys (router
|
|
659
|
+
bookkeeping, other location state slots). Both are client-only; they throw
|
|
660
|
+
when called on the server.
|
|
517
661
|
|
|
518
|
-
|
|
662
|
+
Neither dispatches an event, so components reading via `useLocationState`
|
|
663
|
+
will NOT re-render until the next navigation/popstate. Pair with `.read()`
|
|
664
|
+
(or a fresh mount via back/forward/reload) instead.
|
|
519
665
|
|
|
520
666
|
```tsx
|
|
521
667
|
"use client";
|
|
522
|
-
import {
|
|
668
|
+
import { ProductState } from "./state";
|
|
523
669
|
|
|
524
|
-
|
|
525
|
-
|
|
670
|
+
// Persisted across hard refresh and back/forward of this entry.
|
|
671
|
+
ProductState.write({ name: "Widget", price: 9.99 });
|
|
672
|
+
|
|
673
|
+
// Read later (or on next mount).
|
|
674
|
+
const current = ProductState.read();
|
|
675
|
+
|
|
676
|
+
// Manually clear the slot. Idempotent if it isn't set.
|
|
677
|
+
ProductState.delete();
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
| Method | Updates `history.state` | Fires `useLocationState` rerender | SSR behavior |
|
|
681
|
+
| ----------- | ----------------------- | --------------------------------- | ------------------- |
|
|
682
|
+
| `.read()` | no | n/a (returns snapshot) | returns `undefined` |
|
|
683
|
+
| `.write()` | yes (replace this slot) | no | throws |
|
|
684
|
+
| `.delete()` | yes (remove this slot) | no | throws |
|
|
526
685
|
|
|
686
|
+
## Cache Control
|
|
687
|
+
|
|
688
|
+
### invalidateClientCache()
|
|
689
|
+
|
|
690
|
+
Force the client's caches to miss after a mutation the router can't see (a REST
|
|
691
|
+
call, a WebSocket push, a login). It is a plain function, not a hook, so it works
|
|
692
|
+
from module-level callbacks too. Imported from the root entry `@rangojs/router`,
|
|
693
|
+
it is selected by export conditions: in a client component it marks the caches
|
|
694
|
+
stale immediately; from a handler/server component it writes a rotated
|
|
695
|
+
`Set-Cookie` for the responding client.
|
|
696
|
+
|
|
697
|
+
```tsx
|
|
698
|
+
"use client";
|
|
699
|
+
import { invalidateClientCache } from "@rangojs/router";
|
|
700
|
+
|
|
701
|
+
function SaveButton() {
|
|
527
702
|
const handleSave = async () => {
|
|
528
703
|
await fetch("/api/data", {
|
|
529
704
|
method: "POST",
|
|
530
705
|
body: JSON.stringify(data),
|
|
531
706
|
});
|
|
532
707
|
|
|
533
|
-
// Invalidate
|
|
534
|
-
|
|
708
|
+
// Invalidate the client's caches after the mutation
|
|
709
|
+
invalidateClientCache();
|
|
535
710
|
};
|
|
536
711
|
|
|
537
712
|
return <button onClick={handleSave}>Save</button>;
|
|
538
713
|
}
|
|
539
714
|
```
|
|
540
715
|
|
|
716
|
+
A module-level subscription works the same way (no component needed):
|
|
717
|
+
|
|
718
|
+
```ts
|
|
719
|
+
import { invalidateClientCache } from "@rangojs/router";
|
|
720
|
+
|
|
721
|
+
socket.on("catalog-updated", () => invalidateClientCache());
|
|
722
|
+
```
|
|
723
|
+
|
|
541
724
|
**Use cases**: REST API mutations, WebSocket updates, non-RSC data changes.
|
|
542
725
|
|
|
543
726
|
## Outlet Components
|
|
@@ -694,24 +877,48 @@ function MountInfo() {
|
|
|
694
877
|
}
|
|
695
878
|
```
|
|
696
879
|
|
|
697
|
-
|
|
880
|
+
### useReverse(routes)
|
|
881
|
+
|
|
882
|
+
Mount-aware local reverse for client components. Import the generated `routes` map from a `urls()` module's `.gen.ts` and call `reverse("name", params?)` — the leading dot is optional. Auto-fills params from `useParams()`; explicit params override.
|
|
883
|
+
|
|
884
|
+
> Per-module `*.gen.ts` files are **CLI opt-in and not Vite-watched** — run `rango generate <urls-file>` (or wire it into `predev`) and re-run it whenever the module's routes change. See `/links` for the full generated-file setup and exposure-boundary rules.
|
|
885
|
+
|
|
886
|
+
```tsx
|
|
887
|
+
"use client";
|
|
888
|
+
import { Link, useReverse } from "@rangojs/router/client";
|
|
889
|
+
import { routes as blogRoutes } from "../urls/blog.gen.js";
|
|
890
|
+
|
|
891
|
+
function BlogNav() {
|
|
892
|
+
const reverse = useReverse(blogRoutes);
|
|
893
|
+
return (
|
|
894
|
+
<nav>
|
|
895
|
+
<Link to={reverse("index")}>Blog</Link>
|
|
896
|
+
<Link to={reverse("post", { postId: "hello" })}>Post</Link>
|
|
897
|
+
</nav>
|
|
898
|
+
);
|
|
899
|
+
}
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
See `/links` for the full URL generation guide. `ctx.reverse()` is server-only; on the client, prefer `useReverse(routes)` for in-module names and pass URLs as props for cross-module ones.
|
|
698
903
|
|
|
699
904
|
## Hook Summary
|
|
700
905
|
|
|
701
|
-
| Hook
|
|
702
|
-
|
|
|
703
|
-
| `useParams()`
|
|
704
|
-
| `usePathname()`
|
|
705
|
-
| `useSearchParams()`
|
|
706
|
-
| `useHref()`
|
|
707
|
-
| `useMount()`
|
|
708
|
-
| `
|
|
709
|
-
| `
|
|
710
|
-
| `
|
|
711
|
-
| `
|
|
712
|
-
| `
|
|
713
|
-
| `
|
|
714
|
-
| `
|
|
715
|
-
| `
|
|
716
|
-
| `
|
|
717
|
-
| `
|
|
906
|
+
| Hook | Purpose | Returns |
|
|
907
|
+
| ------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------ |
|
|
908
|
+
| `useParams()` | Route params | `Readonly<T>` (default `Record<string, string>`) or selected value |
|
|
909
|
+
| `usePathname()` | Current pathname | `string` |
|
|
910
|
+
| `useSearchParams()` | URL search params | `ReadonlyURLSearchParams` |
|
|
911
|
+
| `useHref()` | Mount-aware href | `(path) => string` |
|
|
912
|
+
| `useMount()` | Current include() mount path | `string` |
|
|
913
|
+
| `useReverse()` | Local reverse for imported routes | `(name, params?, search?) => string` |
|
|
914
|
+
| `useNavigation()` | Reactive navigation state | state, location, isStreaming |
|
|
915
|
+
| `useRouter()` | Stable router actions | push, replace, refresh, prefetch, back, forward |
|
|
916
|
+
| `useSegments()` | URL path & segment IDs | path, segmentIds, location |
|
|
917
|
+
| `useLinkStatus()` | Link pending state | { pending } |
|
|
918
|
+
| `useLoader()` | Loader data (strict) | data, isLoading, error |
|
|
919
|
+
| `useFetchLoader()` | Loader with on-demand fetch | data, load, isLoading |
|
|
920
|
+
| `useRefreshLoaders()` | Refresh cross-loader group(s) | `() => (groups: string \| string[]) => Promise<void>` |
|
|
921
|
+
| `useHandle()` | Accumulated handle data | T (handle type) |
|
|
922
|
+
| `useAction()` | Server action state | state, error, result |
|
|
923
|
+
| `useLocationState()` | History state (persists or flash) | T \| undefined |
|
|
924
|
+
| `invalidateClientCache()` | Force client caches to miss (function, not a hook; root entry) | `void` |
|