Skip to content

Commit

Permalink
fix(head): apply baseURL to head composable (#2912)
Browse files Browse the repository at this point in the history
Co-authored-by: Bobbie Goede <bobbiegoede@gmail.com>
  • Loading branch information
warflash and BobbieGoede committed Apr 17, 2024
1 parent 60396c6 commit 6dcbfbb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 23 additions & 0 deletions specs/issues/2911.spec.ts
@@ -0,0 +1,23 @@
import { test, expect, describe } from 'vitest'
import { fileURLToPath } from 'node:url'
import { URL } from 'node:url'
import { setup } from '../utils'
import { renderPage } from '../helper'

describe('#2911', async () => {
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/basic`, import.meta.url)),
browser: true,
nuxtConfig: {
app: {
baseURL: '/base-path'
}
}
})

test('`useLocaleHead` uses Nuxt `app.baseURL` in meta tags', async () => {
const { page } = await renderPage('/base-path')

expect(await page.locator('head #i18n-alt-en').getAttribute('href')).toMatch(/\/base-path$/)
})
})
8 changes: 5 additions & 3 deletions src/runtime/routing/compatibles/head.ts
Expand Up @@ -3,6 +3,7 @@ import { unref, useNuxtApp, useRuntimeConfig } from '#imports'
import { getComposer, getLocale, getLocales, getNormalizedLocales } from '../utils'
import { getRouteBaseName, localeRoute, switchLocalePath } from './routing'
import { isArray, isObject } from '@intlify/shared'
import { joinURL } from 'ufo'

import type { I18nHeadMetaInfo, MetaAttrs, LocaleObject, I18nHeadOptions } from '#build/i18n.options.mjs'
import type { CommonComposableOptions } from '../../utils'
Expand Down Expand Up @@ -72,8 +73,9 @@ export function localeHead(
}

function getBaseUrl() {
const i18n = getComposer(useNuxtApp().$i18n)
return unref(i18n.baseUrl)
const nuxtApp = useNuxtApp()
const i18n = getComposer(nuxtApp.$i18n)
return joinURL(unref(i18n.baseUrl), nuxtApp.$config.app.baseURL)
}

export function getHreflangLinks(
Expand Down Expand Up @@ -224,5 +226,5 @@ function hypenToUnderscore(str: string) {

function toAbsoluteUrl(urlOrPath: string, baseUrl: string) {
if (urlOrPath.match(/^https?:\/\//)) return urlOrPath
return baseUrl + urlOrPath
return joinURL(baseUrl, urlOrPath)
}

0 comments on commit 6dcbfbb

Please sign in to comment.