diff --git a/frontend/packages/i18n/src/middlewares/next-i18next-middleware.ts b/frontend/packages/i18n/src/middlewares/next-i18next-middleware.ts index 132600dac14e18061654bd84355c02f0071f723f..3e9853dd7641fd82474338ff4ac88331cdb8a733 100644 --- a/frontend/packages/i18n/src/middlewares/next-i18next-middleware.ts +++ b/frontend/packages/i18n/src/middlewares/next-i18next-middleware.ts @@ -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); } } } diff --git a/frontend/packages/i18n/src/utils/lng-path-corrector.ts b/frontend/packages/i18n/src/utils/lng-path-corrector.ts index 0c5507a1e54041ba68421574c669722b857f699b..293cd5f034583f3a98bdaf35389e4d97e0439764 100644 --- a/frontend/packages/i18n/src/utils/lng-path-corrector.ts +++ b/frontend/packages/i18n/src/utils/lng-path-corrector.ts @@ -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); } });