6

In most examples, to disable prefetching, they do so by disabling a specific link for prefetching, see the following example:

<Link href="/about" prefetch={false}>
  <a>About us</a>
</Link>

I want to set prefetch to false for the whole project. Are there settings for this in the next.config.js file?

How should I do this?

2 Answers 2

10

Unfortunately, it's not supported in Next.js to disable prefetch globally.

The first workaround

  1. create a Babel plugin local to your project which adds prefetch={false} everywhere we use <Link /> from 'next/link'.
/**
 * Based on the docs at https://nextjs.org/docs/api-reference/next/link, the
 * only way to disable prefetching is to make sure every <Link /> has <Link
 * prefetch={false} />
 *
 * We don't want to create a wrapper Component or go around changing every
 * single <Link />, so we use this Babel Plugin to add them in at build-time.
 */
module.exports = function (babel) {
  const { types: t } = babel
  return {
    name: 'disable-link-prefetching',
    visitor: {
      JSXOpeningElement(path) {
        if (path.node.name.name === 'Link') {
          path.node.attributes.push(
            t.jSXAttribute(
              t.jSXIdentifier('prefetch'),
              t.JSXExpressionContainer(t.booleanLiteral(false)),
            ),
          )
        }
      },
    },
  }
}
  1. Add/modify ./.babelrc to load your local plugin:
{
  "presets": ["next/babel"],
  "plugins": ["./babel/disable-nextjs-link-prefetching"]
}

The Second workaround

Create a custom link component and use prefetch={false} for it and use it instead of using the next/link directly.

import Link from 'next/link'

export default function MyLink(props) {
  // defaults prefetch to false if `prefetch` is not true
  return <Link {...props} prefetch={props.prefetch ?? false}>
}

Resource

Sign up to request clarification or add additional context in comments.

Comments

3

In addition to @Mina answer, note that disable prefetching on <Link> component with prefetch props doesn't disable hover prefetching as mentionned in this github discussion: https://github.com/vercel/next.js/discussions/24437

As described on this discussion there is another workaround for globally disabling prefetching. This solution can be used for the entire website or only on a specific page.

To disable prefetching on entire website. Add following code to _app.tsx

const router = useRouter()
useEffect(() => {
  router.prefetch = async () => { }
}, [router])

to disable prefetching on specific page add following code on your page

const router = useRouter()

useEffect(() => {
  const prefetch = router.prefetch
  router.prefetch = async () => { }
  return () => { router.prefetch = prefetch }
}, [router])

4 Comments

can't do that on server components
Prefetching is execute by browser, so you can add a dedicated component with this code and "use client" on top of it. Then you can add this component in server component where you want to disable prefetching.
hmm not a bad idea
I just deployed this, will come back to tell if I got performance improvements, but for me this should be marked as the correct answer, since it is really global and the least hacky of our options for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.