Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import { NextRequest, NextResponse } from 'next/server'
import { getLogger } from './logging/log-util'
//regex to check if there's an extension in the path, ie .jpg
const PUBLIC_FILE = /\.(.*)$/
const logger = getLogger('middleware')
export async function middleware(req: NextRequest) {
const { cookies, nextUrl, url } = req
const { locale, pathname } = nextUrl
if (
pathname.startsWith('/_next') ||
pathname.includes('/api/') ||
PUBLIC_FILE.test(pathname)
) {
return NextResponse.next()
}
logger.debug(req)
if (locale === 'default' && !pathname.endsWith('/')) {
return NextResponse.redirect(new URL(`/en${pathname}`, url))
}
//Redirect for index page as it's meant to be bilingual so we don't want users navigating to /en or /fr
if ((locale === 'en' || locale === 'fr') && pathname === '/') {
return NextResponse.redirect(new URL(`/`, url))
}
if (
!['/', '/expectations'].includes(pathname) &&
cookies.get('agreed-to-email-esrf-terms')?.value !== 'true'
) {
return NextResponse.redirect(new URL(`/${locale}/expectations`, url))
}
return NextResponse.next()
}
|