File size: 8,621 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type {
  CacheNodeSeedData,
  FlightRouterState,
} from '../../../server/app-render/types'
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import {
  addSearchParamsIfPageSegment,
  PAGE_SEGMENT_KEY,
} from '../../../shared/lib/segment'
import type { NormalizedFlightData } from '../../flight-data-helpers'
import { createEmptyCacheNode } from '../app-router'
import { applyRouterStatePatchToTree } from './apply-router-state-patch-to-tree'
import { createHrefFromUrl } from './create-href-from-url'
import { createRouterCacheKey } from './create-router-cache-key'
import { fillCacheWithNewSubTreeDataButOnlyLoading } from './fill-cache-with-new-subtree-data'
import { handleMutable } from './handle-mutable'
import type { Mutable, ReadonlyReducerState } from './router-reducer-types'

/**
 * This is a stop-gap until per-segment caching is implemented. It leverages the `aliased` flag that is added
 * to prefetch entries when it's determined that the loading state from that entry should be used for this navigation.
 * This function takes the aliased entry and only applies the loading state to the updated cache node.
 * We should remove this once per-segment fetching is implemented as ideally the prefetch cache will contain a
 * more granular segment map and so the router will be able to simply re-use the loading segment for the new navigation.
 */
export function handleAliasedPrefetchEntry(
  navigatedAt: number,
  state: ReadonlyReducerState,
  flightData: string | NormalizedFlightData[],
  url: URL,
  mutable: Mutable
) {
  let currentTree = state.tree
  let currentCache = state.cache
  const href = createHrefFromUrl(url)
  let applied

  if (typeof flightData === 'string') {
    return false
  }

  for (const normalizedFlightData of flightData) {
    // If the segment doesn't have a loading component, we don't need to do anything.
    if (!hasLoadingComponentInSeedData(normalizedFlightData.seedData)) {
      continue
    }

    let treePatch = normalizedFlightData.tree
    // Segments are keyed by searchParams (e.g. __PAGE__?{"foo":"bar"}). We might return a less specific, param-less entry,
    // so we ensure that the final tree contains the correct searchParams (reflected in the URL) are provided in the updated FlightRouterState tree.
    // We only do this on the first read, as otherwise we'd be overwriting the searchParams that may have already been set
    treePatch = addSearchParamsToPageSegments(
      treePatch,
      Object.fromEntries(url.searchParams)
    )

    const { seedData, isRootRender, pathToSegment } = normalizedFlightData
    // TODO-APP: remove ''
    const flightSegmentPathWithLeadingEmpty = ['', ...pathToSegment]

    // Segments are keyed by searchParams (e.g. __PAGE__?{"foo":"bar"}). We might return a less specific, param-less entry,
    // so we ensure that the final tree contains the correct searchParams (reflected in the URL) are provided in the updated FlightRouterState tree.
    // We only do this on the first read, as otherwise we'd be overwriting the searchParams that may have already been set
    treePatch = addSearchParamsToPageSegments(
      treePatch,
      Object.fromEntries(url.searchParams)
    )

    let newTree = applyRouterStatePatchToTree(
      flightSegmentPathWithLeadingEmpty,
      currentTree,
      treePatch,
      href
    )

    const newCache = createEmptyCacheNode()

    // The prefetch cache entry was aliased -- this signals that we only fill in the cache with the
    // loading state and not the actual parallel route seed data.
    if (isRootRender && seedData) {
      // Fill in the cache with the new loading / rsc data
      const rsc = seedData[1]
      const loading = seedData[3]
      newCache.loading = loading
      newCache.rsc = rsc

      // Construct a new tree and apply the aliased loading state for each parallel route
      fillNewTreeWithOnlyLoadingSegments(
        navigatedAt,
        newCache,
        currentCache,
        treePatch,
        seedData
      )
    } else {
      // Copy rsc for the root node of the cache.
      newCache.rsc = currentCache.rsc
      newCache.prefetchRsc = currentCache.prefetchRsc
      newCache.loading = currentCache.loading
      newCache.parallelRoutes = new Map(currentCache.parallelRoutes)

      // copy the loading state only into the leaf node (the part that changed)
      fillCacheWithNewSubTreeDataButOnlyLoading(
        navigatedAt,
        newCache,
        currentCache,
        normalizedFlightData
      )
    }

    // If we don't have an updated tree, there's no reason to update the cache, as the tree
    // dictates what cache nodes to render.
    if (newTree) {
      currentTree = newTree
      currentCache = newCache
      applied = true
    }
  }

  if (!applied) {
    return false
  }

  mutable.patchedTree = currentTree
  mutable.cache = currentCache
  mutable.canonicalUrl = href
  mutable.hashFragment = url.hash

  return handleMutable(state, mutable)
}

function hasLoadingComponentInSeedData(seedData: CacheNodeSeedData | null) {
  if (!seedData) return false

  const parallelRoutes = seedData[2]
  const loading = seedData[3]

  if (loading) {
    return true
  }

  for (const key in parallelRoutes) {
    if (hasLoadingComponentInSeedData(parallelRoutes[key])) {
      return true
    }
  }

  return false
}

function fillNewTreeWithOnlyLoadingSegments(
  navigatedAt: number,
  newCache: CacheNode,
  existingCache: CacheNode,
  routerState: FlightRouterState,
  cacheNodeSeedData: CacheNodeSeedData | null
) {
  const isLastSegment = Object.keys(routerState[1]).length === 0
  if (isLastSegment) {
    return
  }

  for (const key in routerState[1]) {
    const parallelRouteState = routerState[1][key]
    const segmentForParallelRoute = parallelRouteState[0]
    const cacheKey = createRouterCacheKey(segmentForParallelRoute)

    const parallelSeedData =
      cacheNodeSeedData !== null && cacheNodeSeedData[2][key] !== undefined
        ? cacheNodeSeedData[2][key]
        : null

    let newCacheNode: CacheNode
    if (parallelSeedData !== null) {
      // New data was sent from the server.
      const rsc = parallelSeedData[1]
      const loading = parallelSeedData[3]
      newCacheNode = {
        lazyData: null,
        // copy the layout but null the page segment as that's not meant to be used
        rsc: segmentForParallelRoute.includes(PAGE_SEGMENT_KEY) ? null : rsc,
        prefetchRsc: null,
        head: null,
        prefetchHead: null,
        parallelRoutes: new Map(),
        loading,
        navigatedAt,
      }
    } else {
      // No data available for this node. This will trigger a lazy fetch
      // during render.
      newCacheNode = {
        lazyData: null,
        rsc: null,
        prefetchRsc: null,
        head: null,
        prefetchHead: null,
        parallelRoutes: new Map(),
        loading: null,
        navigatedAt: -1,
      }
    }

    const existingParallelRoutes = newCache.parallelRoutes.get(key)
    if (existingParallelRoutes) {
      existingParallelRoutes.set(cacheKey, newCacheNode)
    } else {
      newCache.parallelRoutes.set(key, new Map([[cacheKey, newCacheNode]]))
    }

    fillNewTreeWithOnlyLoadingSegments(
      navigatedAt,
      newCacheNode,
      existingCache,
      parallelRouteState,
      parallelSeedData
    )
  }
}

/**
 * Add search params to the page segments in the flight router state
 * Page segments that are associated with search params have a page segment key
 * followed by a query string. This function will add those params to the page segment.
 * This is useful if we return an aliased prefetch entry (ie, won't have search params)
 * but the canonical router URL has search params.
 */
export function addSearchParamsToPageSegments(
  flightRouterState: FlightRouterState,
  searchParams: Record<string, string | string[] | undefined>
): FlightRouterState {
  const [segment, parallelRoutes, ...rest] = flightRouterState

  // If it's a page segment, modify the segment by adding search params
  if (segment.includes(PAGE_SEGMENT_KEY)) {
    const newSegment = addSearchParamsIfPageSegment(segment, searchParams)
    return [newSegment, parallelRoutes, ...rest]
  }

  // Otherwise, recurse through the parallel routes and return a new tree
  const updatedParallelRoutes: { [key: string]: FlightRouterState } = {}

  for (const [key, parallelRoute] of Object.entries(parallelRoutes)) {
    updatedParallelRoutes[key] = addSearchParamsToPageSegments(
      parallelRoute,
      searchParams
    )
  }

  return [segment, updatedParallelRoutes, ...rest]
}