more efficient language redirectPatterns middleware (#23412)
* more efficient language redirectPatterns middleware Part of #1322 * feedbacked
This commit is contained in:
@@ -1,18 +1,37 @@
|
||||
import languages from '../../lib/languages.js'
|
||||
|
||||
const redirectPatterns = Object.values(languages)
|
||||
.map((language) => language.redirectPatterns || [])
|
||||
.flat()
|
||||
// If the enabled languages have `.redirectPatterns`, combine them
|
||||
// into one which we only need to use to determine if we should bother
|
||||
// doing the redirect at all.
|
||||
const combinedRedirectPatternRegex =
|
||||
redirectPatterns.length > 0
|
||||
? new RegExp(redirectPatterns.map((rex) => rex.source).join('|'))
|
||||
: null
|
||||
|
||||
// This middleware handles redirects for mistyped language codes
|
||||
//
|
||||
// Examples:
|
||||
// /jp* -> /ja*
|
||||
// /zh-TW* -> /cn*
|
||||
export default function languageCodeRedirects(req, res, next) {
|
||||
for (const code in languages) {
|
||||
const language = languages[code]
|
||||
const redirectPatterns = language.redirectPatterns || []
|
||||
for (const i in redirectPatterns) {
|
||||
const redirectPattern = redirectPatterns[i]
|
||||
if (redirectPattern.test(req.path)) {
|
||||
return res.redirect(301, req.path.replace(redirectPattern, `/${language.code}`))
|
||||
// Only in the unlikely event that the `req.path` starts with one of these
|
||||
// prefixes do we bother looking up what the redirect should be.
|
||||
if (
|
||||
!req.path.startsWith('/_next/static') &&
|
||||
combinedRedirectPatternRegex &&
|
||||
combinedRedirectPatternRegex.test(req.path)
|
||||
) {
|
||||
// This loop is almost never ever used to it doesn't have to be
|
||||
// particularly smart or fast.
|
||||
for (const language of Object.values(languages)) {
|
||||
const redirectPatterns = language.redirectPatterns || []
|
||||
for (const redirectPattern of redirectPatterns) {
|
||||
if (redirectPattern.test(req.path)) {
|
||||
return res.redirect(301, req.path.replace(redirectPattern, `/${language.code}`))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user