next.js: Client side navigation not working when middleware is enable for SSG

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
    Binaries:
      Node: 16.14.0
      npm: 8.3.1
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.2.4
      eslint-config-next: 13.2.4
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

Data fetching (gS(S)P, getInitialProps), Internationalization (i18n), Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue

https://github.com/AdilAmanat/documentation-starter-kit

To Reproduce

Prefetch and url using next/link. It’s only happening on specific GitHub repo, not with all repo.

Note: it’s working fine in above mentioned repo this is just to by pass validation.

Describe the Bug

Client-side navigation and prefetch not working when we have a middleware file. After upgrading to next 13 we have started getting 404 on client-side navigation on _next/data url. I tried reproducing it on new app but it was working on new environment. This is only happening in vercel deployment. On the local environment, it’s working fine.

Screenshot 2023-03-14 at 3 12 30 PM

If you look at the above screenshot it’s generating 404 on prefetch for _next/data request. I tried debuggin this issue and found out this request is generated from this line. This is because this is because when next do the initial request with middleware from this line it recive a html response not json and then it generates wrong url and eventually end up with 404.

When I remove the middleware file it starts working as expected.

Expected Behavior

middleware shouldn’t impact the behaviour we already have with previous next versions.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 31 (24 by maintainers)

Most upvoted comments

Yes, since this doesn’t seem to correspond to this issue it should be in a separate one. 👍

@JesseKoldewijn sorry about that I was actually using 13.2.4-canary which is no more canary now.