Sitemap hreflang alternates: i18n config on @astrojs/sitemap (en/it/mt from src/pages locale dirs) + serialize hook adding x-default; assigns a new links array to avoid the lib's shared-cache triple x-default; /privacy/ stays unlinked (EN-only page)
deploy / build-and-publish (push) Successful in 40s
deploy / build-and-publish (push) Successful in 40s
This commit is contained in:
@@ -1,14 +1,41 @@
|
||||
import { defineConfig } from "astro/config";
|
||||
import sitemap from "@astrojs/sitemap";
|
||||
import fs from "node:fs";
|
||||
|
||||
// The canonical public origin for SEO is the APEX domain (marketing at
|
||||
// validationlab.eu root); Caddy serves the same build at the apex, www
|
||||
// and the astro subdomain.
|
||||
|
||||
// Locale subdirectories of src/pages (it/, mt/, ...) drive both the
|
||||
// sitemap's hreflang alternates and nothing else - the <head> tags are
|
||||
// built in Site.astro from the page files themselves.
|
||||
const DEFAULT_LOCALE = "en";
|
||||
const localeDirs = fs
|
||||
.readdirSync(new URL("./src/pages/", import.meta.url), { withFileTypes: true })
|
||||
.filter((d) => d.isDirectory())
|
||||
.map((d) => d.name);
|
||||
|
||||
export default defineConfig({
|
||||
site: "https://validationlab.eu",
|
||||
integrations: [
|
||||
sitemap({
|
||||
filter: (page) => !page.includes("/admin/") && !page.includes("/uploads/"),
|
||||
i18n: {
|
||||
defaultLocale: DEFAULT_LOCALE,
|
||||
// keys are the URL prefixes, values the hreflang codes
|
||||
locales: Object.fromEntries([DEFAULT_LOCALE, ...localeDirs].map((c) => [c, c])),
|
||||
},
|
||||
serialize: (item) => {
|
||||
// the sitemap lib emits one xhtml:link per locale but no x-default;
|
||||
// point it at the default-locale URL. NOTE: item.links may be a
|
||||
// shared cached array across same-path locale URLs - never mutate
|
||||
// it, assign a new array.
|
||||
if (item.links?.length) {
|
||||
const def = item.links.find((l) => l.lang === DEFAULT_LOCALE);
|
||||
if (def) item.links = [...item.links, { url: def.url, lang: "x-default" }];
|
||||
}
|
||||
return item;
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user