|
| 1 | +interface Env { |
| 2 | + ASSETS: Fetcher; |
| 3 | +} |
| 4 | + |
| 5 | +const LOCALE_COOKIE = "locale"; |
| 6 | + |
| 7 | +function getCookie(request: Request, name: string): string | null { |
| 8 | + const cookieHeader = request.headers.get("Cookie"); |
| 9 | + if (!cookieHeader) { |
| 10 | + return null; |
| 11 | + } |
| 12 | + |
| 13 | + for (const part of cookieHeader.split(";")) { |
| 14 | + const [key, ...rest] = part.trim().split("="); |
| 15 | + if (key === name) { |
| 16 | + return decodeURIComponent(rest.join("=")); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + return null; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Prefer English only when the highest-priority language tag is English. |
| 25 | + * A Brazilian abroad with pt-BR first stays on Portuguese. |
| 26 | + */ |
| 27 | +function prefersEnglish(acceptLanguage: string | null): boolean { |
| 28 | + if (!acceptLanguage) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + const tags = acceptLanguage |
| 33 | + .split(",") |
| 34 | + .map((part) => { |
| 35 | + const [rawTag, ...params] = part.trim().split(";"); |
| 36 | + const qParam = params.find((param) => param.trim().startsWith("q=")); |
| 37 | + const quality = qParam ? Number.parseFloat(qParam.split("=")[1] ?? "1") : 1; |
| 38 | + |
| 39 | + return { |
| 40 | + lang: rawTag.trim().toLowerCase(), |
| 41 | + quality: Number.isFinite(quality) ? quality : 0, |
| 42 | + }; |
| 43 | + }) |
| 44 | + .filter((tag) => tag.lang.length > 0) |
| 45 | + .sort((a, b) => b.quality - a.quality); |
| 46 | + |
| 47 | + for (const { lang } of tags) { |
| 48 | + if (lang.startsWith("pt")) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + if (lang.startsWith("en")) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +function isPortugueseHome(pathname: string): boolean { |
| 61 | + return pathname === "/" || pathname === "/index.html"; |
| 62 | +} |
| 63 | + |
| 64 | +export default { |
| 65 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 66 | + const url = new URL(request.url); |
| 67 | + const isGetOrHead = request.method === "GET" || request.method === "HEAD"; |
| 68 | + const savedLocale = getCookie(request, LOCALE_COOKIE); |
| 69 | + |
| 70 | + const shouldOfferEnglishHome = |
| 71 | + isGetOrHead && |
| 72 | + isPortugueseHome(url.pathname) && |
| 73 | + savedLocale !== "pt" && |
| 74 | + (savedLocale === "en" || prefersEnglish(request.headers.get("Accept-Language"))); |
| 75 | + |
| 76 | + if (shouldOfferEnglishHome) { |
| 77 | + const target = new URL("/en", url); |
| 78 | + return new Response(null, { |
| 79 | + status: 302, |
| 80 | + headers: { |
| 81 | + Location: target.toString(), |
| 82 | + Vary: "Accept-Language, Cookie", |
| 83 | + "Cache-Control": "private, no-store", |
| 84 | + }, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + return env.ASSETS.fetch(request); |
| 89 | + }, |
| 90 | +}; |
0 commit comments