@rangojs/router 0.0.0-experimental.47 → 0.0.0-experimental.49

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.
@@ -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.47",
1748
+ version: "0.0.0-experimental.49",
1749
1749
  description: "Django-inspired RSC router with composable URL patterns",
1750
1750
  keywords: [
1751
1751
  "react",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rangojs/router",
3
- "version": "0.0.0-experimental.47",
3
+ "version": "0.0.0-experimental.49",
4
4
  "description": "Django-inspired RSC router with composable URL patterns",
5
5
  "keywords": [
6
6
  "react",
@@ -92,6 +92,73 @@ path("/dashboard/:id", (ctx) => {
92
92
  ])
93
93
  ```
94
94
 
95
+ ## Setting Handles (Meta, Breadcrumbs)
96
+
97
+ Parallel slot handlers can call `ctx.use(Meta)` or `ctx.use(Breadcrumbs)` to
98
+ push handle data. The data is associated with the **parent** layout or route
99
+ segment, not the parallel segment itself. This is because parallels execute
100
+ after their parent handler and inherit its segment scope.
101
+
102
+ This works well for document-level metadata — the handle data follows the
103
+ parent's lifecycle (appears when the parent is mounted, removed when it
104
+ unmounts).
105
+
106
+ ```typescript
107
+ parallel({
108
+ "@meta": (ctx) => {
109
+ const meta = ctx.use(Meta);
110
+ meta({ title: "Product Detail" });
111
+ meta({ name: "description", content: "..." });
112
+ return null; // UI-less slot, only sets metadata
113
+ },
114
+ "@sidebar": (ctx) => <Sidebar />,
115
+ })
116
+ ```
117
+
118
+ Multiple parallels on the same parent can each push handle data — they all
119
+ accumulate under the parent segment ID.
120
+
121
+ ### Pattern: `@meta` slot for per-route metadata overrides
122
+
123
+ A dedicated `@meta` parallel slot lets routes define metadata separately from
124
+ their handler logic. The layout sets defaults via a title template, and each
125
+ route overrides via its own `@meta` slot. Since child segments push after
126
+ parents and `collectMeta` uses last-wins deduplication, overrides work
127
+ naturally.
128
+
129
+ ```typescript
130
+ // Layout sets defaults
131
+ layout((ctx) => {
132
+ ctx.use(Meta)({ title: { template: "%s | Store", default: "Store" } });
133
+ return <StoreLayout />;
134
+ }, () => [
135
+ // Route with @meta override — decoupled from handler rendering
136
+ path("/:slug", ProductPage, { name: "product" }, () => [
137
+ parallel({
138
+ "@meta": async (ctx) => {
139
+ const product = await ctx.use(ProductLoader);
140
+ const meta = ctx.use(Meta);
141
+ meta({ title: product.name });
142
+ meta({ name: "description", content: product.description });
143
+ meta({
144
+ "script:ld+json": {
145
+ "@context": "https://schema.org",
146
+ "@type": "Product",
147
+ name: product.name,
148
+ description: product.description,
149
+ },
150
+ });
151
+ return null; // UI-less slot
152
+ },
153
+ }),
154
+ ]),
155
+ ])
156
+ ```
157
+
158
+ This keeps the route handler focused on rendering UI while metadata
159
+ (title, description, Open Graph, JSON-LD) lives in a composable slot that
160
+ can be added, removed, or swapped per route without touching the handler.
161
+
95
162
  ## Parallel Routes with Loaders
96
163
 
97
164
  Add loaders and loading states to parallel routes:
@@ -149,6 +149,13 @@ export function withBackgroundRevalidation<TEnv>(
149
149
  : undefined;
150
150
 
151
151
  requestCtx?.waitUntil(async () => {
152
+ // Prevent background metrics from polluting foreground timeline.
153
+ // The foreground uses its own metricsStore reference directly (via
154
+ // appendMetric), so nulling Store.metrics only affects track() calls
155
+ // inside this background Store.run() scope.
156
+ const savedMetrics = ctx.Store.metrics;
157
+ ctx.Store.metrics = undefined;
158
+
152
159
  const start = performance.now();
153
160
  debugLog("backgroundRevalidation", "revalidating stale route", {
154
161
  pathname: ctx.pathname,
@@ -179,7 +186,9 @@ export function withBackgroundRevalidation<TEnv>(
179
186
  setupLoaderAccess(freshHandlerContext, freshLoaderPromises);
180
187
 
181
188
  // Resolve all segments fresh (without revalidation logic)
182
- // to ensure complete components for caching
189
+ // to ensure complete components for caching.
190
+ // Skip DSL loaders — they are never cached (cacheRoute filters them)
191
+ // and are always resolved fresh on each request.
183
192
  const freshSegments = await ctx.Store.run(() =>
184
193
  resolveAllSegments(
185
194
  ctx.entries,
@@ -187,6 +196,7 @@ export function withBackgroundRevalidation<TEnv>(
187
196
  ctx.matched.params,
188
197
  freshHandlerContext,
189
198
  freshLoaderPromises,
199
+ { skipLoaders: true },
190
200
  ),
191
201
  );
192
202
 
@@ -234,6 +244,7 @@ export function withBackgroundRevalidation<TEnv>(
234
244
  });
235
245
  } finally {
236
246
  requestCtx._handleStore = originalHandleStore;
247
+ ctx.Store.metrics = savedMetrics;
237
248
  }
238
249
  });
239
250
  };
@@ -522,18 +522,23 @@ export function withCacheLookup<TEnv>(
522
522
  const entryInfo = entryRevalidateMap?.get(segment.id);
523
523
 
524
524
  // Even without explicit revalidation rules, route segments and their
525
- // children must re-render when search params change — the handler reads
526
- // ctx.searchParams so different ?page= values produce different content.
525
+ // children must re-render when params or search params change — the
526
+ // handler reads ctx.params/ctx.searchParams so different values produce
527
+ // different content. Matches evaluateRevalidation's default logic.
527
528
  const searchChanged = ctx.prevUrl.search !== ctx.url.search;
529
+ const routeParamsChanged = !paramsEqual(
530
+ ctx.matched.params,
531
+ ctx.prevParams,
532
+ );
528
533
  const shouldDefaultRevalidate =
529
- searchChanged &&
534
+ (searchChanged || routeParamsChanged) &&
530
535
  (segment.type === "route" ||
531
536
  (segment.belongsToRoute &&
532
537
  (segment.type === "layout" || segment.type === "parallel")));
533
538
 
534
539
  if (!entryInfo || entryInfo.revalidate.length === 0) {
535
540
  if (shouldDefaultRevalidate) {
536
- // Search params changed — must re-render even without custom rules
541
+ // Params or search params changed — must re-render even without custom rules
537
542
  if (isTraceActive()) {
538
543
  pushRevalidationTraceEntry({
539
544
  segmentId: segment.id,
@@ -542,7 +547,9 @@ export function withCacheLookup<TEnv>(
542
547
  source: "cache-hit",
543
548
  defaultShouldRevalidate: true,
544
549
  finalShouldRevalidate: true,
545
- reason: "cached-search-changed",
550
+ reason: routeParamsChanged
551
+ ? "cached-params-changed"
552
+ : "cached-search-changed",
546
553
  });
547
554
  }
548
555
  yield segment;
@@ -165,10 +165,14 @@ export function withCacheStore<TEnv>(
165
165
  // Combine main segments with intercept segments
166
166
  const allSegmentsToCache = [...allSegments, ...state.interceptSegments];
167
167
 
168
- // Check if any non-loader segments have null components
169
- // This happens when client already had those segments (partial navigation)
168
+ // Check if any non-loader segments have null components from revalidation
169
+ // skip (client already had them). Segments where the handler intentionally
170
+ // returned null are not revalidation skips — re-rendering them will still
171
+ // produce null, so proactive caching would be wasted work.
172
+ const clientIdSet = new Set(ctx.clientSegmentIds);
170
173
  const hasNullComponents = allSegmentsToCache.some(
171
- (s) => s.component === null && s.type !== "loader",
174
+ (s) =>
175
+ s.component === null && s.type !== "loader" && clientIdSet.has(s.id),
172
176
  );
173
177
 
174
178
  const requestCtx = getRequestContext();
@@ -195,6 +199,10 @@ export function withCacheStore<TEnv>(
195
199
  // Proactive caching: render all segments fresh in background
196
200
  // This ensures cache has complete components for future requests
197
201
  requestCtx.waitUntil(async () => {
202
+ // Prevent background metrics from polluting foreground timeline.
203
+ const savedMetrics = ctx.Store.metrics;
204
+ ctx.Store.metrics = undefined;
205
+
198
206
  const start = performance.now();
199
207
  debugLog("cacheStore", "proactive caching started", {
200
208
  pathname: ctx.pathname,
@@ -225,7 +233,9 @@ export function withCacheStore<TEnv>(
225
233
  // Use normal loader access so handle data is captured
226
234
  setupLoaderAccess(proactiveHandlerContext, proactiveLoaderPromises);
227
235
 
228
- // Re-resolve ALL segments without revalidation
236
+ // Re-resolve ALL segments without revalidation.
237
+ // Skip DSL loaders — they are never cached (cacheRoute filters them)
238
+ // and are always resolved fresh on each request.
229
239
  const Store = ctx.Store;
230
240
  const freshSegments = await Store.run(() =>
231
241
  resolveAllSegments(
@@ -234,6 +244,7 @@ export function withCacheStore<TEnv>(
234
244
  ctx.matched.params,
235
245
  proactiveHandlerContext,
236
246
  proactiveLoaderPromises,
247
+ { skipLoaders: true },
237
248
  ),
238
249
  );
239
250
 
@@ -285,6 +296,7 @@ export function withCacheStore<TEnv>(
285
296
  });
286
297
  } finally {
287
298
  requestCtx._handleStore = originalHandleStore;
299
+ ctx.Store.metrics = savedMetrics;
288
300
  }
289
301
  });
290
302
  } else {
@@ -67,10 +67,11 @@
67
67
  * Keep if:
68
68
  * - component !== null (needs rendering)
69
69
  * - type === "loader" (carries data even with null component)
70
+ * - client doesn't have the segment (structurally required parent node)
70
71
  *
71
72
  * Skip if:
72
- * - component === null AND type !== "loader"
73
- * - (Client already has this segment's UI)
73
+ * - component === null AND type !== "loader" AND client has it cached
74
+ * - (Revalidation skip — client already has this segment's UI)
74
75
  *
75
76
  *
76
77
  * INTERCEPT HANDLING
@@ -168,10 +169,15 @@ export function buildMatchResult<TEnv>(
168
169
  // Deduplicate allIds (defense-in-depth for partial match path)
169
170
  allIds = [...new Set(allIds)];
170
171
 
171
- // Filter out segments with null components (client already has them)
172
- // BUT always include loader segments - they carry data even with null component
172
+ // Filter out null-component segments only when the client already has
173
+ // them cached (revalidation skip). If the client doesn't have the segment,
174
+ // it must be included even with null component — it's structurally required
175
+ // as a parent node for child layouts/parallels to reconcile against.
176
+ // Loader segments are always included as they carry data.
177
+ const clientIdSet = new Set(ctx.clientSegmentIds);
173
178
  segmentsToRender = allSegments.filter(
174
- (s) => s.component !== null || s.type === "loader",
179
+ (s) =>
180
+ s.component !== null || s.type === "loader" || !clientIdSet.has(s.id),
175
181
  );
176
182
  }
177
183
 
@@ -210,6 +210,7 @@ export interface RouterContext<TEnv = any> {
210
210
  params: Record<string, string>,
211
211
  handlerContext: HandlerContext<any, TEnv>,
212
212
  loaderPromises: Map<string, Promise<any>>,
213
+ options?: { skipLoaders?: boolean },
213
214
  ) => Promise<ResolvedSegment[]>;
214
215
 
215
216
  // Generator-based simple resolution
@@ -344,7 +344,7 @@ export async function resolveSegment<TEnv>(
344
344
  namespace: entry.id,
345
345
  type: "route",
346
346
  index: 0,
347
- component,
347
+ component: component ?? null,
348
348
  loading: entry.loading === false ? null : entry.loading,
349
349
  transition: entry.transition,
350
350
  params,
@@ -722,10 +722,12 @@ export async function resolveEntryHandlerWithRevalidation<TEnv>(
722
722
  () => null,
723
723
  );
724
724
 
725
+ // Normalize void handlers (undefined) to null so the reconciler's
726
+ // component === null checks work consistently for both void and explicit null.
725
727
  const resolvedComponent =
726
728
  component && typeof component === "object" && "content" in component
727
- ? (component as { content: ReactNode }).content
728
- : component;
729
+ ? ((component as { content: ReactNode }).content ?? null)
730
+ : (component ?? null);
729
731
 
730
732
  const segment: ResolvedSegment = {
731
733
  id: entry.shortCode,