File size: 4,140 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
import { fetchServerResponse } from '../fetch-server-response'
import { createHrefFromUrl } from '../create-href-from-url'
import { applyRouterStatePatchToTree } from '../apply-router-state-patch-to-tree'
import { isNavigatingToNewRootLayout } from '../is-navigating-to-new-root-layout'
import type {
  ReadonlyReducerState,
  ReducerState,
  HmrRefreshAction,
  Mutable,
} from '../router-reducer-types'
import { handleExternalUrl } from './navigate-reducer'
import { handleMutable } from '../handle-mutable'
import { applyFlightData } from '../apply-flight-data'
import type { CacheNode } from '../../../../shared/lib/app-router-context.shared-runtime'
import { createEmptyCacheNode } from '../../app-router'
import { handleSegmentMismatch } from '../handle-segment-mismatch'
import { hasInterceptionRouteInCurrentTree } from './has-interception-route-in-current-tree'

// A version of refresh reducer that keeps the cache around instead of wiping all of it.
function hmrRefreshReducerImpl(
  state: ReadonlyReducerState,
  action: HmrRefreshAction
): ReducerState {
  const { origin } = action
  const mutable: Mutable = {}
  const href = state.canonicalUrl

  mutable.preserveCustomHistoryState = false

  const cache: CacheNode = createEmptyCacheNode()
  // If the current tree was intercepted, the nextUrl should be included in the request.
  // This is to ensure that the refresh request doesn't get intercepted, accidentally triggering the interception route.
  const includeNextUrl = hasInterceptionRouteInCurrentTree(state.tree)

  // TODO-APP: verify that `href` is not an external url.
  // Fetch data from the root of the tree.
  const navigatedAt = Date.now()
  cache.lazyData = fetchServerResponse(new URL(href, origin), {
    flightRouterState: [state.tree[0], state.tree[1], state.tree[2], 'refetch'],
    nextUrl: includeNextUrl ? state.nextUrl : null,
    isHmrRefresh: true,
  })

  return cache.lazyData.then(
    ({ flightData, canonicalUrl: canonicalUrlOverride }) => {
      // Handle case when navigating to page in `pages` from `app`
      if (typeof flightData === 'string') {
        return handleExternalUrl(
          state,
          mutable,
          flightData,
          state.pushRef.pendingPush
        )
      }

      // Remove cache.lazyData as it has been resolved at this point.
      cache.lazyData = null

      let currentTree = state.tree
      let currentCache = state.cache

      for (const normalizedFlightData of flightData) {
        const { tree: treePatch, isRootRender } = normalizedFlightData
        if (!isRootRender) {
          // TODO-APP: handle this case better
          console.log('REFRESH FAILED')
          return state
        }

        const newTree = applyRouterStatePatchToTree(
          // TODO-APP: remove ''
          [''],
          currentTree,
          treePatch,
          state.canonicalUrl
        )

        if (newTree === null) {
          return handleSegmentMismatch(state, action, treePatch)
        }

        if (isNavigatingToNewRootLayout(currentTree, newTree)) {
          return handleExternalUrl(
            state,
            mutable,
            href,
            state.pushRef.pendingPush
          )
        }

        const canonicalUrlOverrideHref = canonicalUrlOverride
          ? createHrefFromUrl(canonicalUrlOverride)
          : undefined

        if (canonicalUrlOverride) {
          mutable.canonicalUrl = canonicalUrlOverrideHref
        }
        const applied = applyFlightData(
          navigatedAt,
          currentCache,
          cache,
          normalizedFlightData
        )

        if (applied) {
          mutable.cache = cache
          currentCache = cache
        }

        mutable.patchedTree = newTree
        mutable.canonicalUrl = href

        currentTree = newTree
      }
      return handleMutable(state, mutable)
    },
    () => state
  )
}

function hmrRefreshReducerNoop(
  state: ReadonlyReducerState,
  _action: HmrRefreshAction
): ReducerState {
  return state
}

export const hmrRefreshReducer =
  process.env.NODE_ENV === 'production'
    ? hmrRefreshReducerNoop
    : hmrRefreshReducerImpl