@rangojs/router 0.0.0-experimental.50 → 0.0.0-experimental.52
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/vite/index.js +17 -2
- package/package.json +1 -1
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +8 -0
- package/skills/loader/SKILL.md +52 -42
- package/skills/route/SKILL.md +31 -0
- package/skills/typesafety/SKILL.md +10 -0
- package/src/browser/prefetch/queue.ts +34 -18
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/cache/taint.ts +55 -0
- package/src/context-var.ts +72 -2
- package/src/route-definition/helpers-types.ts +6 -5
- package/src/router/handler-context.ts +31 -8
- package/src/router/loader-resolution.ts +7 -1
- package/src/router/middleware-types.ts +6 -2
- package/src/router/middleware.ts +2 -2
- package/src/router/segment-resolution/fresh.ts +11 -5
- package/src/router/segment-resolution/revalidation.ts +5 -0
- package/src/server/context.ts +12 -0
- package/src/server/request-context.ts +42 -8
- package/src/types/handler-context.ts +10 -6
- package/src/types/loader-types.ts +4 -4
package/dist/vite/index.js
CHANGED
|
@@ -1745,7 +1745,7 @@ import { resolve } from "node:path";
|
|
|
1745
1745
|
// package.json
|
|
1746
1746
|
var package_default = {
|
|
1747
1747
|
name: "@rangojs/router",
|
|
1748
|
-
version: "0.0.0-experimental.
|
|
1748
|
+
version: "0.0.0-experimental.52",
|
|
1749
1749
|
description: "Django-inspired RSC router with composable URL patterns",
|
|
1750
1750
|
keywords: [
|
|
1751
1751
|
"react",
|
|
@@ -3274,8 +3274,17 @@ function jsonParseExpression(value) {
|
|
|
3274
3274
|
}
|
|
3275
3275
|
|
|
3276
3276
|
// src/context-var.ts
|
|
3277
|
+
var NON_CACHEABLE_KEYS = /* @__PURE__ */ Symbol.for(
|
|
3278
|
+
"rango:non-cacheable-keys"
|
|
3279
|
+
);
|
|
3280
|
+
function getNonCacheableKeys(variables) {
|
|
3281
|
+
if (!variables[NON_CACHEABLE_KEYS]) {
|
|
3282
|
+
variables[NON_CACHEABLE_KEYS] = /* @__PURE__ */ new Set();
|
|
3283
|
+
}
|
|
3284
|
+
return variables[NON_CACHEABLE_KEYS];
|
|
3285
|
+
}
|
|
3277
3286
|
var FORBIDDEN_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
3278
|
-
function contextSet(variables, keyOrVar, value) {
|
|
3287
|
+
function contextSet(variables, keyOrVar, value, options) {
|
|
3279
3288
|
if (typeof keyOrVar === "string") {
|
|
3280
3289
|
if (FORBIDDEN_KEYS.has(keyOrVar)) {
|
|
3281
3290
|
throw new Error(
|
|
@@ -3283,8 +3292,14 @@ function contextSet(variables, keyOrVar, value) {
|
|
|
3283
3292
|
);
|
|
3284
3293
|
}
|
|
3285
3294
|
variables[keyOrVar] = value;
|
|
3295
|
+
if (options?.cache === false) {
|
|
3296
|
+
getNonCacheableKeys(variables).add(keyOrVar);
|
|
3297
|
+
}
|
|
3286
3298
|
} else {
|
|
3287
3299
|
variables[keyOrVar.key] = value;
|
|
3300
|
+
if (options?.cache === false) {
|
|
3301
|
+
getNonCacheableKeys(variables).add(keyOrVar.key);
|
|
3302
|
+
}
|
|
3288
3303
|
}
|
|
3289
3304
|
}
|
|
3290
3305
|
|
package/package.json
CHANGED
|
@@ -162,6 +162,38 @@ middleware(async (ctx, next) => {
|
|
|
162
162
|
});
|
|
163
163
|
```
|
|
164
164
|
|
|
165
|
+
## Context Variable Cache Safety
|
|
166
|
+
|
|
167
|
+
Context variables created with `createVar()` are cacheable by default and can
|
|
168
|
+
be read freely inside `cache()` and `"use cache"` scopes. Non-cacheable vars
|
|
169
|
+
throw at read time to prevent request-specific data from being captured.
|
|
170
|
+
|
|
171
|
+
There are two ways to mark a value as non-cacheable:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
// Var-level policy — inherently request-specific data
|
|
175
|
+
const Session = createVar<SessionData>({ cache: false });
|
|
176
|
+
|
|
177
|
+
// Write-level escalation — this specific write is non-cacheable
|
|
178
|
+
ctx.set(Theme, derivedTheme, { cache: false });
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
"Least cacheable wins": if either the var definition or the `ctx.set()` call
|
|
182
|
+
specifies `cache: false`, the value is non-cacheable.
|
|
183
|
+
|
|
184
|
+
**Behavior inside cache scopes:**
|
|
185
|
+
|
|
186
|
+
| Operation | Inside `cache()` / `"use cache"` |
|
|
187
|
+
| ----------------------------------- | -------------------------------- |
|
|
188
|
+
| `ctx.get(cacheableVar)` | Allowed |
|
|
189
|
+
| `ctx.get(nonCacheableVar)` | Throws |
|
|
190
|
+
| `ctx.set(var, value)` (cacheable) | Allowed |
|
|
191
|
+
| `ctx.header()`, `ctx.cookie()`, etc | Throws (response side effects) |
|
|
192
|
+
|
|
193
|
+
Write is dumb — `ctx.set()` stores the cache metadata but does not enforce.
|
|
194
|
+
Enforcement happens at read time (`ctx.get()`), where ALS detects the cache
|
|
195
|
+
scope and rejects non-cacheable reads.
|
|
196
|
+
|
|
165
197
|
## Loaders Are Always Fresh
|
|
166
198
|
|
|
167
199
|
Loaders are **never cached** by route-level `cache()`. Even on a full cache hit
|
package/skills/caching/SKILL.md
CHANGED
|
@@ -173,6 +173,14 @@ const router = createRouter<AppBindings>({
|
|
|
173
173
|
KV entries require `expirationTtl >= 60s`. Short-lived entries (< 60s total TTL)
|
|
174
174
|
are only cached in L1.
|
|
175
175
|
|
|
176
|
+
## Context Variables Inside Cache Boundaries
|
|
177
|
+
|
|
178
|
+
Context variables (`createVar`) are cacheable by default and can be read and
|
|
179
|
+
written inside `cache()` scopes. Variables marked with `{ cache: false }` (at
|
|
180
|
+
the var level or write level) throw when read inside a cache scope. Response
|
|
181
|
+
side effects (`ctx.header()`, `ctx.cookie()`) always throw inside cache
|
|
182
|
+
boundaries. See `/cache-guide` for the full cache safety table.
|
|
183
|
+
|
|
176
184
|
## Nested Cache Boundaries
|
|
177
185
|
|
|
178
186
|
Override cache settings for specific sections:
|
package/skills/loader/SKILL.md
CHANGED
|
@@ -65,24 +65,10 @@ export const urlpatterns = urls(({ path, loader }) => [
|
|
|
65
65
|
|
|
66
66
|
## Consuming Loader Data
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
> **IMPORTANT: Prefer consuming loaders in client components.** Keeping data
|
|
73
|
-
> fetching in loaders and consumption in client components creates a clean
|
|
74
|
-
> separation: the server-side handler renders static markup that can be
|
|
75
|
-
> freely cached with `cache()`, while loader data stays fresh on every
|
|
76
|
-
> request. When you consume loaders in server handlers via `ctx.use()`, the
|
|
77
|
-
> handler output depends on the loader data, which means caching the handler
|
|
78
|
-
> also caches the data — defeating the purpose of the live data layer.
|
|
79
|
-
|
|
80
|
-
### In Client Components (Preferred)
|
|
81
|
-
|
|
82
|
-
Client components use `useLoader()` from `@rangojs/router/client`.
|
|
83
|
-
The loader **must** be registered with `loader()` in the route's DSL
|
|
84
|
-
segments so the framework knows to resolve it during SSR and stream
|
|
85
|
-
the data to the client:
|
|
68
|
+
Register loaders with `loader()` in the DSL and consume them in client
|
|
69
|
+
components with `useLoader()`. This is the recommended pattern — it keeps
|
|
70
|
+
data fetching on the server and consumption on the client, with a clean
|
|
71
|
+
separation that works correctly with `cache()`.
|
|
86
72
|
|
|
87
73
|
```typescript
|
|
88
74
|
"use client";
|
|
@@ -96,40 +82,60 @@ function ProductDetails() {
|
|
|
96
82
|
```
|
|
97
83
|
|
|
98
84
|
```typescript
|
|
99
|
-
// Route definition — loader() registration required
|
|
85
|
+
// Route definition — loader() registration required
|
|
100
86
|
path("/product/:slug", ProductPage, { name: "product" }, () => [
|
|
101
|
-
loader(ProductLoader),
|
|
87
|
+
loader(ProductLoader),
|
|
102
88
|
]);
|
|
103
89
|
```
|
|
104
90
|
|
|
105
|
-
|
|
91
|
+
DSL loaders are the **live data layer** — they resolve fresh on every
|
|
92
|
+
request, even when the route is inside a `cache()` boundary. The router
|
|
93
|
+
excludes them from the segment cache at storage time and re-resolves them
|
|
94
|
+
on retrieval. This means `cache()` gives you cached UI + fresh data by
|
|
95
|
+
default.
|
|
106
96
|
|
|
107
|
-
|
|
108
|
-
This doesn't require `loader()` registration in the DSL — it works
|
|
109
|
-
standalone. **However**, prefer client-side consumption when possible (see
|
|
110
|
-
note above).
|
|
97
|
+
### Cache safety
|
|
111
98
|
|
|
112
|
-
|
|
113
|
-
|
|
99
|
+
DSL loaders can safely read `createVar({ cache: false })` variables
|
|
100
|
+
because they are always resolved fresh. The read guard is bypassed for
|
|
101
|
+
loader functions — they never produce stale data.
|
|
102
|
+
|
|
103
|
+
### ctx.use(Loader) — escape hatch
|
|
104
|
+
|
|
105
|
+
For cases where you need loader data in the server handler itself (e.g.,
|
|
106
|
+
to set ctx variables or make routing decisions), use `ctx.use(Loader)`:
|
|
114
107
|
|
|
115
|
-
|
|
108
|
+
```typescript
|
|
116
109
|
path("/product/:slug", async (ctx) => {
|
|
117
110
|
const { product } = await ctx.use(ProductLoader);
|
|
118
|
-
|
|
119
|
-
|
|
111
|
+
ctx.set(Product, product); // make available to children
|
|
112
|
+
return <ProductPage />;
|
|
113
|
+
}, { name: "product" }, () => [
|
|
114
|
+
loader(ProductLoader), // still register for client consumption
|
|
115
|
+
])
|
|
120
116
|
```
|
|
121
117
|
|
|
122
|
-
When you
|
|
118
|
+
When you register with `loader()` in the DSL, `ctx.use()` returns the
|
|
123
119
|
same memoized result — loaders never run twice per request.
|
|
124
120
|
|
|
121
|
+
**Limitations of ctx.use(Loader):**
|
|
122
|
+
|
|
123
|
+
- The handler output depends on the loader data. If the route is inside
|
|
124
|
+
`cache()`, the handler is cached with the loader result baked in —
|
|
125
|
+
defeating the live data guarantee.
|
|
126
|
+
- Non-cacheable variable reads (`createVar({ cache: false })`) inside the
|
|
127
|
+
handler still throw, even if the data came from a loader.
|
|
128
|
+
- Prefer DSL `loader()` + client `useLoader()` for data that depends on
|
|
129
|
+
non-cacheable context variables.
|
|
130
|
+
|
|
125
131
|
**Never use `useLoader()` in server components** — it is a client-only API.
|
|
126
132
|
|
|
127
133
|
### Summary
|
|
128
134
|
|
|
129
|
-
|
|
|
130
|
-
|
|
|
131
|
-
|
|
|
132
|
-
|
|
|
135
|
+
| Pattern | API | Cache-safe | Recommended |
|
|
136
|
+
| ------------------------------ | ------------------- | ---------- | ----------- |
|
|
137
|
+
| DSL + client component | `useLoader(Loader)` | Yes | Yes |
|
|
138
|
+
| Handler escape hatch | `ctx.use(Loader)` | No | When needed |
|
|
133
139
|
|
|
134
140
|
## Loader Context
|
|
135
141
|
|
|
@@ -564,10 +570,9 @@ export const CartLoader = createLoader(async (ctx) => {
|
|
|
564
570
|
return { cart };
|
|
565
571
|
});
|
|
566
572
|
|
|
567
|
-
// urls.tsx
|
|
573
|
+
// urls.tsx — register loaders in the DSL
|
|
568
574
|
export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalidate }) => [
|
|
569
575
|
layout(<ShopLayout />, () => [
|
|
570
|
-
// Shared cart loader for all shop routes
|
|
571
576
|
loader(CartLoader, () => [
|
|
572
577
|
revalidate(({ actionId }) => actionId?.includes("Cart") ?? false),
|
|
573
578
|
]),
|
|
@@ -579,17 +584,22 @@ export const urlpatterns = urls(({ path, layout, loader, loading, cache, revalid
|
|
|
579
584
|
]),
|
|
580
585
|
]);
|
|
581
586
|
|
|
582
|
-
//
|
|
587
|
+
// components/ProductDetails.tsx — consume in client component
|
|
588
|
+
"use client";
|
|
589
|
+
import { useLoader } from "@rangojs/router/client";
|
|
583
590
|
import { ProductLoader, CartLoader } from "./loaders/shop";
|
|
584
591
|
|
|
585
|
-
|
|
586
|
-
const { product } =
|
|
587
|
-
const { cart } =
|
|
592
|
+
function ProductDetails() {
|
|
593
|
+
const { data: { product } } = useLoader(ProductLoader);
|
|
594
|
+
const { data: { cart } } = useLoader(CartLoader);
|
|
588
595
|
|
|
589
596
|
return (
|
|
590
597
|
<div>
|
|
591
598
|
<h1>{product.name}</h1>
|
|
592
|
-
<AddToCartButton
|
|
599
|
+
<AddToCartButton
|
|
600
|
+
productId={product.id}
|
|
601
|
+
inCart={cart?.items.includes(product.id)}
|
|
602
|
+
/>
|
|
593
603
|
</div>
|
|
594
604
|
);
|
|
595
605
|
}
|
package/skills/route/SKILL.md
CHANGED
|
@@ -181,6 +181,37 @@ String keys still work (`ctx.set("key", value)` / `ctx.get("key")`), but
|
|
|
181
181
|
Only route handlers and middleware can call `ctx.set()`. Layouts, parallels,
|
|
182
182
|
and intercepts can only read via `ctx.get()`.
|
|
183
183
|
|
|
184
|
+
#### Non-cacheable context variables
|
|
185
|
+
|
|
186
|
+
Mark a var as non-cacheable when it holds inherently request-specific data
|
|
187
|
+
(sessions, auth tokens, per-request IDs). There are two ways:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
// Var-level: every value written to this var is non-cacheable
|
|
191
|
+
const Session = createVar<SessionData>({ cache: false });
|
|
192
|
+
|
|
193
|
+
// Write-level: escalate a normally-cacheable var for this specific write
|
|
194
|
+
const Theme = createVar<string>();
|
|
195
|
+
ctx.set(Theme, userTheme, { cache: false });
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
"Least cacheable wins" — if either the var definition or the write site says
|
|
199
|
+
`cache: false`, the value is non-cacheable.
|
|
200
|
+
|
|
201
|
+
Reading a non-cacheable var inside `cache()` or `"use cache"` throws at
|
|
202
|
+
runtime. This prevents request-specific data from leaking into cached output:
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
// This throws — Session is non-cacheable
|
|
206
|
+
async function CachedWidget(ctx) {
|
|
207
|
+
"use cache";
|
|
208
|
+
const session = ctx.get(Session); // Error: non-cacheable var read inside cache scope
|
|
209
|
+
return <Widget />;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Cacheable vars (the default) can be read freely inside cache scopes.
|
|
214
|
+
|
|
184
215
|
### Revalidation Contracts for Handler Data
|
|
185
216
|
|
|
186
217
|
Handler-first guarantees apply within a single full render pass. For partial
|
|
@@ -369,8 +369,18 @@ interface PaginationData {
|
|
|
369
369
|
perPage: number;
|
|
370
370
|
}
|
|
371
371
|
export const Pagination = createVar<PaginationData>();
|
|
372
|
+
|
|
373
|
+
// Non-cacheable var — reading inside cache() or "use cache" throws at runtime
|
|
374
|
+
const Session = createVar<SessionData>({ cache: false });
|
|
372
375
|
```
|
|
373
376
|
|
|
377
|
+
`createVar` accepts an optional options object. The `cache` option (default
|
|
378
|
+
`true`) controls whether the var's values can be read inside cache scopes.
|
|
379
|
+
Write-level escalation is also supported: `ctx.set(Var, value, { cache: false })`
|
|
380
|
+
marks a specific write as non-cacheable even if the var itself is cacheable.
|
|
381
|
+
"Least cacheable wins" — if either says `cache: false`, the value throws on
|
|
382
|
+
read inside `cache()` or `"use cache"`.
|
|
383
|
+
|
|
374
384
|
### Producer (handler or middleware)
|
|
375
385
|
|
|
376
386
|
```typescript
|
|
@@ -5,21 +5,19 @@
|
|
|
5
5
|
* Hover prefetches bypass this queue — they fire directly for immediate response
|
|
6
6
|
* to user intent.
|
|
7
7
|
*
|
|
8
|
-
* Draining
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Draining waits for an idle main-thread moment and for viewport images to
|
|
9
|
+
* finish loading, so prefetch fetch() calls never compete with critical
|
|
10
|
+
* resources for the browser's connection pool.
|
|
11
11
|
*
|
|
12
12
|
* When a navigation starts, queued prefetches are cancelled but executing ones
|
|
13
13
|
* are left running. Navigation can reuse their in-flight responses via the
|
|
14
14
|
* prefetch cache's inflight promise map, avoiding duplicate requests.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
import { wait, waitForIdle, waitForViewportImages } from "./resource-ready.js";
|
|
18
18
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
? requestAnimationFrame
|
|
22
|
-
: (fn) => setTimeout(fn, 0);
|
|
19
|
+
const MAX_CONCURRENT = 2;
|
|
20
|
+
const IMAGE_WAIT_TIMEOUT = 2000;
|
|
23
21
|
|
|
24
22
|
let active = 0;
|
|
25
23
|
const queue: Array<{
|
|
@@ -30,6 +28,7 @@ const queued = new Set<string>();
|
|
|
30
28
|
const executing = new Set<string>();
|
|
31
29
|
let abortController: AbortController | null = null;
|
|
32
30
|
let drainScheduled = false;
|
|
31
|
+
let drainGeneration = 0;
|
|
33
32
|
|
|
34
33
|
function startExecution(
|
|
35
34
|
key: string,
|
|
@@ -50,18 +49,32 @@ function startExecution(
|
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
/**
|
|
53
|
-
* Schedule a drain
|
|
54
|
-
* Coalesces multiple drain requests into a single
|
|
55
|
-
* batch completion doesn't schedule redundant
|
|
52
|
+
* Schedule a drain after the browser is idle and viewport images are loaded.
|
|
53
|
+
* Coalesces multiple drain requests into a single deferred callback so
|
|
54
|
+
* batch completion doesn't schedule redundant waits.
|
|
55
|
+
*
|
|
56
|
+
* The two-step wait ensures prefetch fetch() calls don't compete with
|
|
57
|
+
* images for the browser's connection pool:
|
|
58
|
+
* 1. waitForIdle — yield until the main thread has a quiet moment
|
|
59
|
+
* 2. waitForViewportImages OR 2s timeout — yield until visible images
|
|
60
|
+
* finish loading, but don't let slow/broken images block indefinitely
|
|
56
61
|
*/
|
|
57
62
|
function scheduleDrain(): void {
|
|
58
63
|
if (drainScheduled) return;
|
|
59
64
|
if (active >= MAX_CONCURRENT || queue.length === 0) return;
|
|
60
65
|
drainScheduled = true;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
const gen = drainGeneration;
|
|
67
|
+
waitForIdle()
|
|
68
|
+
.then(() =>
|
|
69
|
+
Promise.race([waitForViewportImages(), wait(IMAGE_WAIT_TIMEOUT)]),
|
|
70
|
+
)
|
|
71
|
+
.then(() => {
|
|
72
|
+
drainScheduled = false;
|
|
73
|
+
// Stale drain: a cancel/abort happened while we were waiting.
|
|
74
|
+
// A fresh scheduleDrain will be called by whatever enqueues next.
|
|
75
|
+
if (gen !== drainGeneration) return;
|
|
76
|
+
if (queue.length > 0) drain();
|
|
77
|
+
});
|
|
65
78
|
}
|
|
66
79
|
|
|
67
80
|
function drain(): void {
|
|
@@ -74,9 +87,10 @@ function drain(): void {
|
|
|
74
87
|
|
|
75
88
|
/**
|
|
76
89
|
* Enqueue a prefetch for concurrency-limited execution.
|
|
77
|
-
* Execution is
|
|
78
|
-
*
|
|
79
|
-
* Deduplicates by key — items already queued or executing
|
|
90
|
+
* Execution is deferred until the browser is idle and viewport images
|
|
91
|
+
* have finished loading, so prefetches never compete with critical
|
|
92
|
+
* resources. Deduplicates by key — items already queued or executing
|
|
93
|
+
* are skipped.
|
|
80
94
|
*
|
|
81
95
|
* The executor receives an AbortSignal that is aborted when
|
|
82
96
|
* cancelAllPrefetches() is called (e.g. on navigation start).
|
|
@@ -106,6 +120,7 @@ export function cancelAllPrefetches(): void {
|
|
|
106
120
|
queue.length = 0;
|
|
107
121
|
queued.clear();
|
|
108
122
|
drainScheduled = false;
|
|
123
|
+
drainGeneration++;
|
|
109
124
|
}
|
|
110
125
|
|
|
111
126
|
/**
|
|
@@ -125,4 +140,5 @@ export function abortAllPrefetches(): void {
|
|
|
125
140
|
executing.clear();
|
|
126
141
|
active = 0;
|
|
127
142
|
drainScheduled = false;
|
|
143
|
+
drainGeneration++;
|
|
128
144
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource Readiness
|
|
3
|
+
*
|
|
4
|
+
* Utilities to defer speculative prefetches until critical resources
|
|
5
|
+
* (viewport images) have finished loading. Prevents prefetch fetch()
|
|
6
|
+
* calls from competing with images for the browser's connection pool.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolve when all in-viewport images have finished loading.
|
|
11
|
+
* Returns immediately if no images are pending.
|
|
12
|
+
*
|
|
13
|
+
* Only checks images that exist at call time — does not observe
|
|
14
|
+
* dynamically added images. For SPA navigations where new images
|
|
15
|
+
* appear after render, call this after the navigation settles.
|
|
16
|
+
*/
|
|
17
|
+
export function waitForViewportImages(): Promise<void> {
|
|
18
|
+
if (typeof document === "undefined") return Promise.resolve();
|
|
19
|
+
|
|
20
|
+
const pending = Array.from(document.querySelectorAll("img")).filter((img) => {
|
|
21
|
+
if (img.complete) return false;
|
|
22
|
+
const rect = img.getBoundingClientRect();
|
|
23
|
+
return (
|
|
24
|
+
rect.bottom > 0 &&
|
|
25
|
+
rect.right > 0 &&
|
|
26
|
+
rect.top < window.innerHeight &&
|
|
27
|
+
rect.left < window.innerWidth
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (pending.length === 0) return Promise.resolve();
|
|
32
|
+
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
const settled = new Set<HTMLImageElement>();
|
|
35
|
+
|
|
36
|
+
const settle = (img: HTMLImageElement) => {
|
|
37
|
+
if (settled.has(img)) return;
|
|
38
|
+
settled.add(img);
|
|
39
|
+
if (settled.size >= pending.length) resolve();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
for (const img of pending) {
|
|
43
|
+
img.addEventListener("load", () => settle(img), { once: true });
|
|
44
|
+
img.addEventListener("error", () => settle(img), { once: true });
|
|
45
|
+
// Re-check: image may have completed between the initial filter
|
|
46
|
+
// and listener attachment. settle() is idempotent per image, so
|
|
47
|
+
// a queued load event firing afterward is harmless.
|
|
48
|
+
if (img.complete) settle(img);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Resolve after the given number of milliseconds.
|
|
55
|
+
*/
|
|
56
|
+
export function wait(ms: number): Promise<void> {
|
|
57
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolve when the browser has an idle main-thread moment.
|
|
62
|
+
* Uses requestIdleCallback where available, falls back to setTimeout.
|
|
63
|
+
*
|
|
64
|
+
* This is a scheduling hint, not an asset-loaded detector — combine
|
|
65
|
+
* with waitForViewportImages() for full resource readiness.
|
|
66
|
+
*/
|
|
67
|
+
export function waitForIdle(timeout = 200): Promise<void> {
|
|
68
|
+
if (typeof window !== "undefined" && "requestIdleCallback" in window) {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
window.requestIdleCallback(() => resolve(), { timeout });
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
setTimeout(resolve, 0);
|
|
76
|
+
});
|
|
77
|
+
}
|
package/src/cache/taint.ts
CHANGED
|
@@ -81,6 +81,61 @@ export function assertNotInsideCacheExec(
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Symbol stamped on ctx when resolving handlers inside a cache() DSL boundary.
|
|
86
|
+
* Separate from INSIDE_CACHE_EXEC ("use cache") because cache() allows
|
|
87
|
+
* ctx.set() (children are also cached) but blocks response-level side effects
|
|
88
|
+
* (headers, cookies, status) which are lost on cache hit.
|
|
89
|
+
*/
|
|
90
|
+
export const INSIDE_CACHE_SCOPE: unique symbol = Symbol.for(
|
|
91
|
+
"rango:inside-cache-scope",
|
|
92
|
+
) as any;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Mark ctx as inside a cache() scope. Must be paired with unstampCacheScope.
|
|
96
|
+
*/
|
|
97
|
+
export function stampCacheScope(obj: object): void {
|
|
98
|
+
const current = (obj as any)[INSIDE_CACHE_SCOPE] ?? 0;
|
|
99
|
+
(obj as any)[INSIDE_CACHE_SCOPE] = current + 1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Remove cache() scope mark.
|
|
104
|
+
*/
|
|
105
|
+
export function unstampCacheScope(obj: object): void {
|
|
106
|
+
const current = (obj as any)[INSIDE_CACHE_SCOPE] ?? 0;
|
|
107
|
+
if (current <= 1) {
|
|
108
|
+
delete (obj as any)[INSIDE_CACHE_SCOPE];
|
|
109
|
+
} else {
|
|
110
|
+
(obj as any)[INSIDE_CACHE_SCOPE] = current - 1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Throw if ctx is inside a cache() DSL boundary.
|
|
116
|
+
* Call from response-level side effects (header, setCookie, setStatus, etc.)
|
|
117
|
+
* which are lost on cache hit because the handler body is skipped.
|
|
118
|
+
* ctx.set() is allowed inside cache() — children are also cached and can
|
|
119
|
+
* read the value.
|
|
120
|
+
*/
|
|
121
|
+
export function assertNotInsideCacheScope(
|
|
122
|
+
ctx: unknown,
|
|
123
|
+
methodName: string,
|
|
124
|
+
): void {
|
|
125
|
+
if (
|
|
126
|
+
ctx !== null &&
|
|
127
|
+
ctx !== undefined &&
|
|
128
|
+
typeof ctx === "object" &&
|
|
129
|
+
(INSIDE_CACHE_SCOPE as symbol) in (ctx as Record<symbol, unknown>)
|
|
130
|
+
) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`ctx.${methodName}() cannot be called inside a cache() boundary. ` +
|
|
133
|
+
`On cache hit the handler is skipped, so this side effect would be lost. ` +
|
|
134
|
+
`Move ctx.${methodName}() to a middleware or layout outside the cache() scope.`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
84
139
|
/**
|
|
85
140
|
* Brand symbol for functions wrapped by registerCachedFunction().
|
|
86
141
|
* Used at runtime to detect when a "use cache" function is misused
|
package/src/context-var.ts
CHANGED
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
* interface PaginationData { current: number; total: number }
|
|
13
13
|
* export const Pagination = createVar<PaginationData>();
|
|
14
14
|
*
|
|
15
|
+
* // Non-cacheable var — throws if set/get inside cache() or "use cache"
|
|
16
|
+
* export const User = createVar<UserData>({ cache: false });
|
|
17
|
+
*
|
|
15
18
|
* // handler
|
|
16
19
|
* ctx.set(Pagination, { current: 1, total: 4 });
|
|
17
20
|
*
|
|
@@ -23,18 +26,36 @@
|
|
|
23
26
|
export interface ContextVar<T> {
|
|
24
27
|
readonly __brand: "context-var";
|
|
25
28
|
readonly key: symbol;
|
|
29
|
+
/** When false, the var is non-cacheable — throws inside cache() / "use cache" */
|
|
30
|
+
readonly cache: boolean;
|
|
26
31
|
/** Phantom field to carry the type parameter. Never set at runtime. */
|
|
27
32
|
readonly __type?: T;
|
|
28
33
|
}
|
|
29
34
|
|
|
35
|
+
export interface ContextVarOptions {
|
|
36
|
+
/**
|
|
37
|
+
* When false, marks this variable as non-cacheable.
|
|
38
|
+
* Setting or getting this var inside a cache() boundary or "use cache"
|
|
39
|
+
* function will throw. Use for inherently request-specific data (user
|
|
40
|
+
* sessions, auth tokens, etc.) that must never be baked into cached segments.
|
|
41
|
+
*
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
cache?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
30
47
|
/**
|
|
31
48
|
* Create a typed context variable token.
|
|
32
49
|
*
|
|
33
50
|
* The returned object is used with ctx.set(token, value) and ctx.get(token)
|
|
34
51
|
* for compile-time-checked data flow between handlers, layouts, and middleware.
|
|
35
52
|
*/
|
|
36
|
-
export function createVar<T>(): ContextVar<T> {
|
|
37
|
-
return {
|
|
53
|
+
export function createVar<T>(options?: ContextVarOptions): ContextVar<T> {
|
|
54
|
+
return {
|
|
55
|
+
__brand: "context-var" as const,
|
|
56
|
+
key: Symbol(),
|
|
57
|
+
cache: options?.cache !== false,
|
|
58
|
+
};
|
|
38
59
|
}
|
|
39
60
|
|
|
40
61
|
/**
|
|
@@ -49,6 +70,36 @@ export function isContextVar(value: unknown): value is ContextVar<unknown> {
|
|
|
49
70
|
);
|
|
50
71
|
}
|
|
51
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Symbol used as a Set stored on the variables object to track
|
|
75
|
+
* which keys hold non-cacheable values (from write-level { cache: false }).
|
|
76
|
+
*/
|
|
77
|
+
const NON_CACHEABLE_KEYS: unique symbol = Symbol.for(
|
|
78
|
+
"rango:non-cacheable-keys",
|
|
79
|
+
) as any;
|
|
80
|
+
|
|
81
|
+
function getNonCacheableKeys(variables: any): Set<string | symbol> {
|
|
82
|
+
if (!variables[NON_CACHEABLE_KEYS]) {
|
|
83
|
+
variables[NON_CACHEABLE_KEYS] = new Set();
|
|
84
|
+
}
|
|
85
|
+
return variables[NON_CACHEABLE_KEYS];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check if a variable value is non-cacheable (either var-level or write-level).
|
|
90
|
+
*/
|
|
91
|
+
export function isNonCacheable(
|
|
92
|
+
variables: any,
|
|
93
|
+
keyOrVar: string | ContextVar<any>,
|
|
94
|
+
): boolean {
|
|
95
|
+
if (typeof keyOrVar !== "string" && !keyOrVar.cache) {
|
|
96
|
+
return true; // var-level policy
|
|
97
|
+
}
|
|
98
|
+
const key = typeof keyOrVar === "string" ? keyOrVar : keyOrVar.key;
|
|
99
|
+
const set = variables[NON_CACHEABLE_KEYS] as Set<string | symbol> | undefined;
|
|
100
|
+
return set?.has(key) ?? false; // write-level policy
|
|
101
|
+
}
|
|
102
|
+
|
|
52
103
|
/**
|
|
53
104
|
* Read a variable from the variables store.
|
|
54
105
|
* Accepts either a string key (legacy) or a ContextVar token (typed).
|
|
@@ -64,6 +115,17 @@ export function contextGet(
|
|
|
64
115
|
/** Keys that must never be used as string variable names */
|
|
65
116
|
const FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
66
117
|
|
|
118
|
+
export interface ContextSetOptions {
|
|
119
|
+
/**
|
|
120
|
+
* When false, marks this specific write as non-cacheable.
|
|
121
|
+
* "Least cacheable wins" — if either the var definition or this option
|
|
122
|
+
* says cache: false, the value is non-cacheable.
|
|
123
|
+
*
|
|
124
|
+
* @default true (inherits from createVar)
|
|
125
|
+
*/
|
|
126
|
+
cache?: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
67
129
|
/**
|
|
68
130
|
* Write a variable to the variables store.
|
|
69
131
|
* Accepts either a string key (legacy) or a ContextVar token (typed).
|
|
@@ -72,6 +134,7 @@ export function contextSet(
|
|
|
72
134
|
variables: any,
|
|
73
135
|
keyOrVar: string | ContextVar<any>,
|
|
74
136
|
value: any,
|
|
137
|
+
options?: ContextSetOptions,
|
|
75
138
|
): void {
|
|
76
139
|
if (typeof keyOrVar === "string") {
|
|
77
140
|
if (FORBIDDEN_KEYS.has(keyOrVar)) {
|
|
@@ -80,7 +143,14 @@ export function contextSet(
|
|
|
80
143
|
);
|
|
81
144
|
}
|
|
82
145
|
variables[keyOrVar] = value;
|
|
146
|
+
if (options?.cache === false) {
|
|
147
|
+
getNonCacheableKeys(variables).add(keyOrVar);
|
|
148
|
+
}
|
|
83
149
|
} else {
|
|
84
150
|
variables[keyOrVar.key] = value;
|
|
151
|
+
// Track write-level non-cacheable (var-level is checked via keyOrVar.cache)
|
|
152
|
+
if (options?.cache === false) {
|
|
153
|
+
getNonCacheableKeys(variables).add(keyOrVar.key);
|
|
154
|
+
}
|
|
85
155
|
}
|
|
86
156
|
}
|
|
@@ -228,11 +228,12 @@ export type RouteHelpers<T extends RouteDefinition, TEnv> = {
|
|
|
228
228
|
* revalidate(({ actionId }) => actionId?.includes("Cart") ?? false),
|
|
229
229
|
* ])
|
|
230
230
|
*
|
|
231
|
-
* //
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* }
|
|
231
|
+
* // Consume in client components with useLoader()
|
|
232
|
+
* // (preferred — cache-safe, always fresh)
|
|
233
|
+
* function ProductDetails() {
|
|
234
|
+
* const { data } = useLoader(ProductLoader);
|
|
235
|
+
* return <div>{data.name}</div>;
|
|
236
|
+
* }
|
|
236
237
|
* ```
|
|
237
238
|
* @param loaderDef - Loader created with createLoader()
|
|
238
239
|
* @param use - Optional callback for loader-specific revalidation rules
|
|
@@ -8,7 +8,13 @@ import type { HandlerContext, InternalHandlerContext } from "../types";
|
|
|
8
8
|
import { _getRequestContext } from "../server/request-context.js";
|
|
9
9
|
import { getSearchSchema, isRouteRootScoped } from "../route-map-builder.js";
|
|
10
10
|
import { parseSearchParams, serializeSearchParams } from "../search-params.js";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
contextGet,
|
|
13
|
+
contextSet,
|
|
14
|
+
isNonCacheable,
|
|
15
|
+
type ContextSetOptions,
|
|
16
|
+
} from "../context-var.js";
|
|
17
|
+
import { isInsideCacheScope } from "../server/context.js";
|
|
12
18
|
import { NOCACHE_SYMBOL, assertNotInsideCacheExec } from "../cache/taint.js";
|
|
13
19
|
import { isAutoGeneratedRouteName } from "../route-name.js";
|
|
14
20
|
import { PRERENDER_PASSTHROUGH } from "../prerender.js";
|
|
@@ -213,7 +219,7 @@ export function createHandlerContext<TEnv>(
|
|
|
213
219
|
const stubResponse =
|
|
214
220
|
requestContext?.res ?? new Response(null, { status: 200 });
|
|
215
221
|
|
|
216
|
-
// Guard mutating Headers methods so they throw inside "use cache"
|
|
222
|
+
// Guard mutating Headers methods so they throw inside "use cache" or cache() scope.
|
|
217
223
|
// Uses lazy `ctx` reference (assigned below) — only the specific handler ctx
|
|
218
224
|
// is stamped by cache-runtime, not the shared request context.
|
|
219
225
|
const MUTATING_HEADERS_METHODS = new Set(["set", "append", "delete"]);
|
|
@@ -225,6 +231,13 @@ export function createHandlerContext<TEnv>(
|
|
|
225
231
|
if (MUTATING_HEADERS_METHODS.has(prop as string)) {
|
|
226
232
|
return (...args: any[]) => {
|
|
227
233
|
assertNotInsideCacheExec(ctx, "headers");
|
|
234
|
+
if (isInsideCacheScope()) {
|
|
235
|
+
throw new Error(
|
|
236
|
+
`ctx.headers.${String(prop)}() cannot be called inside a cache() boundary. ` +
|
|
237
|
+
`On cache hit the handler is skipped, so this side effect would be lost. ` +
|
|
238
|
+
`Move header mutations to a middleware or layout outside the cache() scope.`,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
228
241
|
return value.apply(target, args);
|
|
229
242
|
};
|
|
230
243
|
}
|
|
@@ -245,13 +258,23 @@ export function createHandlerContext<TEnv>(
|
|
|
245
258
|
originalUrl: new URL(request.url),
|
|
246
259
|
env: bindings,
|
|
247
260
|
var: variables,
|
|
248
|
-
get: ((keyOrVar: any) =>
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
261
|
+
get: ((keyOrVar: any) => {
|
|
262
|
+
// Read-time guard: non-cacheable var inside cache() → throw.
|
|
263
|
+
// Works for both ContextVar tokens and string keys.
|
|
264
|
+
if (isNonCacheable(variables, keyOrVar) && isInsideCacheScope()) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
`ctx.get() for a non-cacheable variable cannot be called inside a cache() boundary. ` +
|
|
267
|
+
`The variable was created with { cache: false } or set with { cache: false }, ` +
|
|
268
|
+
`and its value would be stale on cache hit. Move the read outside the cached scope.`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
return contextGet(variables, keyOrVar);
|
|
272
|
+
}) as HandlerContext<any, TEnv>["get"],
|
|
273
|
+
set: ((keyOrVar: any, value: any, options?: ContextSetOptions) => {
|
|
253
274
|
assertNotInsideCacheExec(ctx, "set");
|
|
254
|
-
|
|
275
|
+
// Write is dumb: store value + non-cacheable metadata.
|
|
276
|
+
// Enforcement happens at read time via ctx.get().
|
|
277
|
+
contextSet(variables, keyOrVar, value, options);
|
|
255
278
|
}) as HandlerContext<any, TEnv>["set"],
|
|
256
279
|
res: stubResponse, // Stub response for setting headers
|
|
257
280
|
headers: guardedHeaders, // Guarded shorthand for res.headers
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import type { ReactNode } from "react";
|
|
8
8
|
import { track } from "../server/context";
|
|
9
9
|
import type { EntryData } from "../server/context";
|
|
10
|
+
import { contextGet } from "../context-var.js";
|
|
10
11
|
import type {
|
|
11
12
|
ResolvedSegment,
|
|
12
13
|
HandlerContext,
|
|
@@ -241,6 +242,11 @@ function createLoaderExecutor<TEnv>(
|
|
|
241
242
|
pendingLoaders.add(loader.$$id);
|
|
242
243
|
|
|
243
244
|
const currentLoaderId = loader.$$id;
|
|
245
|
+
// Loader functions are always fresh (never cached), so they get an
|
|
246
|
+
// unguarded get that bypasses non-cacheable read guards. This applies
|
|
247
|
+
// to ALL loaders — DSL and handler-called — because the loader
|
|
248
|
+
// function itself always re-executes. Also handles nested deps
|
|
249
|
+
// (loaderA → use(loaderB)) since all share this unguarded get.
|
|
244
250
|
const loaderCtx: LoaderContext<Record<string, string | undefined>, TEnv> = {
|
|
245
251
|
params: ctx.params,
|
|
246
252
|
routeParams: (ctx.params ?? {}) as Record<string, string>,
|
|
@@ -251,7 +257,7 @@ function createLoaderExecutor<TEnv>(
|
|
|
251
257
|
url: ctx.url,
|
|
252
258
|
env: ctx.env,
|
|
253
259
|
var: ctx.var,
|
|
254
|
-
get: ctx.get,
|
|
260
|
+
get: ((keyOrVar: any) => contextGet(ctx.var, keyOrVar)) as typeof ctx.get,
|
|
255
261
|
use: <TDep, TDepParams = any>(
|
|
256
262
|
dep: LoaderDefinition<TDep, TDepParams>,
|
|
257
263
|
): Promise<TDep> => {
|
|
@@ -27,8 +27,12 @@ type GetVariableFn = {
|
|
|
27
27
|
* Set variable function type
|
|
28
28
|
*/
|
|
29
29
|
type SetVariableFn = {
|
|
30
|
-
<T>(contextVar: ContextVar<T>, value: T): void;
|
|
31
|
-
<K extends keyof DefaultVars>(
|
|
30
|
+
<T>(contextVar: ContextVar<T>, value: T, options?: { cache?: boolean }): void;
|
|
31
|
+
<K extends keyof DefaultVars>(
|
|
32
|
+
key: K,
|
|
33
|
+
value: DefaultVars[K],
|
|
34
|
+
options?: { cache?: boolean },
|
|
35
|
+
): void;
|
|
32
36
|
};
|
|
33
37
|
|
|
34
38
|
/**
|
package/src/router/middleware.ts
CHANGED
|
@@ -204,8 +204,8 @@ export function createMiddlewareContext<TEnv>(
|
|
|
204
204
|
get: ((keyOrVar: any) =>
|
|
205
205
|
contextGet(variables, keyOrVar)) as MiddlewareContext<TEnv>["get"],
|
|
206
206
|
|
|
207
|
-
set: ((keyOrVar: any, value: unknown) => {
|
|
208
|
-
contextSet(variables, keyOrVar, value);
|
|
207
|
+
set: ((keyOrVar: any, value: unknown, options?: any) => {
|
|
208
|
+
contextSet(variables, keyOrVar, value, options);
|
|
209
209
|
}) as MiddlewareContext<TEnv>["set"],
|
|
210
210
|
|
|
211
211
|
var: variables as MiddlewareContext<TEnv>["var"],
|
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
} from "./helpers.js";
|
|
31
31
|
import { getRouterContext } from "../router-context.js";
|
|
32
32
|
import { resolveSink, safeEmit } from "../telemetry.js";
|
|
33
|
-
import { track } from "../../server/context.js";
|
|
33
|
+
import { track, RSCRouterContext } from "../../server/context.js";
|
|
34
34
|
|
|
35
35
|
// ---------------------------------------------------------------------------
|
|
36
36
|
// Streamed handler telemetry
|
|
@@ -100,9 +100,7 @@ export async function resolveLoaders<TEnv>(
|
|
|
100
100
|
|
|
101
101
|
if (!loadingDisabled) {
|
|
102
102
|
// Streaming loaders: promises kick off now, settle during RSC serialization.
|
|
103
|
-
|
|
104
|
-
// RSC/SSR stream consumption, after the perf timeline is logged.
|
|
105
|
-
return loaderEntries.map((loaderEntry, i) => {
|
|
103
|
+
const segments = loaderEntries.map((loaderEntry, i) => {
|
|
106
104
|
const { loader } = loaderEntry;
|
|
107
105
|
const segmentId = `${shortCode}D${i}.${loader.$$id}`;
|
|
108
106
|
return {
|
|
@@ -122,11 +120,12 @@ export async function resolveLoaders<TEnv>(
|
|
|
122
120
|
belongsToRoute,
|
|
123
121
|
};
|
|
124
122
|
});
|
|
123
|
+
|
|
124
|
+
return segments;
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
// Loading disabled: still start all loaders in parallel, but only emit
|
|
128
128
|
// settled promises so handlers don't stream loading placeholders.
|
|
129
|
-
// We can measure actual execution time here since we await all loaders.
|
|
130
129
|
const pendingLoaderData = loaderEntries.map((loaderEntry) => {
|
|
131
130
|
const start = performance.now();
|
|
132
131
|
const promise = resolveLoaderData(loaderEntry, ctx, ctx.pathname);
|
|
@@ -580,6 +579,13 @@ export async function resolveAllSegments<TEnv>(
|
|
|
580
579
|
} catch {}
|
|
581
580
|
|
|
582
581
|
for (const entry of entries) {
|
|
582
|
+
// Set ALS flag when entering a cache() boundary so that ctx.get()
|
|
583
|
+
// can guard non-cacheable variable reads. Also guards response-level
|
|
584
|
+
// side effects (headers.set). Persists for all descendant entries.
|
|
585
|
+
if (entry.type === "cache") {
|
|
586
|
+
const store = RSCRouterContext.getStore();
|
|
587
|
+
if (store) store.insideCacheScope = true;
|
|
588
|
+
}
|
|
583
589
|
const doneEntry = track(`segment:${entry.id}`, 1);
|
|
584
590
|
const resolvedSegments = await resolveWithErrorBoundary(
|
|
585
591
|
entry,
|
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
import { getRouterContext } from "../router-context.js";
|
|
43
43
|
import { resolveSink, safeEmit } from "../telemetry.js";
|
|
44
44
|
import { track } from "../../server/context.js";
|
|
45
|
+
import { RSCRouterContext } from "../../server/context.js";
|
|
45
46
|
|
|
46
47
|
// ---------------------------------------------------------------------------
|
|
47
48
|
// Telemetry helpers
|
|
@@ -1248,6 +1249,10 @@ export async function resolveAllSegmentsWithRevalidation<TEnv>(
|
|
|
1248
1249
|
}
|
|
1249
1250
|
|
|
1250
1251
|
const nonParallelEntry = entry as Exclude<EntryData, { type: "parallel" }>;
|
|
1252
|
+
if (entry.type === "cache") {
|
|
1253
|
+
const store = RSCRouterContext.getStore();
|
|
1254
|
+
if (store) store.insideCacheScope = true;
|
|
1255
|
+
}
|
|
1251
1256
|
const doneEntry = track(`segment:${entry.id}`, 1);
|
|
1252
1257
|
const resolved = await resolveWithErrorBoundary(
|
|
1253
1258
|
nonParallelEntry,
|
package/src/server/context.ts
CHANGED
|
@@ -273,6 +273,9 @@ interface HelperContext {
|
|
|
273
273
|
string,
|
|
274
274
|
import("../cache/profile-registry.js").CacheProfile
|
|
275
275
|
>;
|
|
276
|
+
/** True when resolving handlers inside a cache() DSL boundary.
|
|
277
|
+
* Read by ctx.get() to guard non-cacheable variable reads. */
|
|
278
|
+
insideCacheScope?: boolean;
|
|
276
279
|
}
|
|
277
280
|
// Use a global symbol key so the AsyncLocalStorage instance survives HMR
|
|
278
281
|
// module re-evaluation. Without this, Vite's RSC module runner may create
|
|
@@ -666,3 +669,12 @@ export function track(label: string, depth?: number): () => void {
|
|
|
666
669
|
});
|
|
667
670
|
};
|
|
668
671
|
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Check if the current execution is inside a cache() DSL boundary.
|
|
675
|
+
* Returns false inside loader execution — loaders are always fresh
|
|
676
|
+
* (never cached), so non-cacheable reads are safe.
|
|
677
|
+
*/
|
|
678
|
+
export function isInsideCacheScope(): boolean {
|
|
679
|
+
return RSCRouterContext.getStore()?.insideCacheScope === true;
|
|
680
|
+
}
|
|
@@ -20,7 +20,12 @@ import type {
|
|
|
20
20
|
DefaultRouteName,
|
|
21
21
|
} from "../types/global-namespace.js";
|
|
22
22
|
import type { Handle } from "../handle.js";
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
type ContextVar,
|
|
25
|
+
contextGet,
|
|
26
|
+
contextSet,
|
|
27
|
+
isNonCacheable,
|
|
28
|
+
} from "../context-var.js";
|
|
24
29
|
import { createHandleStore, type HandleStore } from "./handle-store.js";
|
|
25
30
|
import { isHandle } from "../handle.js";
|
|
26
31
|
import { track, type MetricsStore } from "./context.js";
|
|
@@ -30,6 +35,7 @@ import type { Theme, ResolvedThemeConfig } from "../theme/types.js";
|
|
|
30
35
|
import { THEME_COOKIE } from "../theme/constants.js";
|
|
31
36
|
import type { LocationStateEntry } from "../browser/react/location-state-shared.js";
|
|
32
37
|
import { NOCACHE_SYMBOL, assertNotInsideCacheExec } from "../cache/taint.js";
|
|
38
|
+
import { isInsideCacheScope } from "./context.js";
|
|
33
39
|
import {
|
|
34
40
|
createReverseFunction,
|
|
35
41
|
stripInternalParams,
|
|
@@ -72,8 +78,12 @@ export interface RequestContext<
|
|
|
72
78
|
};
|
|
73
79
|
/** Set a variable (shared with middleware and handlers) */
|
|
74
80
|
set: {
|
|
75
|
-
<T>(
|
|
76
|
-
|
|
81
|
+
<T>(
|
|
82
|
+
contextVar: ContextVar<T>,
|
|
83
|
+
value: T,
|
|
84
|
+
options?: { cache?: boolean },
|
|
85
|
+
): void;
|
|
86
|
+
<K extends string>(key: K, value: any, options?: { cache?: boolean }): void;
|
|
77
87
|
};
|
|
78
88
|
/**
|
|
79
89
|
* Route params (populated after route matching)
|
|
@@ -506,6 +516,18 @@ export function createRequestContext<TEnv>(
|
|
|
506
516
|
responseCookieCache = null;
|
|
507
517
|
};
|
|
508
518
|
|
|
519
|
+
// Guard: throw if a response-level side effect is called inside a cache() scope.
|
|
520
|
+
// Uses ALS to detect the scope (set during segment resolution).
|
|
521
|
+
function assertNotInsideCacheScopeALS(methodName: string): void {
|
|
522
|
+
if (isInsideCacheScope()) {
|
|
523
|
+
throw new Error(
|
|
524
|
+
`ctx.${methodName}() cannot be called inside a cache() boundary. ` +
|
|
525
|
+
`On cache hit the handler is skipped, so this side effect would be lost. ` +
|
|
526
|
+
`Move ctx.${methodName}() to a middleware or layout outside the cache() scope.`,
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
509
531
|
// Effective cookie read: response stub Set-Cookie wins, then original header.
|
|
510
532
|
// The stub IS the source of truth for same-request mutations.
|
|
511
533
|
const effectiveCookie = (name: string): string | undefined => {
|
|
@@ -570,11 +592,19 @@ export function createRequestContext<TEnv>(
|
|
|
570
592
|
pathname: url.pathname,
|
|
571
593
|
searchParams: cleanUrl.searchParams,
|
|
572
594
|
var: variables,
|
|
573
|
-
get: ((keyOrVar: any) =>
|
|
574
|
-
|
|
575
|
-
|
|
595
|
+
get: ((keyOrVar: any) => {
|
|
596
|
+
if (isNonCacheable(variables, keyOrVar) && isInsideCacheScope()) {
|
|
597
|
+
throw new Error(
|
|
598
|
+
`ctx.get() for a non-cacheable variable cannot be called inside a cache() boundary. ` +
|
|
599
|
+
`The variable was created with { cache: false } or set with { cache: false }, ` +
|
|
600
|
+
`and its value would be stale on cache hit. Move the read outside the cached scope.`,
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
return contextGet(variables, keyOrVar);
|
|
604
|
+
}) as RequestContext<TEnv>["get"],
|
|
605
|
+
set: ((keyOrVar: any, value: any, options?: any) => {
|
|
576
606
|
assertNotInsideCacheExec(ctx, "set");
|
|
577
|
-
contextSet(variables, keyOrVar, value);
|
|
607
|
+
contextSet(variables, keyOrVar, value, options);
|
|
578
608
|
}) as RequestContext<TEnv>["set"],
|
|
579
609
|
params: {} as Record<string, string>,
|
|
580
610
|
|
|
@@ -612,6 +642,7 @@ export function createRequestContext<TEnv>(
|
|
|
612
642
|
|
|
613
643
|
setCookie(name: string, value: string, options?: CookieOptions): void {
|
|
614
644
|
assertNotInsideCacheExec(ctx, "setCookie");
|
|
645
|
+
assertNotInsideCacheScopeALS("setCookie");
|
|
615
646
|
stubResponse.headers.append(
|
|
616
647
|
"Set-Cookie",
|
|
617
648
|
serializeCookieValue(name, value, options),
|
|
@@ -624,6 +655,7 @@ export function createRequestContext<TEnv>(
|
|
|
624
655
|
options?: Pick<CookieOptions, "domain" | "path">,
|
|
625
656
|
): void {
|
|
626
657
|
assertNotInsideCacheExec(ctx, "deleteCookie");
|
|
658
|
+
assertNotInsideCacheScopeALS("deleteCookie");
|
|
627
659
|
stubResponse.headers.append(
|
|
628
660
|
"Set-Cookie",
|
|
629
661
|
serializeCookieValue(name, "", { ...options, maxAge: 0 }),
|
|
@@ -633,11 +665,13 @@ export function createRequestContext<TEnv>(
|
|
|
633
665
|
|
|
634
666
|
header(name: string, value: string): void {
|
|
635
667
|
assertNotInsideCacheExec(ctx, "header");
|
|
668
|
+
assertNotInsideCacheScopeALS("header");
|
|
636
669
|
stubResponse.headers.set(name, value);
|
|
637
670
|
},
|
|
638
671
|
|
|
639
672
|
setStatus(status: number): void {
|
|
640
673
|
assertNotInsideCacheExec(ctx, "setStatus");
|
|
674
|
+
assertNotInsideCacheScopeALS("setStatus");
|
|
641
675
|
stubResponse = new Response(null, {
|
|
642
676
|
status,
|
|
643
677
|
headers: stubResponse.headers,
|
|
@@ -676,6 +710,7 @@ export function createRequestContext<TEnv>(
|
|
|
676
710
|
|
|
677
711
|
onResponse(callback: (response: Response) => Response): void {
|
|
678
712
|
assertNotInsideCacheExec(ctx, "onResponse");
|
|
713
|
+
assertNotInsideCacheScopeALS("onResponse");
|
|
679
714
|
this._onResponseCallbacks.push(callback);
|
|
680
715
|
},
|
|
681
716
|
|
|
@@ -906,7 +941,6 @@ export function createUseFunction<TEnv>(
|
|
|
906
941
|
),
|
|
907
942
|
};
|
|
908
943
|
|
|
909
|
-
// Start loader execution with tracking
|
|
910
944
|
const doneLoader = track(`loader:${loader.$$id}`, 2);
|
|
911
945
|
const promise = Promise.resolve(loaderFn(loaderCtx)).finally(() => {
|
|
912
946
|
doneLoader();
|
|
@@ -293,8 +293,11 @@ export type HandlerContext<
|
|
|
293
293
|
* and server components rendered within the request context.
|
|
294
294
|
*
|
|
295
295
|
* For loaders: Returns a promise that resolves to the loader data.
|
|
296
|
-
* Loaders are executed in parallel and memoized per request
|
|
297
|
-
* `
|
|
296
|
+
* Loaders are executed in parallel and memoized per request.
|
|
297
|
+
* Prefer DSL `loader()` + client `useLoader()` over `ctx.use(Loader)` —
|
|
298
|
+
* DSL loaders are always fresh and cache-safe. Use `ctx.use(Loader)` only
|
|
299
|
+
* when you need loader data in the handler itself (e.g., to set context
|
|
300
|
+
* variables or make routing decisions).
|
|
298
301
|
*
|
|
299
302
|
* For handles: Returns a push function to add data for this segment.
|
|
300
303
|
* Handle data accumulates across all matched route segments.
|
|
@@ -302,10 +305,11 @@ export type HandlerContext<
|
|
|
302
305
|
*
|
|
303
306
|
* @example
|
|
304
307
|
* ```typescript
|
|
305
|
-
* // Loader
|
|
306
|
-
* route("
|
|
307
|
-
* const
|
|
308
|
-
*
|
|
308
|
+
* // Loader escape hatch — use when handler needs the data directly
|
|
309
|
+
* route("product", async (ctx) => {
|
|
310
|
+
* const { product } = await ctx.use(ProductLoader);
|
|
311
|
+
* ctx.set(Product, product); // make available to children
|
|
312
|
+
* return <ProductPage />;
|
|
309
313
|
* });
|
|
310
314
|
*
|
|
311
315
|
* // Handle usage - direct value
|
|
@@ -166,11 +166,11 @@ export type LoadOptions =
|
|
|
166
166
|
* return await db.products.findBySlug(slug);
|
|
167
167
|
* });
|
|
168
168
|
*
|
|
169
|
-
* //
|
|
170
|
-
* const
|
|
169
|
+
* // Client usage (preferred — cache-safe, always fresh)
|
|
170
|
+
* const { data } = useLoader(CartLoader);
|
|
171
171
|
*
|
|
172
|
-
* //
|
|
173
|
-
* const cart =
|
|
172
|
+
* // Server escape hatch (handler needs data directly)
|
|
173
|
+
* const cart = await ctx.use(CartLoader);
|
|
174
174
|
* ```
|
|
175
175
|
*/
|
|
176
176
|
export type LoaderDefinition<
|