changed / to redirect accordingly to Accept-Language header
This commit is contained in:
parent
47c723e932
commit
acb157efa1
4 changed files with 38 additions and 6 deletions
|
|
@ -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
|
// (Astro can't express this as a dynamic redirect in a static build: the
|
||||||
// destination must be an existing route with the same params.)
|
// destination must be an existing route with the same params.)
|
||||||
const LEGACY_EN_PATHS = [
|
const LEGACY_EN_PATHS = [
|
||||||
'/',
|
|
||||||
'/about',
|
'/about',
|
||||||
'/posts',
|
'/posts',
|
||||||
'/posts/hello-world',
|
'/posts/hello-world',
|
||||||
|
|
@ -40,11 +39,6 @@ const DRAFT_PATHS = readPostFiles()
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
site: 'https://gabeefran.co',
|
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}`])),
|
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)) })],
|
integrations: [react(), mdx(), sitemap({ filter: (page) => !DRAFT_PATHS.some((path) => page.endsWith(path)) })],
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site);
|
||||||
{Object.entries(alternates).map(([altLang, path]) => (
|
{Object.entries(alternates).map(([altLang, path]) => (
|
||||||
<link rel="alternate" hreflang={HREFLANGS[altLang as Lang]} href={new URL(path, Astro.site)} />
|
<link rel="alternate" hreflang={HREFLANGS[altLang as Lang]} href={new URL(path, Astro.site)} />
|
||||||
))}
|
))}
|
||||||
|
<link rel="alternate" hreflang="x-default" href="https://gabeefran.co/" />
|
||||||
<meta name="generator" content={Astro.generator} />
|
<meta name="generator" content={Astro.generator} />
|
||||||
<link rel="sitemap" href="/sitemap-index.xml" />
|
<link rel="sitemap" href="/sitemap-index.xml" />
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ export const HREFLANGS: Record<Lang, string> = {
|
||||||
pt: 'pt-BR',
|
pt: 'pt-BR',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const SUPPORTED = ["pt", "en"] as const;
|
||||||
|
export const DEFAULT = "en";
|
||||||
|
|
||||||
export const ui = {
|
export const ui = {
|
||||||
en: {
|
en: {
|
||||||
'site.name': 'gabeefranco',
|
'site.name': 'gabeefranco',
|
||||||
|
|
@ -179,3 +182,21 @@ export function postPath(lang: Lang, slug: string): string {
|
||||||
export function localizedAlternates(pathname: string): Record<Lang, string> {
|
export function localizedAlternates(pathname: string): Record<Lang, string> {
|
||||||
return Object.fromEntries(LANGS.map((lang) => [lang, getLocalizedPath(pathname, lang)])) as Record<Lang, string>;
|
return Object.fromEntries(LANGS.map((lang) => [lang, getLocalizedPath(pathname, lang)])) as Record<Lang, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
16
src/pages/index.ts
Normal file
16
src/pages/index.ts
Normal file
|
|
@ -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;
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue