From d9471c00f6c4da2997b83a04bfc22a507599e990 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Fri, 14 Aug 2026 23:35:52 +0800 Subject: [PATCH] fix href reloc --- v3/src/layouts/Layout.astro | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/v3/src/layouts/Layout.astro b/v3/src/layouts/Layout.astro index f42c3fa9..a391758f 100644 --- a/v3/src/layouts/Layout.astro +++ b/v3/src/layouts/Layout.astro @@ -131,7 +131,19 @@ metadata.description ||= (t({ } } if (lang && lang !== defaultLocale) { - location.href = `${baseUrl + lang + location.pathname + (location.hash || location.search || '')}`.replace(/\/$/, ''); + // Localized URLs can differ from prefixed paths (blog post slugs carry a + // language suffix), so prefer the hreflang alternate rendered above. + let target; + document.querySelectorAll('link[rel="alternate"][hreflang]').forEach((link) => { + if (link.getAttribute('hreflang').toLowerCase() === lang) { + target = link.getAttribute('href'); + } + }); + if (target) { + location.href = target + (location.hash || location.search || ''); + } else { + location.href = `${baseUrl + lang + location.pathname + (location.hash || location.search || '')}`.replace(/\/$/, ''); + } } }