Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
cvolant opened this issue Aug 12, 2022 · 5 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template.

Comments

@cvolant
Copy link
Contributor

cvolant commented Aug 12, 2022

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.
@cvolant cvolant added the bug Issue was opened via the bug report template. label Aug 12, 2022
@cvolant
Copy link
Contributor Author

cvolant commented Oct 21, 2022

Up. 🙂

@cvolant
Copy link
Contributor Author

cvolant commented Jan 20, 2023

According to @mlake (here), using Vercel, the issue happens even with a very simple middleware without any matcher...

If I use the following super-simple do-nothing middleware (no matchers or anything), there's no problem running locally (either dev or next build + next start)...BUT if I deploy this same middleware to Vercel, the pageProps are empty and I get client-side exception on my translated routes. frown emoji. sigh emoji.
without any middleware, the translated routes are fine.

// middleware.ts

import {NextRequest, NextResponse} from "next/server"

export function middleware(request: NextRequest) {
    return
}

@ristomatti
Copy link

Any chance PR #45504 would relate to this?

@cvolant
Copy link
Contributor Author

cvolant commented Apr 11, 2023

Yes, you are right, thanks. I cannot reproduce the full bug with canary version.
There is still a bug though : the following redirect also affects prefetches.

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

So /_next/data/weodsfds_someid_dfslfjds/fr/about.json is redirected toward /fr/a-propos, and it should probably not, or should it?

@cvolant cvolant closed this as completed Apr 11, 2023
@github-actions
Copy link
Contributor

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 github-actions bot locked as resolved and limited conversation to collaborators May 12, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants