reducer.d.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import type { CacheNode } from '../../shared/lib/app-router-context';
  2. import type { FlightRouterState, FlightData, FlightSegmentPath } from '../../server/app-render';
  3. export declare type FocusAndScrollRef = {
  4. /**
  5. * If focus and scroll should be set in the layout-router's useEffect()
  6. */
  7. apply: boolean;
  8. };
  9. export declare const ACTION_RELOAD = "reload";
  10. export declare const ACTION_NAVIGATE = "navigate";
  11. export declare const ACTION_RESTORE = "restore";
  12. export declare const ACTION_SERVER_PATCH = "server-patch";
  13. export declare const ACTION_PREFETCH = "prefetch";
  14. /**
  15. * Reload triggers a reload of the full page data.
  16. * - fetches the Flight data and fills subTreeData at the root of the cache.
  17. * - The router state is updated at the root of the state tree.
  18. */
  19. interface ReloadAction {
  20. type: typeof ACTION_RELOAD;
  21. cache: CacheNode;
  22. mutable: {
  23. previousTree?: FlightRouterState;
  24. patchedTree?: FlightRouterState;
  25. };
  26. }
  27. /**
  28. * Navigate triggers a navigation to the provided url. It supports a combination of `cacheType` (`hard` and `soft`) and `navigateType` (`push` and `replace`).
  29. *
  30. * `navigateType`:
  31. * - `push` - pushes a new history entry in the browser history
  32. * - `replace` - replaces the current history entry in the browser history
  33. *
  34. * `cacheType`:
  35. * - `hard` - Creates a new cache in one of two ways:
  36. * - Not optimistic
  37. * - Default if there is no loading.js.
  38. * - Fetch data in the reducer and suspend there.
  39. * - Copies the previous cache nodes as far as possible and applies new subTreeData.
  40. * - Applies the new router state.
  41. * - optimistic
  42. * - Enabled when somewhere in the router state path to the page there is a loading.js.
  43. * - Similar to `soft` but kicks off the data fetch in the reducer and applies `data` in the spot that should suspend.
  44. * - This enables showing loading states while navigating.
  45. * - Will trigger fast path or server-patch case in layout-router.
  46. * - `soft`
  47. * - Reuses the existing cache.
  48. * - Creates an optimistic router state that causes the fetch to start in layout-router when there is missing data.
  49. * - If there is no missing data the existing cache data is rendered.
  50. */
  51. interface NavigateAction {
  52. type: typeof ACTION_NAVIGATE;
  53. url: URL;
  54. navigateType: 'push' | 'replace';
  55. forceOptimisticNavigation: boolean;
  56. cache: CacheNode;
  57. mutable: {
  58. previousTree?: FlightRouterState;
  59. patchedTree?: FlightRouterState;
  60. useExistingCache?: true;
  61. };
  62. }
  63. /**
  64. * Restore applies the provided router state.
  65. * - Only used for `popstate` (back/forward navigation) where a known router state has to be applied.
  66. * - Router state is applied as-is from the history state.
  67. * - If any data is missing it will be fetched in layout-router during rendering and trigger fast path or server-patch case.
  68. * - If no data is missing the existing cached data is rendered.
  69. */
  70. interface RestoreAction {
  71. type: typeof ACTION_RESTORE;
  72. url: URL;
  73. tree: FlightRouterState;
  74. }
  75. /**
  76. * Server-patch applies the provided Flight data to the cache and router tree.
  77. * - Only triggered in layout-router when the data can't be handled in the fast path.
  78. * - Main case where this is triggered is when a rewrite applies and Flight data for a different path is returned from the server.
  79. * - Creates a new cache and router state with the Flight data applied.
  80. */
  81. interface ServerPatchAction {
  82. type: typeof ACTION_SERVER_PATCH;
  83. flightData: FlightData;
  84. previousTree: FlightRouterState;
  85. cache: CacheNode;
  86. mutable: {
  87. patchedTree?: FlightRouterState;
  88. };
  89. }
  90. interface PrefetchAction {
  91. type: typeof ACTION_PREFETCH;
  92. url: URL;
  93. flightData: FlightData;
  94. }
  95. interface PushRef {
  96. /**
  97. * If the app-router should push a new history entry in app-router's useEffect()
  98. */
  99. pendingPush: boolean;
  100. /**
  101. * Multi-page navigation through location.href.
  102. */
  103. mpaNavigation: boolean;
  104. }
  105. /**
  106. * Handles keeping the state of app-router.
  107. */
  108. declare type AppRouterState = {
  109. /**
  110. * The router state, this is written into the history state in app-router using replaceState/pushState.
  111. * - Has to be serializable as it is written into the history state.
  112. * - Holds which segments are shown on the screen.
  113. * - Holds where loading states (loading.js) exists.
  114. */
  115. tree: FlightRouterState;
  116. /**
  117. * The cache holds React nodes for every segment that is shown on screen as well as previously shown segments and prefetched segments.
  118. * It also holds in-progress data requests.
  119. */
  120. cache: CacheNode;
  121. /**
  122. * Cache that holds prefetched Flight responses keyed by url
  123. */
  124. prefetchCache: Map<string, {
  125. flightSegmentPath: FlightSegmentPath;
  126. treePatch: FlightRouterState;
  127. }>;
  128. /**
  129. * Decides if the update should create a new history entry and if the navigation can't be handled by app-router.
  130. */
  131. pushRef: PushRef;
  132. /**
  133. * Decides if the update should apply scroll and focus management.
  134. */
  135. focusAndScrollRef: FocusAndScrollRef;
  136. /**
  137. * The canonical url that is pushed/replaced
  138. */
  139. canonicalUrl: string;
  140. };
  141. /**
  142. * Reducer that handles the app-router state updates.
  143. */
  144. export declare function reducer(state: Readonly<AppRouterState>, action: Readonly<ReloadAction | NavigateAction | RestoreAction | ServerPatchAction | PrefetchAction>): AppRouterState;
  145. export {};