MCPcopy
hub / github.com/vercel/next.js / prefetch

Method prefetch

packages/next/src/shared/lib/router/router.ts:2371–2556  ·  view source on GitHub ↗

* Prefetch page code, you may wait for the data during page rendering. * This feature only works in production! * @param url the href of prefetched page * @param asPath the as path of the prefetched page

(
    url: string,
    asPath: string = url,
    options: PrefetchOptions = {}
  )

Source from the content-addressed store, hash-verified

2369 * @param asPath the as path of the prefetched page
2370 */
2371 async prefetch(
2372 url: string,
2373 asPath: string = url,
2374 options: PrefetchOptions = {}
2375 ): Promise<void> {
2376 // Prefetch is not supported in development mode because it would trigger on-demand-entries
2377 if (process.env.NODE_ENV !== 'production') {
2378 return
2379 }
2380
2381 if (typeof window !== 'undefined' && isBot(window.navigator.userAgent)) {
2382 // No prefetches for bots that render the link since they are typically navigating
2383 // links via the equivalent of a hard navigation and hence never utilize these
2384 // prefetches.
2385 return
2386 }
2387 let parsed = parseRelativeUrl(url)
2388 const urlPathname = parsed.pathname
2389
2390 let { pathname, query } = parsed
2391 const originalPathname = pathname
2392
2393 if (process.env.__NEXT_I18N_SUPPORT) {
2394 if (options.locale === false) {
2395 pathname = normalizeLocalePath!(pathname, this.locales).pathname
2396 parsed.pathname = pathname
2397 url = formatWithValidation(parsed)
2398
2399 let parsedAs = parseRelativeUrl(asPath)
2400 const localePathResult = normalizeLocalePath!(
2401 parsedAs.pathname,
2402 this.locales
2403 )
2404 parsedAs.pathname = localePathResult.pathname
2405 options.locale = localePathResult.detectedLocale || this.defaultLocale
2406 asPath = formatWithValidation(parsedAs)
2407 }
2408 }
2409
2410 const pages = await this.pageLoader.getPageList()
2411 let resolvedAs = asPath
2412
2413 const locale =
2414 typeof options.locale !== 'undefined'
2415 ? options.locale || undefined
2416 : this.locale
2417
2418 const isMiddlewareMatch = await matchesMiddleware({
2419 asPath: asPath,
2420 locale: locale,
2421 router: this,
2422 })
2423
2424 if (process.env.__NEXT_HAS_REWRITES && asPath.startsWith('/')) {
2425 let rewrites: any
2426 ;({ __rewrites: rewrites } = await getClientBuildManifest())
2427
2428 const rewritesResult = resolveRewrites(

Callers

nothing calls this directly

Calls 15

_bflMethod · 0.95
isBotFunction · 0.90
parseRelativeUrlFunction · 0.90
formatWithValidationFunction · 0.90
getClientBuildManifestFunction · 0.90
addBasePathFunction · 0.90
addLocaleFunction · 0.90
removeLocaleFunction · 0.90
removeBasePathFunction · 0.90
isDynamicRouteFunction · 0.90
getRouteMatcherFunction · 0.90
getRouteRegexFunction · 0.90

Tested by

no test coverage detected