next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Middleware + basePath + SSG index page data request returns HTML instead of JSON - page does not receive props.

Open Gilighost opened this issue 2 years ago • 1 comments

Verify canary release

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

Provide environment information

From Vercel: Operating System: Platform: linux Arch: x64 Version: #1 SMP Fri Aug 26 09:54:31 UTC 2022 Binaries: Node: 16.16.0 npm: 8.11.0 Yarn: 1.22.17 pnpm: 6.34.0 Relevant packages: next: 12.3.2-canary.27 eslint-config-next: 12.3.1 14:28:03.384 | react: 18.2.0

What browser are you using? (if relevant)

Chrome 105.0.5195.125

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

Data requests for a root index page using SSG (pages/index.js with getStaticProps) return HTML instead of JSON when middleware is present and a basePath is configured.

image

NOTE: This only happens when deployed to Vercel. When running locally with next dev or next build && next start there is no issue.

Expected Behavior

Data requests should return JSON to properly hydrate the page.

Link to reproduction

https://github.com/Gilighost/nextjs-middleware-issue

To Reproduce

  • Configure a basePath
  • Add middleware (can just return NextResponse.next())
  • Add getStaticProps to pages/index.js
  • Add a next/link with href='/'
  • Deploy to Vercel
  • A hard navigation to the basePath will return the expected HTML
  • Click the link to "/"
    • The page will not receive any props (the data request returns HTML instead of JSON)

You can see an example here: https://my-app-tau-dusky.vercel.app/my-app

  • The first page render is fine, but the page will not receive props if you click the link to "My App"
  • There is a link to another page which shows how non index pages are not affected (click the link to "My Page" and it will receive props as expected)

An example without middleware (works as expected with no middleware present): https://my-app-git-no-middleware-gilighost.vercel.app/my-app

Gilighost avatar Oct 13 '22 20:10 Gilighost

We have the same issue in one of our projects that has basePath configured, we had to downgrade to [email protected] to have a middleware working there. As you said it only happens on Vercel builds.

I made an example as well...

Link for the build: https://next-q74rpy07z-caiomatias.vercel.app/company

Link for the repo: https://github.com/CaioMatias/next-bug

CaioMatias avatar Nov 09 '22 17:11 CaioMatias

Fixed by https://github.com/vercel/vercel/pull/9255

Gilighost avatar Jan 24 '23 16:01 Gilighost

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

github-actions[bot] avatar Feb 24 '23 00:02 github-actions[bot]