@rangojs/router 0.0.0-experimental.63 → 0.0.0-experimental.64

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.63",
1748
+ version: "0.0.0-experimental.64",
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.63",
3
+ "version": "0.0.0-experimental.64",
4
4
  "description": "Django-inspired RSC router with composable URL patterns",
5
5
  "keywords": [
6
6
  "react",
package/src/client.tsx CHANGED
@@ -13,7 +13,6 @@ import {
13
13
  type ClientErrorBoundaryFallbackProps,
14
14
  type ErrorInfo,
15
15
  type LoaderDefinition,
16
- type LoaderFn,
17
16
  type ResolvedSegment,
18
17
  } from "./types";
19
18
  import {
@@ -313,57 +312,6 @@ export {
313
312
  type UseLoaderOptions,
314
313
  } from "./use-loader.js";
315
314
 
316
- /**
317
- * Client-safe createLoader factory
318
- *
319
- * Creates a loader definition that can be used with useLoader().
320
- * This is the client-side version that only stores the $$id - the function
321
- * is ignored since loaders only execute on the server.
322
- *
323
- * The $$id is injected by the exposeLoaderId Vite plugin. In most cases,
324
- * you should import the loader directly from the server file rather than
325
- * creating a reference manually.
326
- *
327
- * @param fn - Loader function (ignored on client, kept for API compatibility)
328
- * @param _fetchable - Optional fetchable flag (ignored on client)
329
- * @param __injectedId - $$id injected by Vite plugin
330
- *
331
- * @example
332
- * ```tsx
333
- * "use client";
334
- * import { useLoader } from "rsc-router/client";
335
- * import { CartLoader } from "../loaders/cart"; // Import from server file
336
- *
337
- * export function CartIcon() {
338
- * const cart = useLoader(CartLoader);
339
- * return <span>Cart ({cart?.items.length ?? 0})</span>;
340
- * }
341
- * ```
342
- */
343
- // Overload 1: With function only (not fetchable)
344
- export function createLoader<T>(
345
- fn: LoaderFn<T, Record<string, string | undefined>, any>,
346
- ): LoaderDefinition<Awaited<T>, Record<string, string | undefined>>;
347
-
348
- // Overload 2: With function and fetchable flag
349
- export function createLoader<T>(
350
- fn: LoaderFn<T, Record<string, string | undefined>, any>,
351
- fetchable: true,
352
- ): LoaderDefinition<Awaited<T>, Record<string, string | undefined>>;
353
-
354
- // Implementation - function is ignored at runtime on client
355
- // The $$id is injected by Vite plugin as hidden third parameter
356
- export function createLoader(
357
- _fn: LoaderFn<any, Record<string, string | undefined>, any>,
358
- _fetchable?: true,
359
- __injectedId?: string,
360
- ): LoaderDefinition<any, Record<string, string | undefined>> {
361
- return {
362
- __brand: "loader",
363
- $$id: __injectedId || "",
364
- };
365
- }
366
-
367
315
  /**
368
316
  * Props for the ErrorBoundary component
369
317
  */
@@ -534,10 +482,8 @@ export {
534
482
  type ScrollRestorationProps,
535
483
  } from "./browser/react/ScrollRestoration.js";
536
484
 
537
- // Handle API - for accumulating data across route segments
538
- export { createHandle, isHandle, type Handle } from "./handle.js";
539
-
540
- // Handle data hook
485
+ // Handle data hook (client-side only createHandle/isHandle are server APIs from the root export)
486
+ export { type Handle } from "./handle.js";
541
487
  export { useHandle } from "./browser/react/use-handle.js";
542
488
 
543
489
  // Built-in handles
@@ -664,11 +664,15 @@ const loadingFn: RouteHelpers<any, any>["loading"] = (component, options) => {
664
664
  invariant(false, "No parent entry available for loading()");
665
665
  }
666
666
 
667
+ // Unwrap function form: loading(() => <Skeleton />) → loading(<Skeleton />)
668
+ const resolved =
669
+ typeof component === "function" ? (component as () => any)() : component;
670
+
667
671
  // If ssr: false and we're in SSR, set loading to false
668
672
  if (options?.ssr === false && ctx.isSSR) {
669
673
  parent.loading = false;
670
674
  } else {
671
- parent.loading = component;
675
+ parent.loading = resolved;
672
676
  }
673
677
 
674
678
  const name = `$${store.getNextIndex("loading")}`;
@@ -255,7 +255,10 @@ export type RouteHelpers<T extends RouteDefinition, TEnv> = {
255
255
  * @param options - Configuration options
256
256
  * @param options.ssr - If false, skip showing loading on document requests (SSR)
257
257
  */
258
- loading: (component: ReactNode, options?: { ssr?: boolean }) => LoadingItem;
258
+ loading: (
259
+ component: ReactNode | (() => ReactNode),
260
+ options?: { ssr?: boolean },
261
+ ) => LoadingItem;
259
262
  /**
260
263
  * Attach an error boundary to catch errors in this segment and children
261
264
  * ```typescript
@@ -2,10 +2,18 @@
2
2
  * Content Negotiation Utilities
3
3
  *
4
4
  * Pure functions for HTTP Accept header parsing and response type matching.
5
- * Used by createRouter's previewMatch for content negotiation between
5
+ * Used by previewMatch and classifyRequest for content negotiation between
6
6
  * RSC routes and response routes (JSON, text, image, stream, etc.).
7
7
  */
8
8
 
9
+ import type { EntryData } from "../server/context.js";
10
+ import type { CollectedMiddleware } from "./middleware-types.js";
11
+ import { collectRouteMiddleware } from "./middleware.js";
12
+ import { loadManifest } from "./manifest.js";
13
+ import { traverseBack } from "./pattern-matching.js";
14
+ import type { RouteMatchResult } from "./pattern-matching.js";
15
+ import type { RouteSnapshot } from "./route-snapshot.js";
16
+
9
17
  // Response type -> MIME type used for Accept header matching
10
18
  export const RESPONSE_TYPE_MIME: Record<string, string> = {
11
19
  json: "application/json",
@@ -114,3 +122,94 @@ export function pickNegotiateVariant(
114
122
  // No match -- use first candidate as default
115
123
  return candidates[0]!;
116
124
  }
125
+
126
+ /**
127
+ * Result of content negotiation for a route with negotiate variants.
128
+ */
129
+ export interface NegotiationResult {
130
+ /** The winning response type */
131
+ responseType: string;
132
+ /** Handler function for the winning variant */
133
+ handler: Function;
134
+ /** Manifest entry for the winning variant (may differ from primary) */
135
+ manifestEntry: EntryData;
136
+ /** Route middleware for the winning variant */
137
+ routeMiddleware: CollectedMiddleware[];
138
+ /** Always true — negotiation occurred */
139
+ negotiated: true;
140
+ }
141
+
142
+ /**
143
+ * Perform content negotiation for a route with negotiate variants.
144
+ *
145
+ * Returns a NegotiationResult when a response route wins negotiation.
146
+ * Returns null when RSC wins or no negotiation is needed.
147
+ *
148
+ * Shared by previewMatch and classifyRequest to avoid duplicating
149
+ * the candidate-building and variant-loading logic.
150
+ */
151
+ export async function negotiateRoute(
152
+ request: Request,
153
+ pathname: string,
154
+ snapshot: RouteSnapshot,
155
+ ): Promise<NegotiationResult | null> {
156
+ const { matched, manifestEntry, routeMiddleware, responseType } = snapshot;
157
+ if (!matched.negotiateVariants || matched.negotiateVariants.length === 0) {
158
+ return null;
159
+ }
160
+
161
+ const acceptEntries = parseAcceptTypes(request.headers.get("accept") || "");
162
+
163
+ // Build candidate list preserving definition order.
164
+ const variants = matched.negotiateVariants;
165
+ let candidates: Array<{ routeKey: string; responseType: string }>;
166
+ if (responseType) {
167
+ candidates = [...variants, { routeKey: matched.routeKey, responseType }];
168
+ } else {
169
+ const rscCandidate = {
170
+ routeKey: matched.routeKey,
171
+ responseType: RSC_RESPONSE_TYPE,
172
+ };
173
+ candidates = matched.rscFirst
174
+ ? [rscCandidate, ...variants]
175
+ : [...variants, rscCandidate];
176
+ }
177
+
178
+ const variant = pickNegotiateVariant(acceptEntries, candidates);
179
+
180
+ // RSC won negotiation
181
+ if (variant.responseType === RSC_RESPONSE_TYPE) {
182
+ return null;
183
+ }
184
+
185
+ // Primary response-type won — use existing manifest entry and middleware
186
+ if (responseType && variant.routeKey === matched.routeKey) {
187
+ return {
188
+ responseType,
189
+ handler: manifestEntry.handler as Function,
190
+ manifestEntry,
191
+ routeMiddleware,
192
+ negotiated: true,
193
+ };
194
+ }
195
+
196
+ // Different variant won — load its manifest entry
197
+ const negotiateEntry = await loadManifest(
198
+ matched.entry,
199
+ variant.routeKey,
200
+ pathname,
201
+ undefined,
202
+ false,
203
+ );
204
+ const variantMiddleware = collectRouteMiddleware(
205
+ traverseBack(negotiateEntry),
206
+ matched.params,
207
+ );
208
+ return {
209
+ responseType: variant.responseType,
210
+ handler: negotiateEntry.handler as Function,
211
+ manifestEntry: negotiateEntry,
212
+ routeMiddleware: variantMiddleware,
213
+ negotiated: true,
214
+ };
215
+ }