diff --git a/astro.config.mjs b/astro.config.mjs
index 39d2dfb..717af48 100644
--- a/astro.config.mjs
+++ b/astro.config.mjs
@@ -14,7 +14,6 @@ import { readPostFiles } from './src/lib/post-files.ts';
// (Astro can't express this as a dynamic redirect in a static build: the
// destination must be an existing route with the same params.)
const LEGACY_EN_PATHS = [
- '/',
'/about',
'/posts',
'/posts/hello-world',
@@ -40,11 +39,6 @@ const DRAFT_PATHS = readPostFiles()
export default defineConfig({
site: 'https://gabeefran.co',
- // No `i18n` block on purpose: locales come from src/lib/i18n.ts and every
- // locale, English included, lives under src/pages/[lang]/. Astro's i18n
- // routing would add middleware that 404s the unprefixed on-demand
- // /page-card and /social-card routes (prefix-always) or /en/* (default).
-
redirects: Object.fromEntries(LEGACY_EN_PATHS.map((path) => [path, path === '/' ? '/en' : `/en${path}`])),
integrations: [react(), mdx(), sitemap({ filter: (page) => !DRAFT_PATHS.some((path) => page.endsWith(path)) })],
diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro
index 9bc826d..3d42c6d 100644
--- a/src/layouts/BaseLayout.astro
+++ b/src/layouts/BaseLayout.astro
@@ -44,6 +44,7 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site);
{Object.entries(alternates).map(([altLang, path]) => (
))}
+
diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts
index 677c278..ef31b3f 100644
--- a/src/lib/i18n.ts
+++ b/src/lib/i18n.ts
@@ -16,6 +16,9 @@ export const HREFLANGS: Record = {
pt: 'pt-BR',
};
+export const SUPPORTED = ["pt", "en"] as const;
+export const DEFAULT = "en";
+
export const ui = {
en: {
'site.name': 'gabeefranco',
@@ -179,3 +182,21 @@ export function postPath(lang: Lang, slug: string): string {
export function localizedAlternates(pathname: string): Record {
return Object.fromEntries(LANGS.map((lang) => [lang, getLocalizedPath(pathname, lang)])) as Record;
}
+
+export function pickLocale(header: string | null): string {
+ if (!header) return DEFAULT;
+
+ // "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7" -> [{tag:"pt", q:1}, ...]
+ const prefs = header
+ .split(",")
+ .map((part) => {
+ const [tag, ...params] = part.trim().split(";");
+ const qParam = params.find((p) => p.trim().startsWith("q="));
+ const q = qParam ? parseFloat(qParam.split("=")[1]) : 1;
+ return { lang: tag.toLowerCase().split("-")[0], q: isNaN(q) ? 0 : q };
+ })
+ .filter((p) => p.q > 0)
+ .sort((a, b) => b.q - a.q);
+
+ return prefs.find((p) => (SUPPORTED as readonly string[]).includes(p.lang))?.lang ?? DEFAULT;
+}
diff --git a/src/pages/index.ts b/src/pages/index.ts
new file mode 100644
index 0000000..42c292c
--- /dev/null
+++ b/src/pages/index.ts
@@ -0,0 +1,16 @@
+import type { APIRoute } from "astro";
+import { pickLocale, SUPPORTED } from "../lib/i18n";
+
+export const prerender = false;
+
+export const GET: APIRoute = ({ request, cookies, redirect }) => {
+ const saved = cookies.get("lang")?.value;
+ const lang =
+ saved && (SUPPORTED as readonly string[]).includes(saved)
+ ? saved
+ : pickLocale(request.headers.get("accept-language"));
+
+ const res = redirect(`/${lang}/`, 302);
+ res.headers.set("Vary", "Accept-Language, Cookie");
+ return res;
+};