Skip to content

middleware (stable version) matcher conflict with some rewrites/redirects #39531

@cvolant

Description

@cvolant

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #49-Ubuntu SMP Thu Aug 4 18:03:25 UTC 2022
Binaries:
  Node: 14.17.0
  npm: 6.14.13
  Yarn: 1.22.17
  pnpm: N/A
Relevant packages:
  next: 12.2.5
  eslint-config-next: N/A
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

Firefox Developer Edition 98.0b10 - Firefox 97.0.2 - Chromium 99.0.4844.51

How are you deploying your application? (if relevant)

yarn dev OR yarn build && yarn start

Describe the Bug

Here is my next config:

{
  i18n: {
    locales: ['en', 'fr'],
    defaultLocale: 'en',
  },

  async redirects() {
    return [    
      {
        source: '/fr/about',
        destination: '/fr/a-propos',
        locale: false,
        permanent: false,
      },
    ]
  },
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/a-propos',
          destination: '/about',
        },
      ],
    }
  },
}

Without any middleware, or with a middleware without matcher config, everything works as exected.

But if I add this config, { matcher: ['/about'] } to the middleware, then the /fr/a-propos page is broken on CSR: the props from getStaticProps are not received by the page because there is a wrong redirection, from /_next/data/development/fr/about.json to /fr/a-propos.

Expected Behavior

The middleware should not conflict with the rewrites/redirects.

Link to reproduction

Repo Github
Repo on CodeSandbox
CodeSandbox deployment

To Reproduce

  1. Open the provided reproduction
  2. Install the dependencies: yarn install
  3. Run with either yarn dev or yarn build && yarn start
  4. Visit http://localhost:3000/
  5. Follow the guided visit, or...
  6. Open the network tab of your browser
  7. Click the Visit /fr/a-propos link
  8. See that the props from getStaticProps are empty
  9. See that there is a weird redirection from /_next/data/development/fr/about.json to /fr/a-propos.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions