docs/middleware/fast-root-redirect.js

17 строки
602 B
JavaScript
Исходник Обычный вид История

2022-05-24 00:36:58 +03:00
import { cacheControlFactory } from './cache-control.js'
import { getLanguageCodeFromHeader } from './detect-language.js'
import { PREFERRED_LOCALE_COOKIE_NAME } from '../lib/constants.js'
2022-05-24 00:36:58 +03:00
const cacheControl = cacheControlFactory(0)
export default function fastRootRedirect(req, res, next) {
if (!req.headers.cookie || !req.headers.cookie.includes(PREFERRED_LOCALE_COOKIE_NAME)) {
// No preferred language cookie header!
const language = getLanguageCodeFromHeader(req) || 'en'
cacheControl(res)
res.set('location', `/${language}`)
return res.status(302).send('')
}
next()
}