{"version":3,"sources":["../../src/client/flight-data-helpers.ts"],"sourcesContent":["import type {\n CacheNodeSeedData,\n FlightData,\n FlightDataPath,\n FlightRouterState,\n FlightSegmentPath,\n Segment,\n HeadData,\n InitialRSCPayload,\n} from '../shared/lib/app-router-types'\nimport { PAGE_SEGMENT_KEY } from '../shared/lib/segment'\nimport type { NormalizedSearch } from './components/segment-cache'\nimport {\n getCacheKeyForDynamicParam,\n parseDynamicParamFromURLPart,\n doesStaticSegmentAppearInURL,\n getRenderedPathname,\n getRenderedSearch,\n} from './route-params'\nimport { createHrefFromUrl } from './components/router-reducer/create-href-from-url'\n\nexport type NormalizedFlightData = {\n /**\n * The full `FlightSegmentPath` inclusive of the final `Segment`\n */\n segmentPath: FlightSegmentPath\n /**\n * The `FlightSegmentPath` exclusive of the final `Segment`\n */\n pathToSegment: FlightSegmentPath\n segment: Segment\n tree: FlightRouterState\n seedData: CacheNodeSeedData | null\n head: HeadData\n isHeadPartial: boolean\n isRootRender: boolean\n}\n\n// TODO: We should only have to export `normalizeFlightData`, however because the initial flight data\n// that gets passed to `createInitialRouterState` doesn't conform to the `FlightDataPath` type (it's missing the root segment)\n// we're currently exporting it so we can use it directly. This should be fixed as part of the unification of\n// the different ways we express `FlightSegmentPath`.\nexport function getFlightDataPartsFromPath(\n flightDataPath: FlightDataPath\n): NormalizedFlightData {\n // Pick the last 4 items from the `FlightDataPath` to get the [tree, seedData, viewport, isHeadPartial].\n const flightDataPathLength = 4\n // tree, seedData, and head are *always* the last three items in the `FlightDataPath`.\n const [tree, seedData, head, isHeadPartial] =\n flightDataPath.slice(-flightDataPathLength)\n // The `FlightSegmentPath` is everything except the last three items. For a root render, it won't be present.\n const segmentPath = flightDataPath.slice(0, -flightDataPathLength)\n\n return {\n // TODO: Unify these two segment path helpers. We are inconsistently pushing an empty segment (\"\")\n // to the start of the segment path in some places which makes it hard to use solely the segment path.\n // Look for \"// TODO-APP: remove ''\" in the codebase.\n pathToSegment: segmentPath.slice(0, -1),\n segmentPath,\n // if the `FlightDataPath` corresponds with the root, there'll be no segment path,\n // in which case we default to ''.\n segment: segmentPath[segmentPath.length - 1] ?? '',\n tree,\n seedData,\n head,\n isHeadPartial,\n isRootRender: flightDataPath.length === flightDataPathLength,\n }\n}\n\nexport function createInitialRSCPayloadFromFallbackPrerender(\n response: Response,\n fallbackInitialRSCPayload: InitialRSCPayload\n): InitialRSCPayload {\n // This is a static fallback page. In order to hydrate the page, we need to\n // parse the client params from the URL, but to account for the possibility\n // that the page was rewritten, we need to check the response headers\n // for x-nextjs-rewritten-path or x-nextjs-rewritten-query headers. Since\n // we can't access the headers of the initial document response, the client\n // performs a fetch request to the current location. Since it's possible that\n // the fetch request will be dynamically rewritten to a different path than\n // the initial document, this fetch request delivers _all_ the hydration data\n // for the page; it was not inlined into the document, like it normally\n // would be.\n //\n // TODO: Consider treating the case where fetch is rewritten to a different\n // path from the document as a special deopt case. We should optimistically\n // assume this won't happen, inline the data into the document, and perform\n // a minimal request (like a HEAD or range request) to verify that the\n // response matches. Tricky to get right because we need to account for\n // all the different deployment environments we support, like output:\n // \"export\" mode, where we currently don't assume that custom response\n // headers are present.\n\n // Patch the Flight data sent by the server with the correct params parsed\n // from the URL + response object.\n const renderedPathname = getRenderedPathname(response)\n const renderedSearch = getRenderedSearch(response)\n const canonicalUrl = createHrefFromUrl(new URL(location.href))\n const originalFlightDataPath = fallbackInitialRSCPayload.f[0]\n const originalFlightRouterState = originalFlightDataPath[0]\n return {\n b: fallbackInitialRSCPayload.b,\n c: canonicalUrl.split('/'),\n q: renderedSearch,\n i: fallbackInitialRSCPayload.i,\n f: [\n [\n fillInFallbackFlightRouterState(\n originalFlightRouterState,\n renderedPathname,\n renderedSearch as NormalizedSearch\n ),\n originalFlightDataPath[1],\n originalFlightDataPath[2],\n originalFlightDataPath[2],\n ],\n ],\n m: fallbackInitialRSCPayload.m,\n G: fallbackInitialRSCPayload.G,\n s: fallbackInitialRSCPayload.s,\n S: fallbackInitialRSCPayload.S,\n }\n}\n\nfunction fillInFallbackFlightRouterState(\n flightRouterState: FlightRouterState,\n renderedPathname: string,\n renderedSearch: NormalizedSearch\n): FlightRouterState {\n const pathnameParts = renderedPathname.split('/').filter((p) => p !== '')\n const index = 0\n return fillInFallbackFlightRouterStateImpl(\n flightRouterState,\n renderedSearch,\n pathnameParts,\n index\n )\n}\n\nfunction fillInFallbackFlightRouterStateImpl(\n flightRouterState: FlightRouterState,\n renderedSearch: NormalizedSearch,\n pathnameParts: Array,\n pathnamePartsIndex: number\n): FlightRouterState {\n const originalSegment = flightRouterState[0]\n let newSegment: Segment\n let doesAppearInURL: boolean\n if (typeof originalSegment === 'string') {\n newSegment = originalSegment\n doesAppearInURL = doesStaticSegmentAppearInURL(originalSegment)\n } else {\n const paramName = originalSegment[0]\n const paramType = originalSegment[2]\n const paramValue = parseDynamicParamFromURLPart(\n paramType,\n pathnameParts,\n pathnamePartsIndex\n )\n const cacheKey = getCacheKeyForDynamicParam(paramValue, renderedSearch)\n newSegment = [paramName, cacheKey, paramType]\n doesAppearInURL = true\n }\n\n // Only increment the index if the segment appears in the URL. If it's a\n // \"virtual\" segment, like a route group, it remains the same.\n const childPathnamePartsIndex = doesAppearInURL\n ? pathnamePartsIndex + 1\n : pathnamePartsIndex\n\n const children = flightRouterState[1]\n const newChildren: { [key: string]: FlightRouterState } = {}\n for (let key in children) {\n const childFlightRouterState = children[key]\n newChildren[key] = fillInFallbackFlightRouterStateImpl(\n childFlightRouterState,\n renderedSearch,\n pathnameParts,\n childPathnamePartsIndex\n )\n }\n\n const newState: FlightRouterState = [\n newSegment,\n newChildren,\n null,\n flightRouterState[3],\n flightRouterState[4],\n ]\n return newState\n}\n\nexport function getNextFlightSegmentPath(\n flightSegmentPath: FlightSegmentPath\n): FlightSegmentPath {\n // Since `FlightSegmentPath` is a repeated tuple of `Segment` and `ParallelRouteKey`, we slice off two items\n // to get the next segment path.\n return flightSegmentPath.slice(2)\n}\n\nexport function normalizeFlightData(\n flightData: FlightData\n): NormalizedFlightData[] | string {\n // FlightData can be a string when the server didn't respond with a proper flight response,\n // or when a redirect happens, to signal to the client that it needs to perform an MPA navigation.\n if (typeof flightData === 'string') {\n return flightData\n }\n\n return flightData.map((flightDataPath) =>\n getFlightDataPartsFromPath(flightDataPath)\n )\n}\n\n/**\n * This function is used to prepare the flight router state for the request.\n * It removes markers that are not needed by the server, and are purely used\n * for stashing state on the client.\n * @param flightRouterState - The flight router state to prepare.\n * @param isHmrRefresh - Whether this is an HMR refresh request.\n * @returns The prepared flight router state.\n */\nexport function prepareFlightRouterStateForRequest(\n flightRouterState: FlightRouterState,\n isHmrRefresh?: boolean\n): string {\n // HMR requests need the complete, unmodified state for proper functionality\n if (isHmrRefresh) {\n return encodeURIComponent(JSON.stringify(flightRouterState))\n }\n\n return encodeURIComponent(\n JSON.stringify(stripClientOnlyDataFromFlightRouterState(flightRouterState))\n )\n}\n\n/**\n * Recursively strips client-only data from FlightRouterState while preserving\n * server-needed information for proper rendering decisions.\n */\nfunction stripClientOnlyDataFromFlightRouterState(\n flightRouterState: FlightRouterState\n): FlightRouterState {\n const [\n segment,\n parallelRoutes,\n _url, // Intentionally unused - URLs are client-only\n refreshMarker,\n isRootLayout,\n hasLoadingBoundary,\n ] = flightRouterState\n\n // __PAGE__ segments are always fetched from the server, so there's\n // no need to send them up\n const cleanedSegment = stripSearchParamsFromPageSegment(segment)\n\n // Recursively process parallel routes\n const cleanedParallelRoutes: { [key: string]: FlightRouterState } = {}\n for (const [key, childState] of Object.entries(parallelRoutes)) {\n cleanedParallelRoutes[key] =\n stripClientOnlyDataFromFlightRouterState(childState)\n }\n\n const result: FlightRouterState = [\n cleanedSegment,\n cleanedParallelRoutes,\n null, // URLs omitted - server reconstructs paths from segments\n shouldPreserveRefreshMarker(refreshMarker) ? refreshMarker : null,\n ]\n\n // Append optional fields if present\n if (isRootLayout !== undefined) {\n result[4] = isRootLayout\n }\n if (hasLoadingBoundary !== undefined) {\n result[5] = hasLoadingBoundary\n }\n\n return result\n}\n\n/**\n * Strips search parameters from __PAGE__ segments to prevent sensitive\n * client-side data from being sent to the server.\n */\nfunction stripSearchParamsFromPageSegment(segment: Segment): Segment {\n if (\n typeof segment === 'string' &&\n segment.startsWith(PAGE_SEGMENT_KEY + '?')\n ) {\n return PAGE_SEGMENT_KEY\n }\n return segment\n}\n\n/**\n * Determines whether the refresh marker should be sent to the server\n * Client-only markers like 'refresh' are stripped, while server-needed markers\n * like 'refetch' and 'inside-shared-layout' are preserved.\n */\nfunction shouldPreserveRefreshMarker(\n refreshMarker: FlightRouterState[3]\n): boolean {\n return Boolean(refreshMarker && refreshMarker !== 'refresh')\n}\n"],"names":["PAGE_SEGMENT_KEY","getCacheKeyForDynamicParam","parseDynamicParamFromURLPart","doesStaticSegmentAppearInURL","getRenderedPathname","getRenderedSearch","createHrefFromUrl","getFlightDataPartsFromPath","flightDataPath","flightDataPathLength","tree","seedData","head","isHeadPartial","slice","segmentPath","pathToSegment","segment","length","isRootRender","createInitialRSCPayloadFromFallbackPrerender","response","fallbackInitialRSCPayload","renderedPathname","renderedSearch","canonicalUrl","URL","location","href","originalFlightDataPath","f","originalFlightRouterState","b","c","split","q","i","fillInFallbackFlightRouterState","m","G","s","S","flightRouterState","pathnameParts","filter","p","index","fillInFallbackFlightRouterStateImpl","pathnamePartsIndex","originalSegment","newSegment","doesAppearInURL","paramName","paramType","paramValue","cacheKey","childPathnamePartsIndex","children","newChildren","key","childFlightRouterState","newState","getNextFlightSegmentPath","flightSegmentPath","normalizeFlightData","flightData","map","prepareFlightRouterStateForRequest","isHmrRefresh","encodeURIComponent","JSON","stringify","stripClientOnlyDataFromFlightRouterState","parallelRoutes","_url","refreshMarker","isRootLayout","hasLoadingBoundary","cleanedSegment","stripSearchParamsFromPageSegment","cleanedParallelRoutes","childState","Object","entries","result","shouldPreserveRefreshMarker","undefined","startsWith","Boolean"],"mappings":"AAUA,SAASA,gBAAgB,QAAQ,wBAAuB;AAExD,SACEC,0BAA0B,EAC1BC,4BAA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