{"version":3,"sources":["../../../../../../src/client/components/router-reducer/reducers/restore-reducer.ts"],"sourcesContent":["import type {\n  ReadonlyReducerState,\n  ReducerState,\n  RestoreAction,\n} from '../router-reducer-types'\nimport { extractPathFromFlightRouterState } from '../compute-changed-path'\nimport {\n  FreshnessPolicy,\n  spawnDynamicRequests,\n  startPPRNavigation,\n  type NavigationRequestAccumulation,\n} from '../ppr-navigations'\nimport type { FlightRouterState } from '../../../../shared/lib/app-router-types'\nimport {\n  completeHardNavigation,\n  completeTraverseNavigation,\n  convertServerPatchToFullTree,\n} from '../../segment-cache/navigation'\nimport { UnknownDynamicStaleTime } from '../../segment-cache/bfcache'\n\nexport function restoreReducer(\n  state: ReadonlyReducerState,\n  action: RestoreAction\n): ReducerState {\n  // This action is used to restore the router state from the history state.\n  // However, it's possible that the history state no longer contains the `FlightRouterState`.\n  // We will copy over the internal state on pushState/replaceState events, but if a history entry\n  // occurred before hydration, or if the user navigated to a hash using a regular anchor link,\n  // the history state will not contain the `FlightRouterState`.\n  // In this case, we'll continue to use the existing tree so the router doesn't get into an invalid state.\n  let treeToRestore: FlightRouterState | undefined\n  let renderedSearch: string | undefined\n  const historyState = action.historyState\n  if (historyState) {\n    treeToRestore = historyState.tree\n    renderedSearch = historyState.renderedSearch\n  } else {\n    treeToRestore = state.tree\n    renderedSearch = state.renderedSearch\n  }\n\n  const currentUrl = new URL(state.canonicalUrl, location.origin)\n  const restoredUrl = action.url\n  const restoredNextUrl =\n    extractPathFromFlightRouterState(treeToRestore) ?? restoredUrl.pathname\n\n  const now = Date.now()\n  // TODO: Store the dynamic stale time on the top-level state so it's known\n  // during restores and refreshes.\n  const accumulation: NavigationRequestAccumulation = {\n    separateRefreshUrls: null,\n    scrollRef: null,\n  }\n  const restoreSeed = convertServerPatchToFullTree(\n    now,\n    treeToRestore,\n    null,\n    renderedSearch,\n    UnknownDynamicStaleTime\n  )\n  const task = startPPRNavigation(\n    now,\n    currentUrl,\n    state.renderedSearch,\n    state.cache,\n    state.tree,\n    restoreSeed.routeTree,\n    restoreSeed.metadataVaryPath,\n    FreshnessPolicy.HistoryTraversal,\n    null,\n    null,\n    restoreSeed.dynamicStaleAt,\n    false,\n    accumulation\n  )\n\n  if (task === null) {\n    return completeHardNavigation(state, restoredUrl, 'replace')\n  }\n  spawnDynamicRequests(\n    task,\n    restoredUrl,\n    restoredNextUrl,\n    FreshnessPolicy.HistoryTraversal,\n    accumulation,\n    // History traversal doesn't use route prediction, so there's no route\n    // cache entry to mark as having a dynamic rewrite on mismatch. If a\n    // mismatch occurs, the retry handler will traverse the known route tree\n    // to find and mark the entry.\n    null,\n    // History traversal always uses 'replace'.\n    'replace'\n  )\n  return completeTraverseNavigation(\n    state,\n    restoredUrl,\n    renderedSearch,\n    task.node,\n    task.route,\n    restoredNextUrl\n  )\n}\n"],"names":["extractPathFromFlightRouterState","FreshnessPolicy","spawnDynamicRequests","startPPRNavigation","completeHardNavigation","completeTraverseNavigation","convertServerPatchToFullTree","UnknownDynamicStaleTime","restoreReducer","state","action","treeToRestore","renderedSearch","historyState","tree","currentUrl","URL","canonicalUrl","location","origin","restoredUrl","url","restoredNextUrl","pathname","now","Date","accumulation","separateRefreshUrls","scrollRef","restoreSeed","task","cache","routeTree","metadataVaryPath","HistoryTraversal","dynamicStaleAt","node","route"],"mappings":"AAKA,SAASA,gCAAgC,QAAQ,0BAAyB;AAC1E,SACEC,eAAe,EACfC,oBAAoB,EACpBC,kBAAkB,QAEb,qBAAoB;AAE3B,SACEC,sBAAsB,EACtBC,0BAA0B,EAC1BC,4BAA4B,QACvB,iCAAgC;AACvC,SAASC,uBAAuB,QAAQ,8BAA6B;AAErE,OAAO,SAASC,eACdC,KAA2B,EAC3BC,MAAqB;IAErB,0EAA0E;IAC1E,4FAA4F;IAC5F,gGAAgG;IAChG,6FAA6F;IAC7F,8DAA8D;IAC9D,yGAAyG;IACzG,IAAIC;IACJ,IAAIC;IACJ,MAAMC,eAAeH,OAAOG,YAAY;IACxC,IAAIA,cAAc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