File size: 5,606 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
/**
 * Entry point to the Segment Cache implementation.
 *
 * All code related to the Segment Cache lives `segment-cache-impl` directory.
 * Callers access it through this indirection.
 *
 * This is to ensure the code is dead code eliminated from the bundle if the
 * flag is disabled.
 *
 * TODO: This is super tedious. Since experimental flags are an essential part
 * of our workflow, we should establish a better pattern for dead code
 * elimination. Ideally it would be done at the bundler level, like how React's
 * build process works. In the React repo, you don't even need to add any extra
 * configuration per experiment — if the code is not reachable, it gets stripped
 * from the build automatically by Rollup. Or, shorter term, we could stub out
 * experimental modules at build time by updating the build config, i.e. a more
 * automated version of what I'm doing manually in this file.
 */

export type { NavigationResult } from './segment-cache-impl/navigation'
export type { PrefetchTask } from './segment-cache-impl/scheduler'

const notEnabled: any = () => {
  throw new Error(
    'Segment Cache experiment is not enabled. This is a bug in Next.js.'
  )
}

export const prefetch: typeof import('./segment-cache-impl/prefetch').prefetch =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/prefetch') as typeof import('./segment-cache-impl/prefetch')
        ).prefetch(...args)
      }
    : notEnabled

export const navigate: typeof import('./segment-cache-impl/navigation').navigate =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/navigation') as typeof import('./segment-cache-impl/navigation')
        ).navigate(...args)
      }
    : notEnabled

export const revalidateEntireCache: typeof import('./segment-cache-impl/cache').revalidateEntireCache =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/cache') as typeof import('./segment-cache-impl/cache')
        ).revalidateEntireCache(...args)
      }
    : notEnabled

export const getCurrentCacheVersion: typeof import('./segment-cache-impl/cache').getCurrentCacheVersion =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/cache') as typeof import('./segment-cache-impl/cache')
        ).getCurrentCacheVersion(...args)
      }
    : notEnabled

export const schedulePrefetchTask: typeof import('./segment-cache-impl/scheduler').schedulePrefetchTask =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/scheduler') as typeof import('./segment-cache-impl/scheduler')
        ).schedulePrefetchTask(...args)
      }
    : notEnabled

export const cancelPrefetchTask: typeof import('./segment-cache-impl/scheduler').cancelPrefetchTask =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/scheduler') as typeof import('./segment-cache-impl/scheduler')
        ).cancelPrefetchTask(...args)
      }
    : notEnabled

export const reschedulePrefetchTask: typeof import('./segment-cache-impl/scheduler').reschedulePrefetchTask =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/scheduler') as typeof import('./segment-cache-impl/scheduler')
        ).reschedulePrefetchTask(...args)
      }
    : notEnabled

export const isPrefetchTaskDirty: typeof import('./segment-cache-impl/scheduler').isPrefetchTaskDirty =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/scheduler') as typeof import('./segment-cache-impl/scheduler')
        ).isPrefetchTaskDirty(...args)
      }
    : notEnabled

export const createCacheKey: typeof import('./segment-cache-impl/cache-key').createCacheKey =
  process.env.__NEXT_CLIENT_SEGMENT_CACHE
    ? function (...args) {
        return (
          require('./segment-cache-impl/cache-key') as typeof import('./segment-cache-impl/cache-key')
        ).createCacheKey(...args)
      }
    : notEnabled

/**
 * Below are public constants. They're small enough that we don't need to
 * DCE them.
 */

export const enum NavigationResultTag {
  MPA,
  Success,
  NoOp,
  Async,
}

/**
 * The priority of the prefetch task. Higher numbers are higher priority.
 */
export const enum PrefetchPriority {
  /**
   * Assigned to the most recently hovered/touched link. Special network
   * bandwidth is reserved for this task only. There's only ever one Intent-
   * priority task at a time; when a new Intent task is scheduled, the previous
   * one is bumped down to Default.
   */
  Intent = 2,
  /**
   * The default priority for prefetch tasks.
   */
  Default = 1,
  /**
   * Assigned to tasks when they spawn non-blocking background work, like
   * revalidating a partially cached entry to see if more data is available.
   */
  Background = 0,
}

export const enum FetchStrategy {
  PPR,
  Full,
  LoadingBoundary,
}

/**
 * A subset of fetch strategies used for prefetch tasks.
 * A prefetch task can't know if it should use `PPR` or `LoadingBoundary`
 * until we complete the initial tree prefetch request, so we use `PPR` to signal both cases
 * and adjust it based on the route when actually fetching.
 * */
export type PrefetchTaskFetchStrategy = FetchStrategy.PPR | FetchStrategy.Full