File size: 5,211 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
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type { Segment } from '../../../server/app-render/types'
import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { createRouterCacheKey } from './create-router-cache-key'
import type { PrefetchCacheEntry } from './router-reducer-types'
import { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment'
import type { NormalizedFlightData } from '../../flight-data-helpers'

/**
 * Common logic for filling cache with new sub tree data.
 */
function fillCacheHelper(
  navigatedAt: number,
  newCache: CacheNode,
  existingCache: CacheNode,
  flightData: NormalizedFlightData,
  prefetchEntry: PrefetchCacheEntry | undefined,
  fillLazyItems: boolean
): void {
  const {
    segmentPath,
    seedData: cacheNodeSeedData,
    tree: treePatch,
    head,
  } = flightData
  let newCacheNode = newCache
  let existingCacheNode = existingCache

  for (let i = 0; i < segmentPath.length; i += 2) {
    const parallelRouteKey: string = segmentPath[i]
    const segment: Segment = segmentPath[i + 1]

    // segmentPath is a repeating tuple of parallelRouteKey and segment
    // we know we've hit the last entry we've reached our final pair
    const isLastEntry = i === segmentPath.length - 2
    const cacheKey = createRouterCacheKey(segment)

    const existingChildSegmentMap =
      existingCacheNode.parallelRoutes.get(parallelRouteKey)

    if (!existingChildSegmentMap) {
      // Bailout because the existing cache does not have the path to the leaf node
      // Will trigger lazy fetch in layout-router because of missing segment
      continue
    }

    let childSegmentMap = newCacheNode.parallelRoutes.get(parallelRouteKey)
    if (!childSegmentMap || childSegmentMap === existingChildSegmentMap) {
      childSegmentMap = new Map(existingChildSegmentMap)
      newCacheNode.parallelRoutes.set(parallelRouteKey, childSegmentMap)
    }

    const existingChildCacheNode = existingChildSegmentMap.get(cacheKey)
    let childCacheNode = childSegmentMap.get(cacheKey)

    if (isLastEntry) {
      if (
        cacheNodeSeedData &&
        (!childCacheNode ||
          !childCacheNode.lazyData ||
          childCacheNode === existingChildCacheNode)
      ) {
        const incomingSegment = cacheNodeSeedData[0]
        const rsc = cacheNodeSeedData[1]
        const loading = cacheNodeSeedData[3]

        childCacheNode = {
          lazyData: null,
          // When `fillLazyItems` is false, we only want to fill the RSC data for the layout,
          // not the page segment.
          rsc:
            fillLazyItems || incomingSegment !== PAGE_SEGMENT_KEY ? rsc : null,
          prefetchRsc: null,
          head: null,
          prefetchHead: null,
          loading,
          parallelRoutes:
            fillLazyItems && existingChildCacheNode
              ? new Map(existingChildCacheNode.parallelRoutes)
              : new Map(),
          navigatedAt,
        }

        if (existingChildCacheNode && fillLazyItems) {
          invalidateCacheByRouterState(
            childCacheNode,
            existingChildCacheNode,
            treePatch
          )
        }
        if (fillLazyItems) {
          fillLazyItemsTillLeafWithHead(
            navigatedAt,
            childCacheNode,
            existingChildCacheNode,
            treePatch,
            cacheNodeSeedData,
            head,
            prefetchEntry
          )
        }

        childSegmentMap.set(cacheKey, childCacheNode)
      }
      continue
    }

    if (!childCacheNode || !existingChildCacheNode) {
      // Bailout because the existing cache does not have the path to the leaf node
      // Will trigger lazy fetch in layout-router because of missing segment
      continue
    }

    if (childCacheNode === existingChildCacheNode) {
      childCacheNode = {
        lazyData: childCacheNode.lazyData,
        rsc: childCacheNode.rsc,
        prefetchRsc: childCacheNode.prefetchRsc,
        head: childCacheNode.head,
        prefetchHead: childCacheNode.prefetchHead,
        parallelRoutes: new Map(childCacheNode.parallelRoutes),
        loading: childCacheNode.loading,
      } as CacheNode
      childSegmentMap.set(cacheKey, childCacheNode)
    }

    // Move deeper into the cache nodes
    newCacheNode = childCacheNode
    existingCacheNode = existingChildCacheNode
  }
}

/**
 * Fill cache with rsc based on flightDataPath
 */
export function fillCacheWithNewSubTreeData(
  navigatedAt: number,
  newCache: CacheNode,
  existingCache: CacheNode,
  flightData: NormalizedFlightData,
  prefetchEntry?: PrefetchCacheEntry
): void {
  fillCacheHelper(
    navigatedAt,
    newCache,
    existingCache,
    flightData,
    prefetchEntry,
    true
  )
}

export function fillCacheWithNewSubTreeDataButOnlyLoading(
  navigatedAt: number,
  newCache: CacheNode,
  existingCache: CacheNode,
  flightData: NormalizedFlightData,
  prefetchEntry?: PrefetchCacheEntry
): void {
  fillCacheHelper(
    navigatedAt,
    newCache,
    existingCache,
    flightData,
    prefetchEntry,
    false
  )
}