| import type { |
| CacheNodeSeedData, |
| FlightData, |
| FlightDataPath, |
| FlightRouterState, |
| FlightSegmentPath, |
| Segment, |
| } from '../server/app-render/types' |
| import type { HeadData } from '../shared/lib/app-router-context.shared-runtime' |
| import { PAGE_SEGMENT_KEY } from '../shared/lib/segment' |
|
|
| export type NormalizedFlightData = { |
| |
| |
| |
| segmentPath: FlightSegmentPath |
| |
| |
| |
| pathToSegment: FlightSegmentPath |
| segment: Segment |
| tree: FlightRouterState |
| seedData: CacheNodeSeedData | null |
| head: HeadData |
| isHeadPartial: boolean |
| isRootRender: boolean |
| } |
|
|
| |
| |
| |
| |
| export function getFlightDataPartsFromPath( |
| flightDataPath: FlightDataPath |
| ): NormalizedFlightData { |
| |
| const flightDataPathLength = 4 |
| |
| const [tree, seedData, head, isHeadPartial] = |
| flightDataPath.slice(-flightDataPathLength) |
| |
| const segmentPath = flightDataPath.slice(0, -flightDataPathLength) |
|
|
| return { |
| |
| |
| |
| pathToSegment: segmentPath.slice(0, -1), |
| segmentPath, |
| |
| |
| segment: segmentPath[segmentPath.length - 1] ?? '', |
| tree, |
| seedData, |
| head, |
| isHeadPartial, |
| isRootRender: flightDataPath.length === flightDataPathLength, |
| } |
| } |
|
|
| export function getNextFlightSegmentPath( |
| flightSegmentPath: FlightSegmentPath |
| ): FlightSegmentPath { |
| |
| |
| return flightSegmentPath.slice(2) |
| } |
|
|
| export function normalizeFlightData( |
| flightData: FlightData |
| ): NormalizedFlightData[] | string { |
| |
| |
| if (typeof flightData === 'string') { |
| return flightData |
| } |
|
|
| return flightData.map(getFlightDataPartsFromPath) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function prepareFlightRouterStateForRequest( |
| flightRouterState: FlightRouterState, |
| isHmrRefresh?: boolean |
| ): string { |
| |
| if (isHmrRefresh) { |
| return encodeURIComponent(JSON.stringify(flightRouterState)) |
| } |
|
|
| return encodeURIComponent( |
| JSON.stringify(stripClientOnlyDataFromFlightRouterState(flightRouterState)) |
| ) |
| } |
|
|
| |
| |
| |
| |
| function stripClientOnlyDataFromFlightRouterState( |
| flightRouterState: FlightRouterState |
| ): FlightRouterState { |
| const [ |
| segment, |
| parallelRoutes, |
| _url, |
| refreshMarker, |
| isRootLayout, |
| hasLoadingBoundary, |
| ] = flightRouterState |
|
|
| |
| |
| const cleanedSegment = stripSearchParamsFromPageSegment(segment) |
|
|
| |
| const cleanedParallelRoutes: { [key: string]: FlightRouterState } = {} |
| for (const [key, childState] of Object.entries(parallelRoutes)) { |
| cleanedParallelRoutes[key] = |
| stripClientOnlyDataFromFlightRouterState(childState) |
| } |
|
|
| const result: FlightRouterState = [ |
| cleanedSegment, |
| cleanedParallelRoutes, |
| null, |
| shouldPreserveRefreshMarker(refreshMarker) ? refreshMarker : null, |
| ] |
|
|
| |
| if (isRootLayout !== undefined) { |
| result[4] = isRootLayout |
| } |
| if (hasLoadingBoundary !== undefined) { |
| result[5] = hasLoadingBoundary |
| } |
|
|
| return result |
| } |
|
|
| |
| |
| |
| |
| function stripSearchParamsFromPageSegment(segment: Segment): Segment { |
| if ( |
| typeof segment === 'string' && |
| segment.startsWith(PAGE_SEGMENT_KEY + '?') |
| ) { |
| return PAGE_SEGMENT_KEY |
| } |
| return segment |
| } |
|
|
| |
| |
| |
| |
| |
| function shouldPreserveRefreshMarker( |
| refreshMarker: FlightRouterState[3] |
| ): boolean { |
| return Boolean(refreshMarker && refreshMarker !== 'refresh') |
| } |
|
|