未验证 提交 fcd6d434 编写于 作者: P Peter Pan 提交者: GitHub

fix: incorrect locale redirect (#780)

上级 b7220a01
......@@ -17,9 +17,9 @@ const route = pathMatch();
export default function (nexti18next: NextI18Next) {
const {config, i18n} = nexti18next;
const {allLanguages, ignoreRoutes, localeSubpaths} = config;
const {allLanguages, ignoreRoutes, localeSubpaths, publicPath} = config;
const isI18nRoute = (req: Request) => ignoreRoutes?.every(x => !req.url.startsWith(x));
const isI18nRoute = (req: Request) => ignoreRoutes?.every(x => !req.url.startsWith((publicPath || '') + x));
const localeSubpathRoute = route(`/:subpath(${Object.values(localeSubpaths || {}).join('|')})(.*)`);
const middleware = [];
......@@ -48,13 +48,17 @@ export default function (nexti18next: NextI18Next) {
*/
middleware.push((req: Request, res: Response, next: NextFunction) => {
if (isI18nRoute(req) && req.i18n) {
let url = publicPath ? req.url.replace(publicPath, '') : req.url;
if (!url) {
url = '/';
}
let currentLng = lngFromReq(req);
const currentLngSubpath = subpathFromLng(config, currentLng);
const currentLngRequiresSubpath = subpathIsRequired(config, currentLng || '');
const currentLngSubpathIsPresent = subpathIsPresent(req.url, currentLngSubpath);
const currentLngSubpathIsPresent = subpathIsPresent(url, currentLngSubpath);
const lngFromCurrentSubpath = allLanguages.find((l: string) =>
subpathIsPresent(req.url, subpathFromLng(config, l))
subpathIsPresent(url, subpathFromLng(config, l))
);
if (lngFromCurrentSubpath !== undefined && lngFromCurrentSubpath !== currentLng) {
......@@ -70,7 +74,7 @@ export default function (nexti18next: NextI18Next) {
If a language subpath is required and
not present, prepend correct subpath
*/
return redirectWithoutCache(res, addSubpath(req.url, currentLngSubpath));
return redirectWithoutCache(res, (publicPath || '') + addSubpath(url, currentLngSubpath));
}
/*
......@@ -79,11 +83,11 @@ export default function (nexti18next: NextI18Next) {
render the correct route
*/
if (typeof lngFromCurrentSubpath === 'string') {
const params = localeSubpathRoute(req.url);
const params = localeSubpathRoute(url);
if (params !== false) {
const {subpath} = params;
req.query = {...req.query, subpath, lng: currentLng};
req.url = removeSubpath(req.url, subpath);
req.url = (publicPath || '') + removeSubpath(url, subpath);
}
}
}
......
......@@ -7,7 +7,7 @@ import {Config} from '../../types';
const parseAs = (originalAs: string | undefined, href: any) => {
const asType = typeof originalAs;
let as;
let as: string;
if (asType === 'undefined') {
as = formatUrl(href, {unicode: true});
......@@ -37,7 +37,7 @@ const parseHref = (originalHref: any) => {
};
export const lngPathCorrector = (config: Config, currentRoute: any, currentLanguage: string) => {
const {allLanguages, localeSubpaths} = config;
const {allLanguages, localeSubpaths, publicPath} = config;
const {as: originalAs, href: originalHref} = currentRoute;
if (!allLanguages.includes(currentLanguage)) {
......@@ -46,6 +46,11 @@ export const lngPathCorrector = (config: Config, currentRoute: any, currentLangu
const href = parseHref(originalHref);
let as = parseAs(originalAs, href);
if (publicPath) {
const url = parseUrl(as);
url.pathname = url.pathname.replace(publicPath, '');
as = formatUrl(url);
}
/*
url.format prefers the 'url.search' string over
......@@ -58,8 +63,8 @@ export const lngPathCorrector = (config: Config, currentRoute: any, currentLangu
Strip any/all subpaths from the `as` value
*/
Object.values(localeSubpaths || {}).forEach((subpath: string) => {
if (subpathIsPresent(as as string, subpath)) {
as = removeSubpath(as as string, subpath);
if (subpathIsPresent(as, subpath)) {
as = removeSubpath(as, subpath);
}
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册