From a245b222dc70f5cf450ba4837b342e4fc9cd7b81 Mon Sep 17 00:00:00 2001 From: Filip Czaplicki Date: Thu, 8 Feb 2024 21:35:52 +0100 Subject: [PATCH] Generate alternate links for languages --- scripts/build_every_lang.sh | 1 + vite.config.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/scripts/build_every_lang.sh b/scripts/build_every_lang.sh index f458003..730eb1f 100755 --- a/scripts/build_every_lang.sh +++ b/scripts/build_every_lang.sh @@ -7,6 +7,7 @@ for lang in public/locales/*; do continue fi export VITE_DEFAULT_LANG=$lang + export VITE_BASE_URL="/$lang/" echo "Generating $lang" npm run build -- --outDir "build/$lang" --base "/$lang" done diff --git a/vite.config.js b/vite.config.js index f188d7d..d827766 100644 --- a/vite.config.js +++ b/vite.config.js @@ -20,6 +20,16 @@ const htmlPlugin = async (env) => { ...translationsEn, ...translationsTarget, }; + const baseUrl = env.VITE_BASE_URL ?? ""; + const canonicalLinks = ``; + const alternateLinks = Object.keys(languages) + .map( + (lang) => + `\t`, + ) + .join("\n"); + const defaultAlternateLink = `\t`; + const seoLinks = `${canonicalLinks}\n${alternateLinks}\n${defaultAlternateLink}`; return { name: "html-transform", transformIndexHtml(html) { @@ -32,6 +42,10 @@ const htmlPlugin = async (env) => { .replace( //, ``, + ) + .replace( + //, + seoLinks, ); }, };