From b2fa32d1343459794e2f75e5b0b280147ff2096f Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 24 Aug 2024 16:36:52 +0800 Subject: [PATCH] Generate alternate link tags for all languages --- src/locales.js | 2 +- vite.config.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/locales.js b/src/locales.js index 74bf4bd4..b19934c6 100644 --- a/src/locales.js +++ b/src/locales.js @@ -15,7 +15,7 @@ const locales = [ export const LOCALES = locales; let devLocales = []; -if (import.meta.env.DEV || import.meta.env.PHANPY_SHOW_DEV_LOCALES) { +if (import.meta.env?.DEV || import.meta.env?.PHANPY_SHOW_DEV_LOCALES) { devLocales = catalogs .filter(({ completion }) => completion < PERCENTAGE_THRESHOLD) .map(({ code }) => code); diff --git a/vite.config.js b/vite.config.js index 7bf25f11..98d05043 100644 --- a/vite.config.js +++ b/vite.config.js @@ -12,9 +12,12 @@ import { VitePWA } from 'vite-plugin-pwa'; import removeConsole from 'vite-plugin-remove-console'; import { run } from 'vite-plugin-run'; +import { ALL_LOCALES } from './src/locales'; + const allowedEnvPrefixes = ['VITE_', 'PHANPY_']; const { NODE_ENV } = process.env; const { + PHANPY_WEBSITE: WEBSITE, PHANPY_CLIENT_NAME: CLIENT_NAME, PHANPY_APP_ERROR_LOGGING: ERROR_LOGGING, } = loadEnv('production', process.cwd(), allowedEnvPrefixes); @@ -83,6 +86,20 @@ export default defineConfig({ }), htmlPlugin({ headScripts: ERROR_LOGGING ? [rollbarCode] : [], + links: [ + ...ALL_LOCALES.map((lang) => ({ + rel: 'alternate', + hreflang: lang, + // *Fully-qualified* URLs + href: `${WEBSITE}/?lang=${lang}`, + })), + // https://developers.google.com/search/docs/specialty/international/localized-versions#xdefault + { + rel: 'alternate', + hreflang: 'x-default', + href: `${WEBSITE}`, + }, + ], }), generateFile([ {