Skip to content

Commit 89a33f8

Browse files
committed
web: Fix inconsistent alternate URLs on root
1 parent b42dbbe commit 89a33f8

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

web/composables/compat.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,22 @@ export function useSelfOrigin() {
241241

242242
/** replace locale in path */
243243
export function getPathWithLocale(fullPath: string, locale: Locale, fallbackLocale: Locale) {
244-
const urlPaths = fullPath.split('/')
244+
const urlPaths = fullPath == '/' ? [''] : fullPath.split('/')
245245
const firstPath = urlPaths[1] as LocaleCode
246246
const validCodes = locales.map(l => l.code)
247-
const urlWithoutLocale = validCodes.includes(firstPath) ? '/' + urlPaths.slice(2).join('/') : fullPath
248-
return `${locale.iso == fallbackLocale.iso ? '' : '/' + locale.code}` + urlWithoutLocale
247+
if (validCodes.includes(firstPath)) {
248+
// on non-en locale
249+
if (locale.iso == fallbackLocale.iso) {
250+
// go to en, so remove
251+
urlPaths.splice(1, 1)
252+
} else {
253+
// go to other, so replace
254+
urlPaths[1] = locale.code
255+
}
256+
} else if (locale.iso != fallbackLocale.iso) {
257+
// on en locale
258+
// go to other, so insert
259+
urlPaths.splice(1, 0, locale.code)
260+
}
261+
return urlPaths.join('/')
249262
}

web/renderer/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function createRouter(i18n: AppI18n, pageContext: PageContext, head: VueHeadClie
2323
link: [
2424
{
2525
rel: 'canonical',
26-
href: origin + to.path, // without queries and hash
26+
href: origin + (to.path == '/' ? '' : to.path), // without queries and hash
2727
},
2828
{
2929
rel: 'alternate',

0 commit comments

Comments
 (0)