1

I've built a Next.js app that uses dynamic routing and I'm noticing some strange behavior with the <Link> prefetches.

Occasionally (usually after rebuilding but not always) my app starts returning 404s for all prefetches on a given page. If I follow a link, the next page loads but again, all the prefetches on that page start to 404. This problem will go away after refreshing the page several times but this is not always consistent.

The breaks seem to happen exclusively on my dynamic routes, using getStaticProps and content revalidation every second. Below is an example of how I've structured my Link, note tripname would be a dynamic value.

<Link href={`/trip/${tripname)}`}>
    <a>
        <Card className={classes.tripCardRoot} />
    </a>
</Link>

This is what happens when the page loads.

enter image description here

Edit:

I spent the day digging into this and have some additional details to better describe what I'm experiencing. This error is definitely related to when I build my app and is only occurring on pages that are being generated after the build process via the fallback:true property in my getStaticPaths() function.

Pages that are explicitly built (e.g. in the paths array) do not seem to exhibit this problem. I'm not sure if this makes sense but it seems as though the first few loads of a given page revert back to a version from the previous build rather than a fallback page. It's only after reloading a page a few times that the fallback functionality actually kicks in and next builds that page as part of the current build. From that point on everything seems to work as expected until the next build where the whole ordeal starts fresh.

Also included a screenshot of the console log indicating that there's an issue in the /_next/data/... directory. Though I'm not sure what this error is actually referring to.

enter image description here

4
  • Which nextjs version are you using?
    – jean182
    Jun 16, 2021 at 22:06
  • Currently running "^10.2.3"
    – LordofX
    Jun 17, 2021 at 17:39
  • Oh sorry I thought you were on an older version, where you need to add this to the props <Link href='/article/[id]' as={/article/${id}}>
    – jean182
    Jun 17, 2021 at 17:58
  • I have come across that solution in a few places but it doesn't seem to be quite what I'm looking for unfortunately, though it did prompt me to dig in a bit more. I've added an edit to add some additional context to the issue.
    – LordofX
    Jun 18, 2021 at 1:28

1 Answer 1

0

I had the exact same problem... After 2 days trial/error I finally found a solution. But it's not the best solution IMO.

I updated all my <Link /> components from next/link and disabled the prefetch option. After this change I don't see any 404 errors in my dev-tools network-tab anymore. All GET-requests for the pre-generated JSON data will only take place on hover now, and magically they now don't result in a 404...

I know it is not the best solution, but at least the problem is gone... Hopefully somebody will reply with a better solution!

1
  • 1
    This reportedly has been fixed in the latest canary build of nextjs - v11.1.3-canary.14 - but the next stable release isn't for a few weeks: github.com/vercel/next.js/issues/27783. Haven't tried it myself yet, but thinking about disabling prefetching in the meantime because I don't want to ship a canary nextjs dependency into prod.
    – mattyb
    Sep 24, 2021 at 20:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.