From 7729a01b020d63a0a09497c8de7008bcb38bfdcb Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Thu, 21 Dec 2023 10:35:28 +0100 Subject: [PATCH] docs: Add note to rewrite example about `localePrefix` (closes #737) --- docs/pages/docs/routing/middleware.mdx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/pages/docs/routing/middleware.mdx b/docs/pages/docs/routing/middleware.mdx index a4e77a5ea..7a29452b0 100644 --- a/docs/pages/docs/routing/middleware.mdx +++ b/docs/pages/docs/routing/middleware.mdx @@ -441,9 +441,9 @@ import createIntlMiddleware from 'next-intl/middleware'; import {NextRequest} from 'next/server'; export default async function middleware(request: NextRequest) { - const [, locale, pathname] = request.nextUrl.pathname.split('/'); + const [, locale, ...segments] = request.nextUrl.pathname.split('/'); - if (pathname === 'profile') { + if (locale != null && segments.join('/') === 'profile') { const usesNewProfile = (request.cookies.get('NEW_PROFILE')?.value || 'false') === 'true'; @@ -465,6 +465,8 @@ export const config = { }; ``` +Note that if you use a [`localePrefix`](#locale-prefix) other than `always`, you need to adapt the handling appropriately to handle unprefixed pathnames too. + ### Example: Integrating with Clerk [`@clerk/nextjs`](https://clerk.com/docs/references/nextjs/overview) provides a middleware that can be integrated with `next-intl` by using the [`beforeAuth` hook](https://clerk.com/docs/references/nextjs/auth-middleware#using-before-auth-to-execute-middleware-before-authentication). By doing this, the middleware from `next-intl` will run first, potentially redirect or rewrite incoming requests, followed by the middleware from `@clerk/next` acting on the response.