forked from wokwi/wokwi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-locale.js
66 lines (58 loc) · 1.93 KB
/
auto-locale.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Automatically redirect users to their native language documentation, if available, based on the Accept-Language header
sent from their browser. Runs on Cloudflare Workers.
Copyright (C) 2021, Uri Shaked.
Released under the terms of the MIT license.
*/
/* SPDX-License-Identifier: MIT */
const docsUrl = 'https://docs.wokwi.com';
/**
* @param {Request} request
*/
function getLanguages(request) {
const acceptLanguage = request.headers.get('Accept-Language');
const result = [];
for (const item of (acceptLanguage ?? '').split(',')) {
const [lang, rest] = item.split(';');
const quality = (rest ?? '').startsWith('q=') ? parseFloat(rest.substr(2)) : 1;
result.push({ lang, quality });
}
return result;
}
/**
* @param {ReturnType<getLanguages>} langs
* @param {string[]} supportedLangs
* @param {string} defaultLang
*/
function pickLanguage(langs, supportedLangs, defaultLang) {
langs = [...langs].sort((a, b) => b.quality - a.quality);
for (const { lang, quality } of langs) {
if (supportedLangs.includes(lang.toLowerCase())) {
return { lang, quality };
}
}
return { lang: defaultLang, quality: 0 };
}
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request).catch((err) => new Response({ status: 500 })));
});
/**
* @param {Request} request
* @returns {Promise<Response>}
*/
async function handleRequest(request) {
const { pathname } = new URL(request.url);
const referer = request.headers.get('Referer');
const secFetchSite = request.headers.get('Sec-Fetch-Site');
const { lang } = pickLanguage(getLanguages(request), ['pt-br', 'pt', 'en'], 'en');
if (secFetchSite !== 'same-origin' && (!referer || !referer.startsWith(docsUrl))) {
if (
lang.startsWith('pt') &&
!pathname.startsWith('/pt-BR/') &&
!pathname.startsWith('/assets/')
) {
return Response.redirect(`${docsUrl}/pt-BR${pathname}`);
}
}
return fetch(request);
}