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

Catch-all rewrites resolve before appDir pages in production builds

Open KurtGokhan opened this issue 2 years ago • 0 comments

Verify canary release

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

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
Binaries:
  Node: 18.12.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 13.0.3
  eslint-config-next: 13.0.3
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

next build && next start

Describe the Bug

I have noticed a strange behavior involving rewrites and appDir. I have a rewrites config like this:

  async rewrites() {
    return {
      fallback: [
        {
          source: '/:path*',
          destination: `${process.env.BACKEND_PROXY}/:path*`,
          basePath: false,
        },
      ],
    };
  }

And I have a page in /login path in app directory. In development, when I visit the /login path, I see my page as expected. In production however, it rewrites to the backed proxy so I cannot see my page. If I remove rewrites rule, I can see the page correctly. This issue also does not happen when page is written in pages directory instead of app directory.

The issue may be caused by next build generating a wrong manifest file. Ì have noticed the .next/server/app-paths-manifest.json file looks like this after build:

{
  "/login/page": "app/login/page.js"
}

Manually changing it to look like this resolves the issue:

{
  "/login": "app/login/page.js"
}

By the way, visiting /login/page causes a 500 error with this stack trace:

Error: Minified React error #130; visit https://reactjs.org/docs/error-decoder.html?invariant=130&args[]=object&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
    at ae (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:103:176)
    at Z (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:104:91)
    at Zd (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:97:314)
    at ae (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:98:207)
    at Z (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:104:91)
    at ae (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:99:9)
    at Z (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:104:91)
    at be (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:106:98)
    at de (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:105:424)
    at Z (node_modules\next\dist\compiled\react-dom\cjs\react-dom-server.browser.production.min.js:104:222)

Expected Behavior

Router should check existing pages before rewriting the path as described here

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://stackblitz.com/edit/vercel-next-js-hfhqep?file=next.config.js

To Reproduce

  • Run yarn build && yarn start
  • Visit /login and observe

KurtGokhan avatar Nov 13 '22 12:11 KurtGokhan