Git Product home page Git Product logo

Comments (3)

amannn avatar amannn commented on July 26, 2024 1

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.

flux0uz avatar flux0uz commented on July 26, 2024

Thanks @amannn,
It works very well like that! One last question, how can I make redirects instead of rewrites?

from next-intl.

amannn avatar amannn commented on July 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.