From bd7d82b5503de8eaff5783da7f52e6df734629fc Mon Sep 17 00:00:00 2001 From: Rachael Sewell Date: Fri, 5 Nov 2021 09:43:47 -0700 Subject: [PATCH] fallback to English page when translation unavailable (#22638) --- middleware/find-page.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/middleware/find-page.js b/middleware/find-page.js index f992672256..2830a7863a 100644 --- a/middleware/find-page.js +++ b/middleware/find-page.js @@ -3,11 +3,31 @@ export default async function findPage(req, res, next) { let page = req.context.pages[req.pagePath] - // if this is a localized request that can't be found, try finding an English variant + // When a user navigates to a translated page that doesn't yet exists + // we want to first check if there is an English page with the same relative + // path. + // If an exact match in English doesn't exist, the requested page might have + // a redirect configured to a new page. This happens when an English page is + // renamed and Crowdin hasn't synced the new file. + // In both cases, redirect to the English page. If we don't redirect most + // components won't refresh and everything except the article will render + // in req.language. if (!page && req.language !== 'en') { const englishPath = req.pagePath.replace(new RegExp(`^/${req.language}`), '/en') // NOTE the fallback page will have page.languageCode = 'en' page = req.context.pages[englishPath] + const redirectToPath = req.context.redirects[englishPath] + + // If the requested translated page has a 1-1 mapping in English, + // redirect to that English page + if (page) { + return res.redirect(301, englishPath) + } + // If the English file was renamed and has a redirect that matches the + // requested page's href, redirect to the new English href + if (redirectToPath) { + return res.redirect(301, redirectToPath) + } } if (page) {