File size: 7,093 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
import type {
  ResponseCacheEntry,
  ResponseGenerator,
  ResponseCacheBase,
  IncrementalResponseCacheEntry,
  IncrementalResponseCache,
} from './types'

import { Batcher } from '../../lib/batcher'
import { scheduleOnNextTick } from '../../lib/scheduler'
import {
  fromResponseCacheEntry,
  routeKindToIncrementalCacheKind,
  toResponseCacheEntry,
} from './utils'
import type { RouteKind } from '../route-kind'

export * from './types'

export default class ResponseCache implements ResponseCacheBase {
  private readonly batcher = Batcher.create<
    { key: string; isOnDemandRevalidate: boolean },
    IncrementalResponseCacheEntry | null,
    string
  >({
    // Ensure on-demand revalidate doesn't block normal requests, it should be
    // safe to run an on-demand revalidate for the same key as a normal request.
    cacheKeyFn: ({ key, isOnDemandRevalidate }) =>
      `${key}-${isOnDemandRevalidate ? '1' : '0'}`,
    // We wait to do any async work until after we've added our promise to
    // `pendingResponses` to ensure that any any other calls will reuse the
    // same promise until we've fully finished our work.
    schedulerFn: scheduleOnNextTick,
  })

  private previousCacheItem?: {
    key: string
    entry: IncrementalResponseCacheEntry | null
    expiresAt: number
  }

  // we don't use minimal_mode name here as this.minimal_mode is
  // statically replace for server runtimes but we need it to
  // be dynamic here
  private minimal_mode?: boolean

  constructor(minimal_mode: boolean) {
    this.minimal_mode = minimal_mode
  }

  public async get(
    key: string | null,
    responseGenerator: ResponseGenerator,
    context: {
      routeKind: RouteKind
      isOnDemandRevalidate?: boolean
      isPrefetch?: boolean
      incrementalCache: IncrementalResponseCache
      isRoutePPREnabled?: boolean
      isFallback?: boolean
      waitUntil?: (prom: Promise<any>) => void
    }
  ): Promise<ResponseCacheEntry | null> {
    // If there is no key for the cache, we can't possibly look this up in the
    // cache so just return the result of the response generator.
    if (!key) {
      return responseGenerator({ hasResolved: false, previousCacheEntry: null })
    }

    const {
      incrementalCache,
      isOnDemandRevalidate = false,
      isFallback = false,
      isRoutePPREnabled = false,
      waitUntil,
    } = context

    const response = await this.batcher.batch(
      { key, isOnDemandRevalidate },
      (cacheKey, resolve) => {
        const prom = (async () => {
          // We keep the previous cache entry around to leverage when the
          // incremental cache is disabled in minimal mode.
          if (
            this.minimal_mode &&
            this.previousCacheItem?.key === cacheKey &&
            this.previousCacheItem.expiresAt > Date.now()
          ) {
            return this.previousCacheItem.entry
          }

          // Coerce the kindHint into a given kind for the incremental cache.
          const kind = routeKindToIncrementalCacheKind(context.routeKind)

          let resolved = false
          let cachedResponse: IncrementalResponseCacheEntry | null = null
          try {
            cachedResponse = !this.minimal_mode
              ? await incrementalCache.get(key, {
                  kind,
                  isRoutePPREnabled: context.isRoutePPREnabled,
                  isFallback,
                })
              : null

            if (cachedResponse && !isOnDemandRevalidate) {
              resolve(cachedResponse)
              resolved = true

              if (!cachedResponse.isStale || context.isPrefetch) {
                // The cached value is still valid, so we don't need
                // to update it yet.
                return null
              }
            }

            const cacheEntry = await responseGenerator({
              hasResolved: resolved,
              previousCacheEntry: cachedResponse,
              isRevalidating: true,
            })

            // If the cache entry couldn't be generated, we don't want to cache
            // the result.
            if (!cacheEntry) {
              // Unset the previous cache item if it was set.
              if (this.minimal_mode) this.previousCacheItem = undefined
              return null
            }

            const resolveValue = await fromResponseCacheEntry({
              ...cacheEntry,
              isMiss: !cachedResponse,
            })
            if (!resolveValue) {
              // Unset the previous cache item if it was set.
              if (this.minimal_mode) this.previousCacheItem = undefined
              return null
            }

            // For on-demand revalidate wait to resolve until cache is set.
            // Otherwise resolve now.
            if (!isOnDemandRevalidate && !resolved) {
              resolve(resolveValue)
              resolved = true
            }

            // We want to persist the result only if it has a cache control value
            // defined.
            if (resolveValue.cacheControl) {
              if (this.minimal_mode) {
                this.previousCacheItem = {
                  key: cacheKey,
                  entry: resolveValue,
                  expiresAt: Date.now() + 1000,
                }
              } else {
                await incrementalCache.set(key, resolveValue.value, {
                  cacheControl: resolveValue.cacheControl,
                  isRoutePPREnabled,
                  isFallback,
                })
              }
            }

            return resolveValue
          } catch (err) {
            // When a path is erroring we automatically re-set the existing cache
            // with new revalidate and expire times to prevent non-stop retrying.
            if (cachedResponse?.cacheControl) {
              const newRevalidate = Math.min(
                Math.max(cachedResponse.cacheControl.revalidate || 3, 3),
                30
              )

              const newExpire =
                cachedResponse.cacheControl.expire === undefined
                  ? undefined
                  : Math.max(
                      newRevalidate + 3,
                      cachedResponse.cacheControl.expire
                    )

              await incrementalCache.set(key, cachedResponse.value, {
                cacheControl: { revalidate: newRevalidate, expire: newExpire },
                isRoutePPREnabled,
                isFallback,
              })
            }

            // While revalidating in the background we can't reject as we already
            // resolved the cache entry so log the error here.
            if (resolved) {
              console.error(err)
              return null
            }

            // We haven't resolved yet, so let's throw to indicate an error.
            throw err
          }
        })()

        // we need to ensure background revalidates are
        // passed to waitUntil
        if (waitUntil) {
          waitUntil(prom)
        }
        return prom
      }
    )

    return toResponseCacheEntry(response)
  }
}