amannn/next-intl

I cannot understand how to merge next-intl middleware with nextjs/platform

timbit123 opened this issue · 3 comments

Description

Hello,

I'm working on a project base from https://github.com/vercel/platforms where you can host multiple website using the domain as a dynamic value.

so when I'm on a public website we do a url rewrite like this in the middleware,ts:L72

return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url));

I'm trying to combine this with intlMiddleware but I cannot get it to work.
Any help would be really appreciated.
Thank you!

Verifications

  • I've verified that the problem I'm experiencing isn't covered in the docs.
  • I've searched for similar, existing issues on GitHub and Stack Overflow.
  • I've compared my app to a working example to look for differences.

Mandatory reproduction URL

https://github.com/vercel/platforms

Reproduction description

Steps to reproduce:

  1. clone the project
  2. run the project with a domain
  3. Be able to have locale with domain (which is dynamic based on the subdomain or other)

Expected behaviour

I would like to be able to do a rewrite url to look like this

  return NextResponse.rewrite(new URL(`/${locale}/${hostname}${path}`, req.url));

I came up with this. it would be great if someone could give his opinion.

export default async function middleware(req: NextRequest) {
  // ...
  const [, locale, ...segments] = req.nextUrl.pathname.split("/");
  let intlResponse = intlMiddleware(req);
  if (!locales.includes(locale)) {
    return intlResponse;
  }
  return NextResponse.rewrite(new URL(`/${locale}/${domain}/${segments}`, req.url), intlResponse);
}

This is discussed in #653, please have a look there for related discussions and options you have.

I also made a note about your use case in #653, thanks for including details about your use case!