File size: 1,274 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
import type {
  PrefetchAction,
  ReducerState,
  ReadonlyReducerState,
} from '../router-reducer-types'
import { PromiseQueue } from '../../promise-queue'
import {
  getOrCreatePrefetchCacheEntry,
  prunePrefetchCache,
} from '../prefetch-cache-utils'
export const prefetchQueue = new PromiseQueue(5)

export const prefetchReducer = process.env.__NEXT_CLIENT_SEGMENT_CACHE
  ? identityReducerWhenSegmentCacheIsEnabled
  : prefetchReducerImpl

function identityReducerWhenSegmentCacheIsEnabled<T>(state: T): T {
  // Unlike the old implementation, the Segment Cache doesn't store its data in
  // the router reducer state.
  //
  // This shouldn't be reachable because we wrap the prefetch API in a check,
  // too, which prevents the action from being dispatched. But it's here for
  // clarity + code elimination.
  return state
}

function prefetchReducerImpl(
  state: ReadonlyReducerState,
  action: PrefetchAction
): ReducerState {
  // let's prune the prefetch cache before we do anything else
  prunePrefetchCache(state.prefetchCache)

  const { url } = action

  getOrCreatePrefetchCacheEntry({
    url,
    nextUrl: state.nextUrl,
    prefetchCache: state.prefetchCache,
    kind: action.kind,
    tree: state.tree,
    allowAliasing: true,
  })

  return state
}