2022-05-24 00:36:58 +03:00
|
|
|
import { cacheControlFactory } from './cache-control.js'
|
2022-08-10 18:28:21 +03:00
|
|
|
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()
|
|
|
|
}
|