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

Function fetchNextData

packages/next/src/shared/lib/router/router.ts:498–643  ·  view source on GitHub ↗
({
  dataHref,
  inflightCache,
  isPrefetch,
  hasMiddleware,
  isServerRender,
  parseJSON,
  persistCache,
  isBackground,
  unstable_skipClientCache,
}: FetchNextDataParams)

Source from the content-addressed store, hash-verified

496}
497
498function fetchNextData({
499 dataHref,
500 inflightCache,
501 isPrefetch,
502 hasMiddleware,
503 isServerRender,
504 parseJSON,
505 persistCache,
506 isBackground,
507 unstable_skipClientCache,
508}: FetchNextDataParams): Promise<FetchDataOutput> {
509 const { href: cacheKey } = new URL(dataHref, window.location.href)
510 const deploymentId = getDeploymentId()
511 const getData = (params?: { method?: 'HEAD' | 'GET' }) =>
512 fetchRetry(dataHref, isServerRender ? 3 : 1, {
513 headers: Object.assign(
514 {} as HeadersInit,
515 isPrefetch ? { purpose: 'prefetch' } : {},
516 isPrefetch && hasMiddleware ? { 'x-middleware-prefetch': '1' } : {},
517 deploymentId ? { 'x-deployment-id': deploymentId } : {}
518 ),
519 method: params?.method ?? 'GET',
520 })
521 .then((response) => {
522 if (response.ok && params?.method === 'HEAD') {
523 return { dataHref, response, text: '', json: {}, cacheKey }
524 }
525
526 return response.text().then((text) => {
527 if (!response.ok) {
528 /**
529 * When the data response is a redirect because of a middleware
530 * we do not consider it an error. The headers must bring the
531 * mapped location.
532 * TODO: Change the status code in the handler.
533 */
534 if (
535 hasMiddleware &&
536 [301, 302, 307, 308].includes(response.status)
537 ) {
538 return { dataHref, response, text, json: {}, cacheKey }
539 }
540
541 if (response.status === 404) {
542 if (tryToParseAsJSON(text)?.notFound) {
543 return {
544 dataHref,
545 json: { notFound: SSG_DATA_NOT_FOUND },
546 response,
547 text,
548 cacheKey,
549 }
550 }
551 }
552
553 const error = new Error(`Failed to load static props`)
554
555 /**

Callers 2

getRouteInfoMethod · 0.85
prefetchMethod · 0.85

Calls 5

getDeploymentIdFunction · 0.90
thenMethod · 0.80
getDataFunction · 0.70
getMethod · 0.65
resolveMethod · 0.65

Tested by

no test coverage detected