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 not running in NX monorepo project, causing "Unable to find next-intl locale" error #1171

Closed
3 tasks done
testBP0612 opened this issue Jul 5, 2024 · 9 comments
Closed
3 tasks done
Labels
bug Something isn't working reproduction-missing Stale unconfirmed Needs triage.

Comments

@testBP0612
Copy link

testBP0612 commented Jul 5, 2024

Description

Hi, I'm experiencing an issue with next-intl in an NX monorepo project. When following the official "With i18n router" setup instructions, I consistently encounter the following error:

"Unable to find next-intl locale because the middleware didn't run on this request."

Here are the key points:

  1. This issue only occurs in my NX monorepo project.
  2. When I use the same setup in a standalone Next.js project, everything works as expected.
  3. I've carefully followed all the steps in the "With i18n router" guide.
  4. Except i18n files are placed in the shared folder. like this:
export default getRequestConfig(async ({ locale }) => {
  // Validate that the incoming `locale` parameter is valid
  if (!locales.includes(locale as any)) return notFound();

  return {
    messages: (await import(`../../../packages/assets/src/messages/${locale}.json`)).default,
  };
});

My setup:

  • NX version: 19.3.2
  • Next.js version: 14.2.3
  • next-intl version: 3.15.3

Could this be related to how NX handles Next.js projects or middlewares? Any insights or suggestions would be greatly appreciated. Thank you!

Verifications

  • I've verified that the problem I'm experiencing isn't covered in the docs.
  • I've searched for similar, existing issues on GitHub and Stack Overflow.
  • I've compared my app to a working example to look for differences.

Mandatory reproduction URL

https://github.com/testBP0612/next-intl-nx-bug-repro

Reproduction description

Steps to reproduce:

  1. Create an NX monorepo project
  2. Add a Next.js app to the monorepo
  3. Install and configure next-intl following the "With i18n router" guide
  4. Create a shared library to store i18n files
  5. Run the app and navigate to a page with an unsupported locale

Expected behaviour

The middleware should run, and for unsupported locales, it should execute the notFound function imported from 'next/navigation'.

Actual behavior:
The middleware doesn't seem to run, resulting in the "Unable to find next-intl locale" error. The notFound function is not being called for unsupported locales.

@testBP0612 testBP0612 added bug Something isn't working unconfirmed Needs triage. labels Jul 5, 2024
@atakanyurttutann
Copy link

I have that same problem

@damien-schneider
Copy link

I encountered the same error. It occurs when I use a private window without specifying "/en" (or another language code) in the URL to initialize the variable. If I make the first request with "/en" (or the appropriate language code) in the URL, it works perfectly afterward. (Hope my example will help to find where the error comes from)

Copy link

github-actions bot commented Jul 8, 2024

Thank you for your report!

Unfortunately, the reproduction is missing or incomplete, and as such we cannot investigate this issue. Please add a reproduction to the issue, otherwise it will be closed automatically.

Templates:

Creating a good bug report takes time.

To help us resolve the issue quickly, please simplify the reproduction as much as possible by removing any unnecessary code, files, and dependencies that are not directly related to the problem. The easier it is for us to see the issue, the faster we can help you.

Apart from the reproduction, make sure to include the precise steps on how to reproduce the issue, e.g.:

  1. Open reproduction
  2. Click on …
  3. See error: …

Thank you for your understanding!

FAQ

"I've included a reproduction, what is missing?"

This comment might have been added because your reproduction doesn't point to a destination where the issue can be reproduced. Please make sure that the latest changes were saved in the reproduction and that the link is correct.

@testBP0612
Copy link
Author

I would like to clarify that I have already added a link to a repository that reproduces the bug in my original issue report. The reproduction can be found at here

@amannn
Copy link
Owner

amannn commented Jul 22, 2024

I was unfortunately not able to reproduce the issue in the reproduction you've provided.

These are the steps I took:

  1. Clone the repo
  2. npm i
  3. npx nx run next-app:dev
  4. Open http://localhost:3000
  5. The server redirects to /en
  6. "Hello!" is displayed

I can't see an issue there.

To be able to help with this, can you provide reproduction steps on how the issue can be reproduced in your reproduction repo?

@testBP0612
Copy link
Author

testBP0612 commented Jul 23, 2024

Thank you for taking the time to test the reproduction. I apologize for the confusion. It seems I wasn't clear enough in describing the issue. Let me provide more specific steps to reproduce the problem:

Follow the steps you've already taken (1-4).
Instead of allowing the redirect to /en, try navigating directly to any other path, for example: http://localhost:3000/123

You should then encounter the following error:

Unable to find `next-intl` locale because the middleware didn't run on this request. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The `notFound()` function will be called as a result.

@testBP0612
Copy link
Author

testBP0612 commented Jul 23, 2024

Additionally, I've just discovered that if we remove the <html /> + <body /> tags from app/layout, it causes the 404 page to break. However, if we keep these tags, we encounter an error message for other languages, such as:

Warning: Prop lang did not match. Server: "en" Client: "zh-TW"

How should we address this issue?
You can refer to my other branch: bug/lang to reproduce this problem.

@amannn
Copy link
Owner

amannn commented Jul 23, 2024

Have a look at the error files guidelines in the docs, that should help! I’m on vacation now for a few weeks, hope this helps to resolve your issue. If not, happy to catch up when I’m back!

Copy link

github-actions bot commented Aug 7, 2024

This issue has been automatically closed because it is currently not actionable and has become stale. If the problem persists, please create a new issue.

@github-actions github-actions bot added the Stale label Aug 7, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working reproduction-missing Stale unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

4 participants