-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Set default locale without locale prefix in pathname (#109)
- Loading branch information
1 parent
0e773c5
commit a91e041
Showing
9 changed files
with
797 additions
and
663 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,66 @@ | ||
import { type NextFetchEvent, type NextRequest } from "next/server"; | ||
import { match } from "@formatjs/intl-localematcher"; | ||
import Negotiator from "negotiator"; | ||
import type { NextFetchEvent, NextRequest } from "next/server"; | ||
import createIntlMiddleware from "next-intl/middleware"; | ||
|
||
import { COOKIE_KEY } from "@/config"; | ||
import { routing } from "@/i18n/routing"; | ||
import { LOCALE_CHANNEL_MAP } from "@/regions/config"; | ||
import { type Locale } from "@/regions/types"; | ||
import { localePrefixes, routing } from "@/i18n/routing"; | ||
import { | ||
DEFAULT_LOCALE, | ||
type Locale, | ||
SUPPORTED_LOCALES, | ||
} from "@/regions/types"; | ||
|
||
import type { CustomMiddleware } from "./chain"; | ||
|
||
const NEXT_LOCALE = "NEXT_LOCALE"; | ||
|
||
function getLocale(request: NextRequest) { | ||
const languages = new Negotiator({ | ||
headers: { | ||
"accept-language": request.headers.get("accept-language") ?? undefined, | ||
}, | ||
}).languages(); | ||
|
||
return match(languages, SUPPORTED_LOCALES, DEFAULT_LOCALE); | ||
} | ||
|
||
export function i18nMiddleware(middleware: CustomMiddleware) { | ||
return async (request: NextRequest, event: NextFetchEvent) => { | ||
const nextLocaleCookie = request.cookies.get(NEXT_LOCALE)?.value ?? "en-GB"; | ||
const channelId = request.nextUrl.pathname.split("/")[1]; | ||
const locale = | ||
Object.keys(LOCALE_CHANNEL_MAP).find( | ||
(key) => LOCALE_CHANNEL_MAP[key as Locale] === channelId, | ||
) ?? nextLocaleCookie; | ||
const prefferedLocale = getLocale(request); | ||
const nextLocaleCookie = request.cookies.get(NEXT_LOCALE)?.value; | ||
const pathname = request.nextUrl.pathname; | ||
const localePrefix = Object.values(localePrefixes).find( | ||
(localePrefix) => | ||
pathname.startsWith(localePrefix) || pathname === localePrefix, | ||
); | ||
const isLocalePrefixedPathname = !!localePrefix; | ||
|
||
if (locale !== nextLocaleCookie) { | ||
request.cookies.delete(COOKIE_KEY.checkoutId); | ||
} | ||
let locale = prefferedLocale; | ||
|
||
const handleI18nRouting = createIntlMiddleware(routing); | ||
|
||
const response = handleI18nRouting(request); | ||
|
||
if (locale !== nextLocaleCookie) { | ||
response.cookies.delete(COOKIE_KEY.checkoutId); | ||
// INFO: All routes have locale prefixes except for default locale/domain - "/". | ||
// If the user types only domain name it should be navigated to preffered region of the store, | ||
// otherwise navigate to the requested locale prefixed pathname | ||
if (isLocalePrefixedPathname) { | ||
locale = | ||
Object.keys(localePrefixes).find( | ||
(key) => | ||
localePrefixes[key as Exclude<Locale, typeof DEFAULT_LOCALE>] === | ||
localePrefix, | ||
) ?? DEFAULT_LOCALE; | ||
} | ||
|
||
// INFO: Store the locale in the cookie to know if the locale has changed between requests | ||
response.cookies.set(NEXT_LOCALE, locale); | ||
|
||
if (locale !== nextLocaleCookie) { | ||
request.cookies.delete(COOKIE_KEY.checkoutId); | ||
response.cookies.delete(COOKIE_KEY.checkoutId); | ||
} | ||
|
||
return middleware(request, event, response); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.