Skip to content

Commit 3560ec4

Browse files
committed
fix(docs): prepend base path to language redirect URLs
Use import.meta.env.BASE_URL to construct correct redirect paths. Previously hardcoded /zh/ and /en/ caused 404 on GitHub Pages where the site is served under /compress-kit/.
1 parent 8001f8b commit 3560ec4

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

docs/en/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { onBeforeMount } from 'vue'
1919

2020
onBeforeMount(() => {
2121
if (typeof window !== 'undefined') {
22-
localStorage.setItem('docs-lang-preference', '/en/')
22+
localStorage.setItem('docs-lang-preference', `${import.meta.env.BASE_URL}en/`)
2323
}
2424
})
2525
</script>

docs/index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ import { onBeforeMount } from 'vue'
2020
onBeforeMount(() => {
2121
if (typeof window === 'undefined') return
2222

23+
const base = import.meta.env.BASE_URL
2324
const userLang = navigator.language || navigator.userLanguage || ''
2425
const savedLang = localStorage.getItem('docs-lang-preference')
2526

2627
// Use saved preference if exists, otherwise use browser language
27-
const targetLang = savedLang || (userLang.startsWith('zh') ? '/zh/' : '/en/')
28+
const targetLang = savedLang || (userLang.startsWith('zh') ? `${base}zh/` : `${base}en/`)
2829
const currentPath = window.location.pathname
2930

3031
// Only redirect if not already on correct language path
31-
if (targetLang === '/zh/' && !currentPath.startsWith('/zh')) {
32-
window.location.replace('/zh/')
33-
} else if (targetLang === '/en/' && !currentPath.startsWith('/en')) {
34-
window.location.replace('/en/')
32+
if (targetLang.endsWith('/zh/') && !currentPath.includes('/zh/')) {
33+
window.location.replace(`${base}zh/`)
34+
} else if (targetLang.endsWith('/en/') && !currentPath.includes('/en/')) {
35+
window.location.replace(`${base}en/`)
3536
}
3637
})
3738
</script>

docs/zh/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { onBeforeMount } from 'vue'
1919

2020
onBeforeMount(() => {
2121
if (typeof window !== 'undefined') {
22-
localStorage.setItem('docs-lang-preference', '/zh/')
22+
localStorage.setItem('docs-lang-preference', `${import.meta.env.BASE_URL}zh/`)
2323
}
2424
})
2525
</script>

0 commit comments

Comments
 (0)