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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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import {
  fetchServerResponse,
  type FetchServerResponseResult,
} from './fetch-server-response'
import {
  PrefetchCacheEntryStatus,
  type PrefetchCacheEntry,
  PrefetchKind,
  type ReadonlyReducerState,
} from './router-reducer-types'
import { prefetchQueue } from './reducers/prefetch-reducer'

const INTERCEPTION_CACHE_KEY_MARKER = '%'

export type AliasedPrefetchCacheEntry = PrefetchCacheEntry & {
  /** This is a special property that indicates a prefetch entry associated with a different URL
   * was returned rather than the requested URL. This signals to the router that it should only
   * apply the part that doesn't depend on searchParams (specifically the loading state).
   */
  aliased?: boolean
}

/**
 * Creates a cache key for the router prefetch cache
 *
 * @param url - The URL being navigated to
 * @param nextUrl - an internal URL, primarily used for handling rewrites. Defaults to '/'.
 * @return The generated prefetch cache key.
 */
function createPrefetchCacheKeyImpl(
  url: URL,
  includeSearchParams: boolean,
  prefix?: string | null
) {
  // Initially we only use the pathname as the cache key. We don't want to include
  // search params so that multiple URLs with the same search parameter can re-use
  // loading states.
  let pathnameFromUrl = url.pathname

  // RSC responses can differ based on search params, specifically in the case where we aren't
  // returning a partial response (ie with `PrefetchKind.AUTO`).
  // In the auto case, since loading.js & layout.js won't have access to search params,
  // we can safely re-use that cache entry. But for full prefetches, we should not
  // re-use the cache entry as the response may differ.
  if (includeSearchParams) {
    // if we have a full prefetch, we can include the search param in the key,
    // as we'll be getting back a full response. The server might have read the search
    // params when generating the full response.
    pathnameFromUrl += url.search
  }

  if (prefix) {
    return `${prefix}${INTERCEPTION_CACHE_KEY_MARKER}${pathnameFromUrl}`
  }

  return pathnameFromUrl
}

function createPrefetchCacheKey(
  url: URL,
  kind: PrefetchKind | undefined,
  nextUrl?: string | null
) {
  return createPrefetchCacheKeyImpl(url, kind === PrefetchKind.FULL, nextUrl)
}

function getExistingCacheEntry(
  url: URL,
  kind: PrefetchKind = PrefetchKind.TEMPORARY,
  nextUrl: string | null,
  prefetchCache: Map<string, PrefetchCacheEntry>,
  allowAliasing: boolean
): AliasedPrefetchCacheEntry | undefined {
  // We first check if there's a more specific interception route prefetch entry
  // This is because when we detect a prefetch that corresponds with an interception route, we prefix it with nextUrl (see `createPrefetchCacheKey`)
  // to avoid conflicts with other pages that may have the same URL but render different things depending on the `Next-URL` header.
  for (const maybeNextUrl of [nextUrl, null]) {
    const cacheKeyWithParams = createPrefetchCacheKeyImpl(
      url,
      true,
      maybeNextUrl
    )
    const cacheKeyWithoutParams = createPrefetchCacheKeyImpl(
      url,
      false,
      maybeNextUrl
    )

    // First, we check if we have a cache entry that exactly matches the URL
    const cacheKeyToUse = url.search
      ? cacheKeyWithParams
      : cacheKeyWithoutParams

    const existingEntry = prefetchCache.get(cacheKeyToUse)
    if (existingEntry && allowAliasing) {
      // We know we're returning an aliased entry when the pathname matches but the search params don't,
      const isAliased =
        existingEntry.url.pathname === url.pathname &&
        existingEntry.url.search !== url.search

      if (isAliased) {
        return {
          ...existingEntry,
          aliased: true,
        }
      }

      return existingEntry
    }

    // If the request contains search params, and we're not doing a full prefetch, we can return the
    // param-less entry if it exists.
    // This is technically covered by the check at the bottom of this function, which iterates over cache entries,
    // but lets us arrive there quicker in the param-full case.
    const entryWithoutParams = prefetchCache.get(cacheKeyWithoutParams)
    if (
      process.env.NODE_ENV !== 'development' &&
      allowAliasing &&
      url.search &&
      kind !== PrefetchKind.FULL &&
      entryWithoutParams &&
      // We shouldn't return the aliased entry if it was relocated to a new cache key.
      // Since it's rewritten, it could respond with a completely different loading state.
      !entryWithoutParams.key.includes(INTERCEPTION_CACHE_KEY_MARKER)
    ) {
      return { ...entryWithoutParams, aliased: true }
    }
  }

  // If we've gotten to this point, we didn't find a specific cache entry that matched
  // the request URL.
  // We attempt a partial match by checking if there's a cache entry with the same pathname.
  // Regardless of what we find, since it doesn't correspond with the requested URL, we'll mark it "aliased".
  // This will signal to the router that it should only apply the loading state on the prefetched data.
  if (
    process.env.NODE_ENV !== 'development' &&
    kind !== PrefetchKind.FULL &&
    allowAliasing
  ) {
    for (const cacheEntry of prefetchCache.values()) {
      if (
        cacheEntry.url.pathname === url.pathname &&
        // We shouldn't return the aliased entry if it was relocated to a new cache key.
        // Since it's rewritten, it could respond with a completely different loading state.
        !cacheEntry.key.includes(INTERCEPTION_CACHE_KEY_MARKER)
      ) {
        return { ...cacheEntry, aliased: true }
      }
    }
  }

  return undefined
}

/**
 * Returns a prefetch cache entry if one exists. Otherwise creates a new one and enqueues a fetch request
 * to retrieve the prefetch data from the server.
 */
export function getOrCreatePrefetchCacheEntry({
  url,
  nextUrl,
  tree,
  prefetchCache,
  kind,
  allowAliasing = true,
}: Pick<ReadonlyReducerState, 'nextUrl' | 'prefetchCache' | 'tree'> & {
  url: URL
  kind?: PrefetchKind
  allowAliasing: boolean
}): AliasedPrefetchCacheEntry {
  const existingCacheEntry = getExistingCacheEntry(
    url,
    kind,
    nextUrl,
    prefetchCache,
    allowAliasing
  )

  if (existingCacheEntry) {
    // Grab the latest status of the cache entry and update it
    existingCacheEntry.status = getPrefetchEntryCacheStatus(existingCacheEntry)

    // when `kind` is provided, an explicit prefetch was requested.
    // if the requested prefetch is "full" and the current cache entry wasn't, we want to re-prefetch with the new intent
    const switchedToFullPrefetch =
      existingCacheEntry.kind !== PrefetchKind.FULL &&
      kind === PrefetchKind.FULL

    if (switchedToFullPrefetch) {
      // If we switched to a full prefetch, validate that the existing cache entry contained partial data.
      // It's possible that the cache entry was seeded with full data but has a cache type of "auto" (ie when cache entries
      // are seeded but without a prefetch intent)
      existingCacheEntry.data.then((prefetchResponse) => {
        const isFullPrefetch =
          Array.isArray(prefetchResponse.flightData) &&
          prefetchResponse.flightData.some((flightData) => {
            // If we started rendering from the root and we returned RSC data (seedData), we already had a full prefetch.
            return flightData.isRootRender && flightData.seedData !== null
          })

        if (!isFullPrefetch) {
          return createLazyPrefetchEntry({
            tree,
            url,
            nextUrl,
            prefetchCache,
            // If we didn't get an explicit prefetch kind, we want to set a temporary kind
            // rather than assuming the same intent as the previous entry, to be consistent with how we
            // lazily create prefetch entries when intent is left unspecified.
            kind: kind ?? PrefetchKind.TEMPORARY,
          })
        }
      })
    }

    // If the existing cache entry was marked as temporary, it means it was lazily created when attempting to get an entry,
    // where we didn't have the prefetch intent. Now that we have the intent (in `kind`), we want to update the entry to the more accurate kind.
    if (kind && existingCacheEntry.kind === PrefetchKind.TEMPORARY) {
      existingCacheEntry.kind = kind
    }

    // We've determined that the existing entry we found is still valid, so we return it.
    return existingCacheEntry
  }

  // If we didn't return an entry, create a new one.
  return createLazyPrefetchEntry({
    tree,
    url,
    nextUrl,
    prefetchCache,
    kind: kind || PrefetchKind.TEMPORARY,
  })
}

/*
 * Used to take an existing cache entry and prefix it with the nextUrl, if it exists.
 * This ensures that we don't have conflicting cache entries for the same URL (as is the case with route interception).
 */
function prefixExistingPrefetchCacheEntry({
  url,
  nextUrl,
  prefetchCache,
  existingCacheKey,
}: Pick<ReadonlyReducerState, 'nextUrl' | 'prefetchCache'> & {
  url: URL
  existingCacheKey: string
}) {
  const existingCacheEntry = prefetchCache.get(existingCacheKey)
  if (!existingCacheEntry) {
    // no-op -- there wasn't an entry to move
    return
  }

  const newCacheKey = createPrefetchCacheKey(
    url,
    existingCacheEntry.kind,
    nextUrl
  )
  prefetchCache.set(newCacheKey, { ...existingCacheEntry, key: newCacheKey })
  prefetchCache.delete(existingCacheKey)

  return newCacheKey
}

/**
 * Use to seed the prefetch cache with data that has already been fetched.
 */
export function createSeededPrefetchCacheEntry({
  nextUrl,
  tree,
  prefetchCache,
  url,
  data,
  kind,
}: Pick<ReadonlyReducerState, 'nextUrl' | 'tree' | 'prefetchCache'> & {
  url: URL
  data: FetchServerResponseResult
  kind: PrefetchKind
}) {
  // The initial cache entry technically includes full data, but it isn't explicitly prefetched -- we just seed the
  // prefetch cache so that we can skip an extra prefetch request later, since we already have the data.
  // if the prefetch corresponds with an interception route, we use the nextUrl to prefix the cache key
  const prefetchCacheKey = data.couldBeIntercepted
    ? createPrefetchCacheKey(url, kind, nextUrl)
    : createPrefetchCacheKey(url, kind)

  const prefetchEntry = {
    treeAtTimeOfPrefetch: tree,
    data: Promise.resolve(data),
    kind,
    prefetchTime: Date.now(),
    lastUsedTime: Date.now(),
    staleTime: data.staleTime,
    key: prefetchCacheKey,
    status: PrefetchCacheEntryStatus.fresh,
    url,
  } satisfies PrefetchCacheEntry

  prefetchCache.set(prefetchCacheKey, prefetchEntry)

  return prefetchEntry
}

/**
 * Creates a prefetch entry entry and enqueues a fetch request to retrieve the data.
 */
function createLazyPrefetchEntry({
  url,
  kind,
  tree,
  nextUrl,
  prefetchCache,
}: Pick<ReadonlyReducerState, 'nextUrl' | 'tree' | 'prefetchCache'> & {
  url: URL
  kind: PrefetchKind
}): PrefetchCacheEntry {
  const prefetchCacheKey = createPrefetchCacheKey(url, kind)

  // initiates the fetch request for the prefetch and attaches a listener
  // to the promise to update the prefetch cache entry when the promise resolves (if necessary)
  const data = prefetchQueue.enqueue(() =>
    fetchServerResponse(url, {
      flightRouterState: tree,
      nextUrl,
      prefetchKind: kind,
    }).then((prefetchResponse) => {
      // TODO: `fetchServerResponse` should be more tighly coupled to these prefetch cache operations
      // to avoid drift between this cache key prefixing logic
      // (which is currently directly influenced by the server response)
      let newCacheKey

      if (prefetchResponse.couldBeIntercepted) {
        // Determine if we need to prefix the cache key with the nextUrl
        newCacheKey = prefixExistingPrefetchCacheEntry({
          url,
          existingCacheKey: prefetchCacheKey,
          nextUrl,
          prefetchCache,
        })
      }

      // If the prefetch was a cache hit, we want to update the existing cache entry to reflect that it was a full prefetch.
      // This is because we know that a static response will contain the full RSC payload, and can be updated to respect the `static`
      // staleTime.
      if (prefetchResponse.prerendered) {
        const existingCacheEntry = prefetchCache.get(
          // if we prefixed the cache key due to route interception, we want to use the new key. Otherwise we use the original key
          newCacheKey ?? prefetchCacheKey
        )
        if (existingCacheEntry) {
          existingCacheEntry.kind = PrefetchKind.FULL
          if (prefetchResponse.staleTime !== -1) {
            // This is the stale time that was collected by the server during
            // static generation. Use this in place of the default stale time.
            existingCacheEntry.staleTime = prefetchResponse.staleTime
          }
        }
      }

      return prefetchResponse
    })
  )

  const prefetchEntry = {
    treeAtTimeOfPrefetch: tree,
    data,
    kind,
    prefetchTime: Date.now(),
    lastUsedTime: null,
    staleTime: -1,
    key: prefetchCacheKey,
    status: PrefetchCacheEntryStatus.fresh,
    url,
  }

  prefetchCache.set(prefetchCacheKey, prefetchEntry)

  return prefetchEntry
}

export function prunePrefetchCache(
  prefetchCache: ReadonlyReducerState['prefetchCache']
) {
  for (const [href, prefetchCacheEntry] of prefetchCache) {
    if (
      getPrefetchEntryCacheStatus(prefetchCacheEntry) ===
      PrefetchCacheEntryStatus.expired
    ) {
      prefetchCache.delete(href)
    }
  }
}

// These values are set by `define-env-plugin` (based on `nextConfig.experimental.staleTimes`)
// and default to 5 minutes (static) / 0 seconds (dynamic)
export const DYNAMIC_STALETIME_MS =
  Number(process.env.__NEXT_CLIENT_ROUTER_DYNAMIC_STALETIME) * 1000

export const STATIC_STALETIME_MS =
  Number(process.env.__NEXT_CLIENT_ROUTER_STATIC_STALETIME) * 1000

function getPrefetchEntryCacheStatus({
  kind,
  prefetchTime,
  lastUsedTime,
}: PrefetchCacheEntry): PrefetchCacheEntryStatus {
  // We will re-use the cache entry data for up to the `dynamic` staletime window.
  if (Date.now() < (lastUsedTime ?? prefetchTime) + DYNAMIC_STALETIME_MS) {
    return lastUsedTime
      ? PrefetchCacheEntryStatus.reusable
      : PrefetchCacheEntryStatus.fresh
  }

  // For "auto" prefetching, we'll re-use only the loading boundary for up to `static` staletime window.
  // A stale entry will only re-use the `loading` boundary, not the full data.
  // This will trigger a "lazy fetch" for the full data.
  if (kind === PrefetchKind.AUTO) {
    if (Date.now() < prefetchTime + STATIC_STALETIME_MS) {
      return PrefetchCacheEntryStatus.stale
    }
  }

  // for "full" prefetching, we'll re-use the cache entry data for up to `static` staletime window.
  if (kind === PrefetchKind.FULL) {
    if (Date.now() < prefetchTime + STATIC_STALETIME_MS) {
      return PrefetchCacheEntryStatus.reusable
    }
  }

  return PrefetchCacheEntryStatus.expired
}