You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to setup next-intl with multi-tenancy like user123.domain.com/en , I tried so far but no result
withTenant.ts
exportfunctionwithTenant(middleware: CustomMiddleware){returnasync(req: NextRequest,event: NextFetchEvent,response: NextResponse)=>{consturl=req.nextUrl.clone();consthost=req.headers.get("host");constsubdomain=getValidSubdomain(host!);// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)lethostname=req.headers.get("host")!.replace(".localhost:3000",`.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);// special case for Vercel preview deployment URLsif(hostname.includes("---")&&hostname.endsWith(`.${process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX}`)){hostname=`${hostname.split("---")[0]}.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`;}constsearchParams=req.nextUrl.searchParams.toString();// Get the pathname of the request (e.g. /, /about, /blog/first-post)constpath=`${url.pathname}${searchParams.length>0 ? `?${searchParams}` : ""}`;// rewrites for app pagesif(hostname==`app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`){constsession=awaitgetToken({ req });if(!session&&path!==PATHNAMES.LOGIN){returnNextResponse.redirect(newURL(PATHNAMES.LOGIN,req.url));}elseif(session&&path==PATHNAMES.LOGIN){returnNextResponse.redirect(newURL("/",req.url));}returnNextResponse.rewrite(newURL(`/app${path==="/" ? "" : path}`,req.url));}// rewrite everything else to `/[domain]/[slug] dynamic routeurl.pathname=`/${subdomain}${url.pathname}`;returnNextResponse.rewrite(url);};}
import{chain}from"./middlewares/chain";exportdefaultchain([withInitResponse,withAuth,withNextIntl,withTenant]);exportconstconfig={// Skip all paths that should not be internationalizedmatcher: ["/((?!api|_next|.*\\..*).*)"],};
withNextIntl.ts
exportfunctionwithNextIntl(middleware: CustomMiddleware){returnasync(request: NextRequest,event: NextFetchEvent)=>{consthandleI18nRouting=createMiddleware<any>({locales: LOCALES,defaultLocale: getPreferredLocale(request),localePrefix: "always",localeDetection: true,});constresponse=handleI18nRouting(request);// Call the next middleware and pass the request and responsereturnmiddleware(request,event,response);};}exportfunctiongetPreferredLocale(request: NextRequest){// cookieconstlocaleCookie=request.cookies.get("NEXT_LOCALE")?.value;if(localeCookie&&LOCALES.includes(localeCookie)){returnlocaleCookie;}//Accept-LanguageconstacceptLanguage=request.headers.get("Accept-Language");if(acceptLanguage){constpreferredLocale=acceptLanguage.split(",")[0].split("-")[0];if(LOCALES.includes(preferredLocale)){returnpreferredLocale;}}return"en";}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to setup
next-intl
with multi-tenancy likeuser123.domain.com/en
, I tried so far but no resultwithTenant.ts
chain.ts
middleware.ts
withNextIntl.ts
folder structure
Beta Was this translation helpful? Give feedback.
All reactions