Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set baseUrl via runtimeConfig #1920

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions specs/seo/baseUrl_runtimeConfig.spec.ts
@@ -0,0 +1,35 @@
import { expect, test } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, $fetch } from '@nuxt/test-utils'
import { getDom, getDataFromDom, assertLocaleHeadWithDom } from '../helper'

const configDomain = 'https://runtime-config-domain.com'
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/basic`, import.meta.url)),
browser: true,
// overrides
nuxtConfig: {
runtimeConfig: {
public: {
i18n: {
baseUrl: configDomain
}
}
},
i18n: {
// debug: true,
defaultLocale: 'en',
baseUrl: ''
}
}
})

test('render seo tags with baseUrl', async () => {
const html = await $fetch('/?noncanonical')
const dom = getDom(html)
await assertLocaleHeadWithDom(dom, '#home-use-locale-head')

const links = getDataFromDom(dom, '#home-use-locale-head').link
const i18nCan = links.find(x => x.id === 'i18n-can')
expect(i18nCan.href).toContain(configDomain)
})
11 changes: 10 additions & 1 deletion src/runtime/utils.ts
Expand Up @@ -485,18 +485,27 @@ export function extendBaseUrl<Context extends NuxtApp = NuxtApp>(
): BaseUrlResolveHandler<Context> {
return (context: Context): string => {
if (isFunction(baseUrl)) {
return baseUrl(context)
const baseUrlResult = baseUrl(context)
__DEBUG__ && console.log('baseUrl: using localeLoader function -', baseUrlResult)
return baseUrlResult
}

const { differentDomains, localeCodeLoader, normalizedLocales } = options
const localeCode = isFunction(localeCodeLoader) ? localeCodeLoader() : localeCodeLoader
if (differentDomains && localeCode) {
const domain = getDomainFromLocale(localeCode, normalizedLocales, options.nuxt)
if (domain) {
__DEBUG__ && console.log('baseUrl: using differentDomains -', domain)
return domain
}
}

const config = context.$config?.public?.i18n
if (config?.baseUrl) {
__DEBUG__ && console.log('baseUrl: using runtimeConfig -', config.baseUrl)
return config.baseUrl
}

return baseUrl
}
}
Expand Down