@rangojs/router 0.35.0 → 0.36.1

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.
Files changed (34) hide show
  1. package/dist/bin/{chunk-3CQXDX3A.js → chunk-THW7E5A2.js} +11 -5
  2. package/dist/bin/{client-urls-projection-2GBDM5YK.js → client-urls-projection-DHUUOJZM.js} +1 -1
  3. package/dist/bin/rango.js +1 -1
  4. package/dist/bin/{runtime-discovery-PA3V3IFQ.js → runtime-discovery-OPRGB3DF.js} +2 -2
  5. package/dist/bin/{temp-rsc-server-WDXEHDY7.js → temp-rsc-server-FV4ERMAS.js} +2 -2
  6. package/dist/types/browser/event-controller.d.ts +10 -0
  7. package/dist/types/browser/fetched-loader-handles.d.ts +33 -0
  8. package/dist/types/browser/react/use-link-status.d.ts +17 -8
  9. package/dist/types/browser/types.d.ts +2 -0
  10. package/dist/types/handles/deferred-resolution.d.ts +7 -0
  11. package/dist/types/handles/pushed-by.d.ts +25 -0
  12. package/dist/types/testing/flight.entry.d.ts +1 -1
  13. package/dist/types/testing/render-route.d.ts +5 -2
  14. package/dist/types/testing/serve-shell-request.d.ts +31 -0
  15. package/dist/vite/index.js +12 -6
  16. package/package.json +1 -1
  17. package/skills/hooks/navigation.md +10 -3
  18. package/skills/loader/SKILL.md +20 -0
  19. package/skills/testing/client-components.md +1 -1
  20. package/skills/testing/serve-shell-request.md +33 -14
  21. package/src/browser/event-controller.ts +49 -1
  22. package/src/browser/fetched-loader-handles.ts +96 -0
  23. package/src/browser/navigation-bridge.ts +2 -1
  24. package/src/browser/react/Link.tsx +13 -2
  25. package/src/browser/react/use-link-status.ts +40 -22
  26. package/src/browser/types.ts +2 -0
  27. package/src/handles/deferred-resolution.ts +29 -2
  28. package/src/handles/pushed-by.ts +75 -0
  29. package/src/rsc/loader-fetch.ts +47 -5
  30. package/src/ssr/ssr-root.tsx +1 -0
  31. package/src/testing/flight.entry.ts +1 -0
  32. package/src/testing/render-route.tsx +14 -3
  33. package/src/testing/serve-shell-request.ts +62 -1
  34. package/src/use-loader.tsx +33 -4
@@ -63,7 +63,10 @@ import {
63
63
  } from "../rsc/shell-capture-constants.js";
64
64
  import { SEGMENT_FRAGMENT_CAPABILITY_HEADER } from "../segment-fragments.js";
65
65
  import { PREFETCH_HEADER } from "../prefetch-markers.js";
66
- import { resolveDeferredHandleValues } from "../handles/deferred-resolution.js";
66
+ import {
67
+ resolveDeferredHandleValues,
68
+ resolveDeferredPushes,
69
+ } from "../handles/deferred-resolution.js";
67
70
  import { withoutPushedBy } from "../handles/pushed-by.js";
68
71
  import type { HandleData } from "../server/handle-store.js";
69
72
  import type { ResolvedSegment } from "../types.js";
@@ -209,6 +212,31 @@ export interface ShellRequestAction {
209
212
  revalidationError: Error | null;
210
213
  }
211
214
 
215
+ /**
216
+ * A fetchable loader read's answer (`?_rsc_loader=<id>`), as `load()` and a
217
+ * `refreshGroup` refresh read it (use-loader.tsx).
218
+ */
219
+ export interface ShellRequestLoader {
220
+ /** What the read resolves with; null when it rejects. */
221
+ result: unknown;
222
+ /** The message the read rejects with, or null. */
223
+ error: string | null;
224
+ /**
225
+ * The values the loader's body pushed to each handle, by handle $$id, in
226
+ * push order, a pending one settled (dropped when it rejects or resolves
227
+ * nullish, as the browser drops it). The browser applies them to the
228
+ * segment that registers the loader, in place of its earlier ones.
229
+ */
230
+ handles: Record<string, unknown[]>;
231
+ /**
232
+ * Which loader's run pushed each of `handles`, index-aligned per handle:
233
+ * another loader the body read with `ctx.use()`, or null for the body's
234
+ * own. A handle with no entry holds only the body's own. The browser leaves
235
+ * a value another loader on the page pushed to that loader's own run.
236
+ */
237
+ pushedBy: Record<string, (string | null)[]>;
238
+ }
239
+
212
240
  /** Result of {@link serveShellRequest}. */
213
241
  export interface ServeShellRequestResult {
214
242
  /** The handler's Response. Its body is already read: see `body`. */
@@ -288,6 +316,13 @@ export interface ServeShellRequestResult {
288
316
  * result.
289
317
  */
290
318
  readAction(): Promise<ShellRequestAction | undefined>;
319
+ /**
320
+ * For a fetchable loader read (`?_rsc_loader=<id>` with an `accept:
321
+ * text/x-component` header), its answer as the browser reads it: the
322
+ * loader's value or error, and the handle values its body pushed.
323
+ * Undefined when no Flight was rendered or the payload is not a loader's.
324
+ */
325
+ readLoader(): Promise<ShellRequestLoader | undefined>;
291
326
  }
292
327
 
293
328
  type ShellHandler = ReturnType<typeof createRSCHandler>;
@@ -441,6 +476,29 @@ async function decodeAction(
441
476
  };
442
477
  }
443
478
 
479
+ /** A loader read's answer, read as use-loader.tsx reads it. */
480
+ async function decodeLoader(
481
+ flight: string,
482
+ ): Promise<ShellRequestLoader | undefined> {
483
+ const payload = await decodePayload<{
484
+ loaderResult?: unknown;
485
+ loaderError?: { message: string };
486
+ handles?: Record<string, unknown[]>;
487
+ pushedBy?: Record<string, (string | null)[]>;
488
+ }>(flight);
489
+ if (!("loaderResult" in payload)) return undefined;
490
+ const { values, pushedBy } = await resolveDeferredPushes(
491
+ payload.handles ?? {},
492
+ payload.pushedBy,
493
+ );
494
+ return {
495
+ result: payload.loaderError ? null : payload.loaderResult,
496
+ error: payload.loaderError?.message ?? null,
497
+ handles: values,
498
+ pushedBy,
499
+ };
500
+ }
501
+
444
502
  /** The ids of the segments a payload marks deferred. */
445
503
  async function decodeDeferredIds(flight: string): Promise<string[]> {
446
504
  const { metadata } = await decodePayload<{
@@ -828,5 +886,8 @@ export async function serveShellRequest<TEnv = any>(
828
886
  async readAction() {
829
887
  return flight ? decodeAction(flight) : undefined;
830
888
  },
889
+ async readLoader() {
890
+ return flight ? decodeLoader(flight) : undefined;
891
+ },
831
892
  };
832
893
  }
@@ -25,6 +25,12 @@ import { decodeLoaderEntry } from "./decode-loader-results.js";
25
25
  import { warnAwaitedSsrSuspension } from "./ssr-suspension-warning.js";
26
26
  import { SETTLED_THENABLE } from "./thenable-status.js";
27
27
  import { useReadAudit } from "./suspense-audit-react.js";
28
+ import {
29
+ startFetchedLoaderRead,
30
+ type FetchedLoaderHandles,
31
+ type FetchedLoaderPage,
32
+ type FetchedLoaderPushedBy,
33
+ } from "./browser/fetched-loader-handles.js";
28
34
  import { INTERNAL_RANGO_SUSPENSE_AUDIT } from "./internal-suspense-audit.js";
29
35
  import type { LoaderDefinition, LoadOptions } from "./types.js";
30
36
 
@@ -122,6 +128,10 @@ function extractContentLoaderData(
122
128
  interface LoaderRscPayload<T = unknown> {
123
129
  loaderResult: T;
124
130
  loaderError?: { message: string; name: string };
131
+ /** What the loader's body pushed to each handle (rsc/loader-fetch.ts). */
132
+ handles?: FetchedLoaderHandles;
133
+ /** Which loader's run pushed each of `handles` (rsc/loader-fetch.ts). */
134
+ pushedBy?: FetchedLoaderPushedBy;
125
135
  }
126
136
 
127
137
  export type LoadFunction<T> = (options?: LoadOptions) => Promise<T>;
@@ -268,8 +278,18 @@ function useLoaderInternal<T>(
268
278
 
269
279
  // The router store's loader data (loader-store.ts). None on the server,
270
280
  // where the hook reads the route's loader data only.
271
- const loaders: LoaderStore | undefined = useContext(NavigationStoreContext)
272
- ?.store?.loaders;
281
+ const navigation = useContext(NavigationStoreContext);
282
+ const loaders: LoaderStore | undefined = navigation?.store?.loaders;
283
+ // Where a read's handle values go (browser/fetched-loader-handles.ts).
284
+ const navigationStore = navigation?.store;
285
+ const eventController = navigation?.eventController;
286
+ const page = useMemo<FetchedLoaderPage | undefined>(
287
+ () =>
288
+ navigationStore && eventController
289
+ ? { store: navigationStore, eventController }
290
+ : undefined,
291
+ [navigationStore, eventController],
292
+ );
273
293
 
274
294
  // Committed route data (updated by the reset effect below), read by the
275
295
  // store when a refresh begins: the snapshot it produces belongs to it.
@@ -283,6 +303,7 @@ function useLoaderInternal<T>(
283
303
  try {
284
304
  const url = new URL(window.location.href);
285
305
  url.searchParams.set("_rsc_loader", loaderId);
306
+ const applyHandles = startFetchedLoaderRead(loaderId, page);
286
307
  const response = fetch(url.toString(), {
287
308
  method: "GET",
288
309
  headers: { Accept: "text/x-component" },
@@ -293,6 +314,7 @@ function useLoaderInternal<T>(
293
314
  throw new Error(payload.loaderError.message);
294
315
  }
295
316
  loaders.finishData(bucketKey, requestId, payload.loaderResult);
317
+ applyHandles(payload.handles, payload.pushedBy);
296
318
  } catch (e) {
297
319
  const err = e instanceof Error ? e : new Error(String(e));
298
320
  loaders.finishError(bucketKey, requestId, err);
@@ -300,7 +322,7 @@ function useLoaderInternal<T>(
300
322
  } finally {
301
323
  loaders.setLoading(bucketKey, requestId, false);
302
324
  }
303
- }, [loaderId, bucketKey, loaders]);
325
+ }, [loaderId, bucketKey, loaders, page]);
304
326
 
305
327
  // Held-navigation pin: a transition commit whose stream for this loader is
306
328
  // still pending announces it (loader-store.ts announcePendingStreams, which
@@ -545,6 +567,12 @@ function useLoaderInternal<T>(
545
567
  };
546
568
  }
547
569
 
570
+ // Only a refetch of the page's own read (the page's params, no body)
571
+ // applies its handle values: a read with other params or a body is
572
+ // data the page does not show, and its pushes would retitle the page.
573
+ const applyHandles = isPlainRefetch(loadOptions)
574
+ ? startFetchedLoaderRead(id, page)
575
+ : undefined;
548
576
  const response = fetch(url.toString(), fetchOptions);
549
577
 
550
578
  const { createFromFetch } = await import("./deps/browser.js");
@@ -563,6 +591,7 @@ function useLoaderInternal<T>(
563
591
  setLocalIsLoading(false);
564
592
  });
565
593
  }
594
+ applyHandles?.(payload.handles, payload.pushedBy);
566
595
  return result;
567
596
  } catch (e) {
568
597
  const err = e instanceof Error ? e : new Error(String(e));
@@ -582,7 +611,7 @@ function useLoaderInternal<T>(
582
611
  }
583
612
  }
584
613
  },
585
- [throwOnError, loaders, routeDataOf],
614
+ [throwOnError, loaders, routeDataOf, page],
586
615
  );
587
616
 
588
617
  if (throwOnError) {