vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.06k stars 27k forks source link

Rewrites don't work if a middleware.ts file is present #51923

Open Reinano opened 1 year ago

Reinano commented 1 year ago

Verify canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:24 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.19.1
      npm: 8.19.3
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.6
      eslint-config-next: 13.4.6
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

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

Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/Reinano/custom-next

To Reproduce

1.next build && next start 2.Go to main page then click any link (about-1 or about-2). 3.Check router.query.slug

  1. U'll be able to see empty router.query (useRouter)

Describe the Bug

I use rewrites and in the case when there is a middleware.ts file in the root of the project, then rewrites with a dynamic slug stop working. I have rewrites like this: const nextConfig = { reactStrictMode: true, async rewrites() { return [ { source: '/es-es/(quenes-somos)/:slug', destination: '/about/:slug', locale: false, }, ] } } If i go to production build and start it. Next dev is ok. After following the link <Link href='/es-es/quenes-somos/about-1'>about-1 </Link> from the main page to the about/[slug] dynamic page, I won't get router.query.slug. If u reload the page u can get router.query.slug Router.query.slug available from getServerSideProps, but not in client side.

Expected Behavior

Would like to have access to router.query.slug from useRouter()

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

paulvanbrenk commented 1 year ago

The problem is the locales, probably the same as this. It looks like Vercel doesn't care about i18n

https://github.com/vercel/next.js/issues/49883

JesseKoldewijn commented 1 year ago

Agreed, there seemed to be an issue with localization in general when combining config rewrites with middleware as far as I've last noticed. But don't quote me on that

samik3k commented 1 year ago

Btw, it's working fine in development, but fails in production build :(