fix wrong homepage locale url match
This commit is contained in:
parent
c4403bfe35
commit
70f4cc13ac
1 changed files with 15 additions and 9 deletions
|
|
@ -48,18 +48,24 @@ module.exports = function (eleventyConfig, { TIME_ZONE, defaultLanguage }) {
|
|||
// determine locale to resolve to
|
||||
const pageLang = desiredLocale || (ctx.page && ctx.page.lang) || ctx.locale || defaultLanguage;
|
||||
|
||||
const LOCALE_URL_DEBUG = process.env.LOCALE_URL_DEBUG === "1" || process.env.LOCALE_URL_DEBUG === "true";
|
||||
if (LOCALE_URL_DEBUG) {
|
||||
console.warn(`[locale_url_resolve] resolving targetUrl="${targetUrl}" desiredLocale="${desiredLocale}" pageLang="${pageLang}"`);
|
||||
}
|
||||
|
||||
// if requested locale is default, return the raw url
|
||||
if (pageLang === defaultLanguage) {
|
||||
if (LOCALE_URL_DEBUG) console.warn(`[locale_url_resolve] requested locale is default (${defaultLanguage}) — returning raw URL "${targetUrl}"`);
|
||||
return targetUrl;
|
||||
// special case for homepage
|
||||
if (targetUrl === "/" || targetUrl === "/index/") {
|
||||
if (pageLang === defaultLanguage) {
|
||||
if (LOCALE_URL_DEBUG) console.warn(`[locale_url_resolve] homepage, default language (${defaultLanguage}) -> "/"`);
|
||||
return "/";
|
||||
}
|
||||
const homepageUrl = `/${pageLang}/`;
|
||||
if (LOCALE_URL_DEBUG) console.warn(`[locale_url_resolve] homepage, language (${pageLang}) -> "${homepageUrl}"`);
|
||||
return homepageUrl;
|
||||
}
|
||||
|
||||
// normalize targetUrl to ensure trailing slash for comparison
|
||||
const normUrl = targetUrl.endsWith("/") ? targetUrl : (targetUrl + "/");
|
||||
// normalize targetUrl to include trailing slash for comparison
|
||||
const normUrl = targetUrl.endsWith("/") ? targetUrl : `${targetUrl}/`;
|
||||
|
||||
// try to find the canonical (default language) page corresponding to targetUrl
|
||||
let canonical = all.find(p => {
|
||||
|
|
@ -83,7 +89,7 @@ module.exports = function (eleventyConfig, { TIME_ZONE, defaultLanguage }) {
|
|||
|
||||
// if cannot find canonical page, fall back to a prefixed URL (best effort)
|
||||
if (!canonical) {
|
||||
const fallback = (`/${pageLang}${targetUrl}`).replace(/\/{2,}/g, "/");
|
||||
const fallback = `/${pageLang}${targetUrl}`.replace(/\/{2,}/g, "/");
|
||||
if (LOCALE_URL_DEBUG) console.warn(`[locale_url_resolve] fallback -> "${fallback}"`);
|
||||
return fallback;
|
||||
}
|
||||
|
|
@ -117,8 +123,8 @@ module.exports = function (eleventyConfig, { TIME_ZONE, defaultLanguage }) {
|
|||
return localized.url;
|
||||
}
|
||||
|
||||
// no localized page found — fall back to prefixed path
|
||||
const fallback2 = (`/${pageLang}${targetUrl}`).replace(/\/{2,}/g, "/");
|
||||
// fallback to prefixed URL
|
||||
const fallback2 = `/${pageLang}${targetUrl}`.replace(/\/{2,}/g, "/");
|
||||
if (LOCALE_URL_DEBUG) console.warn(`[locale_url_resolve] localized NOT found for key="${key}" — fallback -> "${fallback2}"`);
|
||||
return fallback2;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue