-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathproxy.js
More file actions
37 lines (34 loc) · 1011 Bytes
/
proxy.js
File metadata and controls
37 lines (34 loc) · 1011 Bytes
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
import { NextResponse } from 'next/server'
export const config = {
matcher: ['/m/:path*', '/measurement/:path*'],
has: [
{ type: 'header', key: 'enable-embedded-view' },
// { type: 'header', key: 'accept-language' },
],
}
const locales = JSON.parse(process.env.LOCALES || '["en"]')
export function proxy(request) {
if (
request.headers.get('accept-language') &&
request.headers.get('enable-embedded-view')
) {
const lang = request.headers.get('accept-language')
if (locales.includes(lang)) {
return NextResponse.rewrite(
new URL(
`/${lang}${request.nextUrl.pathname}${request.nextUrl.search}`,
request.url,
),
)
}
const fallbackLang = request.headers.get('accept-language').split('-')[0]
if (locales.includes(fallbackLang)) {
return NextResponse.rewrite(
new URL(
`/${fallbackLang}${request.nextUrl.pathname}${request.nextUrl.search}`,
request.url,
),
)
}
}
}