question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Link does not automatically prefetch pages for routes with "has" rewrites

See original GitHub issue

What version of Next.js are you using?

12.0.4

What version of Node.js are you using?

14x

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

A visible Link will automatically prefetch page json for static pages (along with the chunk) when visible. However, if a rewrite is used to add something like a country code to the route, then the Link will no longer prefetch.

Expected Behavior

Links can prefetch routes with or without a rewrite.

To Reproduce

I’ve created a minimal case here: https://github.com/ericmatthys/nextjs-prefetch-with-rewrite It is deployed here: https://nextjs-prefetch-with-rewrite.vercel.app/

If you look at network requests as you browse through the two different paths, you’ll see the “Without Rewrite” path is able to prefetch and navigate through the path quicker. The “With Rewrite” path does not prefetch.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ijjkcommented, Dec 1, 2021

This appears to be due to the rewrite using has when on Vercel but not locally so might be a potential duplicate of https://github.com/vercel/next.js/issues/24798

0reactions
ericmatthyscommented, Dec 1, 2022

It’s best to use middleware for this sort of rewrite now, which avoids the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to prefetch pages built from dynamic routes #10449
When accessing my pages generated from a dynamic URL, it is trying to prefetch .js files based off of their slugs (and 404'ing)...
Read more >
Route prefetching in Next.js - web.dev
Normally, a page is downloaded when you follow a link to it, but Next.js automatically prefetches the JavaScript needed to render the page....
Read more >
Dealing with links in Next.js - LogRocket Blog
In Next 9, automatic prefetching was turned on by default. This prefetches link contents when they are in the page's viewport. Next uses...
Read more >
next/link | Next.js
In order for the <Link /> component to properly prefetch links with rewrites via Middleware, you need to tell Next.js both the URL...
Read more >
Microsoft Edge Browser Policy Documentation
HomepageLocation, Configure the home page URL ... ForceSync, Force synchronization of browser data and do not show the sync consent prompt.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found