Comments (3)
Hey, since you use localePrefix: 'as-necessary'
you need to adapt the handling appropriately to handle unprefixed pathnames too.
This should work:
import createMiddleware from 'next-intl/middleware';
import { locales } from './navigation';
import { NextRequest } from 'next/server';
// Update to simulate authenticated user
const isAuthenticated = false;
export default async function middleware(request: NextRequest) {
const [, localeOrFirstSegment, ...remainingSegments] =
request.nextUrl.pathname.split('/');
const hasLocalePrefix = locales.includes(localeOrFirstSegment as any);
const localePrefix = hasLocalePrefix ? localeOrFirstSegment : undefined;
const pathname = hasLocalePrefix
? remainingSegments.join('/') || '/'
: request.nextUrl.pathname;
if (pathname === '/' && isAuthenticated) {
request.nextUrl.pathname = '/dashboard';
if (hasLocalePrefix) {
request.nextUrl.pathname = `/${localePrefix}` + request.nextUrl.pathname;
}
}
const handleI18nRouting = createMiddleware({
defaultLocale: 'en',
locales,
localePrefix: 'as-needed'
});
const response = handleI18nRouting(request);
return response;
}
export const config = {
matcher: [
// Match all pathnames except for
// - … if they start with `/api`, `/_next` or `/_vercel`
// - … the ones containing a dot (e.g. `favicon.ico`)
'/((?!api|_next|_vercel|.*\\..*).*)',
// However, match all pathnames within `/users`, optionally with a locale prefix
'/([\\w-]+)?/users/(.+)'
]
};
I've also added a note about this to the rewrite example in the docs.
Hope this helps!
from next-intl.
Thanks @amannn,
It works very well like that! One last question, how can I make redirects instead of rewrites?
from next-intl.
Great to hear!
how can I make redirects instead of rewrites?
You can early-return a NextResponse.redirect(…)
if a certain condition is met. In this case you don't need to run the next-intl
middleware!
from next-intl.
Related Issues (20)
- [Docs]: How to setup Next to handle paths like /news/[articleSlug]-[articleId] ? HOT 1
- Error "page not found" in the middleware when accessing the API HOT 1
- numbers are translated when using .rich HOT 1
- useLocale() disables fetch tags cache ( next tags ) HOT 1
- Is it possible to read the value of another namespace in an existing namespace? HOT 1
- Locale Detection: Option to change order HOT 1
- [Docs]: Middleware documentation is too light HOT 1
- Unable to use with RSC when mode is async HOT 3
- Localized navigation doesn't work with too many pathnames (over 25) HOT 2
- false NEXT_LOCALE cookie value when entring route directly HOT 3
- Issue with multiple middleware chain HOT 1
- ESLint plugin HOT 2
- Handling Unsupported Languages HOT 1
- Support `localePrefix` setting per domain HOT 2
- Support Intl.RelativeTimeFormat for `relativeTime` formatting HOT 3
- Docs Invisible Text on Search HOT 1
- Package path ./server.js is not exported from package HOT 2
- Error using `as={Link}` prop with when using `createSharedPathnamesNavigation` HOT 1
- Accidentally changed the locale after navigating when setting "localePrefix" to "never" HOT 2
- I cannot understand how to merge next-intl middleware with nextjs/platform HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-intl.