-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/next/src/client/components/router-reducer/prefetch-cache-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
PrefetchCacheEntryStatus, | ||
type AppRouterState, | ||
type PrefetchCacheEntry, | ||
} from './router-reducer-types' | ||
import { addPathPrefix } from '../../../shared/lib/router/utils/add-path-prefix' | ||
import { pathHasPrefix } from '../../../shared/lib/router/utils/path-has-prefix' | ||
import { createHrefFromUrl } from './create-href-from-url' | ||
|
||
/** | ||
* 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. | ||
*/ | ||
export function createPrefetchCacheKey(url: URL, nextUrl: string | null) { | ||
const pathnameFromUrl = createHrefFromUrl( | ||
url, | ||
// Ensures the hash is not part of the cache key as it does not impact the server fetch | ||
false | ||
) | ||
|
||
// delimit the prefix so we don't conflict with other pages | ||
const nextUrlPrefix = `${nextUrl}%` | ||
|
||
// Route interception depends on `nextUrl` values which aren't a 1:1 mapping to a URL | ||
// The cache key that we store needs to use `nextUrl` to properly distinguish cache entries | ||
if (nextUrl && !pathHasPrefix(pathnameFromUrl, nextUrl)) { | ||
return addPathPrefix(pathnameFromUrl, nextUrlPrefix) | ||
} | ||
|
||
return pathnameFromUrl | ||
} | ||
|
||
export function prunePrefetchCache( | ||
prefetchCache: AppRouterState['prefetchCache'] | ||
) { | ||
for (const [href, prefetchCacheEntry] of prefetchCache) { | ||
if ( | ||
getPrefetchEntryCacheStatus(prefetchCacheEntry) === | ||
PrefetchCacheEntryStatus.expired | ||
) { | ||
prefetchCache.delete(href) | ||
} | ||
} | ||
} | ||
|
||
const FIVE_MINUTES = 5 * 60 * 1000 | ||
const THIRTY_SECONDS = 30 * 1000 | ||
|
||
export function getPrefetchEntryCacheStatus({ | ||
kind, | ||
prefetchTime, | ||
lastUsedTime, | ||
}: PrefetchCacheEntry): PrefetchCacheEntryStatus { | ||
// if the cache entry was prefetched or read less than 30s ago, then we want to re-use it | ||
if (Date.now() < (lastUsedTime ?? prefetchTime) + THIRTY_SECONDS) { | ||
return lastUsedTime | ||
? PrefetchCacheEntryStatus.reusable | ||
: PrefetchCacheEntryStatus.fresh | ||
} | ||
|
||
// if the cache entry was prefetched less than 5 mins ago, then we want to re-use only the loading state | ||
if (kind === 'auto') { | ||
if (Date.now() < prefetchTime + FIVE_MINUTES) { | ||
return PrefetchCacheEntryStatus.stale | ||
} | ||
} | ||
|
||
// if the cache entry was prefetched less than 5 mins ago and was a "full" prefetch, then we want to re-use it "full | ||
if (kind === 'full') { | ||
if (Date.now() < prefetchTime + FIVE_MINUTES) { | ||
return PrefetchCacheEntryStatus.reusable | ||
} | ||
} | ||
|
||
return PrefetchCacheEntryStatus.expired | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp' | ||
import type { | ||
ManifestHeaderRoute, | ||
ManifestRedirectRoute, | ||
ManifestRewriteRoute, | ||
} from '../build' | ||
import { | ||
normalizeRouteRegex, | ||
type Header, | ||
type Redirect, | ||
type Rewrite, | ||
type RouteType, | ||
} from './load-custom-routes' | ||
import { getRedirectStatus, modifyRouteRegex } from './redirect-status' | ||
|
||
export function buildCustomRoute( | ||
type: 'header', | ||
route: Header | ||
): ManifestHeaderRoute | ||
export function buildCustomRoute( | ||
type: 'rewrite', | ||
route: Rewrite | ||
): ManifestRewriteRoute | ||
export function buildCustomRoute( | ||
type: 'redirect', | ||
route: Redirect, | ||
restrictedRedirectPaths: string[] | ||
): ManifestRedirectRoute | ||
export function buildCustomRoute( | ||
type: RouteType, | ||
route: Redirect | Rewrite | Header, | ||
restrictedRedirectPaths?: string[] | ||
): ManifestHeaderRoute | ManifestRewriteRoute | ManifestRedirectRoute { | ||
const compiled = pathToRegexp(route.source, [], { | ||
strict: true, | ||
sensitive: false, | ||
delimiter: '/', // default is `/#?`, but Next does not pass query info | ||
}) | ||
|
||
let source = compiled.source | ||
if (!route.internal) { | ||
source = modifyRouteRegex( | ||
source, | ||
type === 'redirect' ? restrictedRedirectPaths : undefined | ||
) | ||
} | ||
|
||
const regex = normalizeRouteRegex(source) | ||
|
||
if (type !== 'redirect') { | ||
return { ...route, regex } | ||
} | ||
|
||
return { | ||
...route, | ||
statusCode: getRedirectStatus(route as Redirect), | ||
permanent: undefined, | ||
regex, | ||
} | ||
} |