amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.49k stars 226 forks source link

Error "page not found" in the middleware when accessing the API #1011

Closed dixakman closed 6 months ago

dixakman commented 6 months ago

Description

Error "page not found" in the middleware when accessing the API

Code middleware: ` export default async function middleware(request: NextRequest) {

console.log("OK")

const handleI18nRouting = createIntlMiddleware({
    locales,
    defaultLocale,
    pathnames,
    localePrefix
});
const response = handleI18nRouting(request);

response.headers.set('x-your-custom-locale', defaultLocale);

return response;

} `

Show "OK", but the error is "Page not found"

blocks the api, but allows a separate path Config middleware: ` export const config = { matcher: [ // Enable a redirect to a matching locale at the root '/',

    // Set a cookie to remember the previous locale for
    // all requests that have a locale prefix
    '/(ru|en)/:path*',

    // Enable redirects that add missing locales
    // (e.g. `/pathnames` -> `/en/pathnames`)
    '/((?!api|_next|_vercel|.*\\..*).*)',

    '/api/chat/all'
]

}; `

Verifications

Mandatory reproduction URL

https://github.com/amannn/next-intl-bug-repro-app-router

Reproduction description

Steps to reproduce:

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

Expected behaviour

show response from api

amannn commented 6 months ago

Closing, since no reproduction was provided.

As you've also pointed out, your matcher likely has an issue—this is controlled in your user code and not by next-intl. This might be relevant for you: https://next-intl-docs.vercel.app/docs/routing/middleware#matcher-config