From 6b012a16b8d31316e669a66cda5851a21c6f3651 Mon Sep 17 00:00:00 2001 From: wo-o29 Date: Tue, 11 Nov 2025 16:36:18 +0900 Subject: [PATCH] fix: prevent path corruption when switching languages on 404 pages --- fundamentals/a11y/.vitepress/theme/Layout.vue | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/fundamentals/a11y/.vitepress/theme/Layout.vue b/fundamentals/a11y/.vitepress/theme/Layout.vue index 8856664d..127f0a7f 100644 --- a/fundamentals/a11y/.vitepress/theme/Layout.vue +++ b/fundamentals/a11y/.vitepress/theme/Layout.vue @@ -3,9 +3,37 @@ import DefaultTheme from "vitepress/theme"; import Comments from "./components/Comments.vue"; import OneNavigation from "@shared/components/OneNavigation.vue"; import { useLocale } from "./hooks"; +import { onMounted } from "vue"; +import { useRoute } from "vitepress"; const { Layout } = DefaultTheme; const { isKorean } = useLocale(); +const route = useRoute(); + +onMounted(() => { + document.addEventListener("click", (e) => { + const target = e.target as HTMLElement; + const link = target.closest('a[href^="/a11y/"]'); + + if (link?.classList.value === "VPLink link") { + e.preventDefault(); + + const href = link.getAttribute("href"); + const targetLangMatch = href?.match(/^\/a11y\/(en|ja|zh-hans)\//); + const targetLang = targetLangMatch ? targetLangMatch[1] : "ko"; + + const pathWithoutLang = route.path + .replace(/^\/a11y\/(en|ja|zh-hans)\//, "/a11y/") + .replace(/^\/a11y\//, ""); + const newPath = + targetLang === "ko" + ? `/a11y/${pathWithoutLang}` + : `/a11y/${targetLang}/${pathWithoutLang}`; + + window.location.href = newPath; + } + }); +});