vercel/next.js · error · Error

Cannot prefetch ' ' because it cannot be converted to a URL.

Error message

Cannot prefetch '${href}' because it cannot be converted to a URL.

What it means

Defense in createPrefetchURL: constructing a URL from the link href (with base path applied) threw, meaning the href is not resolvable to a valid URL. The error is raised from that catch block; prefetch for this href is impossible and callers like router.prefetch surface the failure.

Solutions

  1. Pass a valid, URL-parseable href to prefetch(); remove prefetch on invalid or empty hrefs.
Defensive patterns

Strategy: validation

When it happens

Trigger: Prefetch is requested for an href that cannot be parsed into a URL.

Common situations: A Link or prefetch call receives a malformed or non-URL string such as an external scheme.


AI-assisted analysis of vercel/next.js@0eb3775416 (2026-08-19). Data as JSON: /api/errors/d821354455e91f15. Report an issue: GitHub.

Appendix: source

Thrown at packages/next/src/client/components/app-router-utils.ts:27

 * Given a link href, constructs the URL that should be prefetched. Returns null
 * in cases where prefetching should be disabled, like external URLs, or
 * during development.
 * @param href The href passed to <Link>, router.prefetch(), or similar
 * @returns A URL object to prefetch, or null if prefetching should be disabled
 */
export function createPrefetchURL(href: string): URL | null {
  // Don't prefetch for bots as they don't navigate.
  if (isBot(window.navigator.userAgent)) {
    return null
  }

  let url: URL
  try {
    url = new URL(addBasePath(href), window.location.href)
  } catch (_) {
    // TODO: Does this need to throw or can we just console.error instead? Does
    // anyone rely on this throwing? (Seems unlikely.)
    throw new Error(
      `Cannot prefetch '${href}' because it cannot be converted to a URL.`
    )
  }

  // Don't prefetch during development (improves compilation performance)
  if (process.env.NODE_ENV === 'development') {
    return null
  }

  // External urls can't be prefetched in the same way.
  if (isExternalURL(url)) {
    return null
  }

  return url
}

View on GitHub (pinned to 0eb3775416)