forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add baseUrl runtimeConfig support * test: add baseUrl runtimeConfig test * docs: add runtimeConfig page * docs: fix incorrect environment variable name
- Loading branch information
1 parent
2960bb4
commit caa697c
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Runtime config | ||
|
||
Some options can be set via the `runtimeConfig`, setting options this way makes it possible to override these after building using environment variables. | ||
|
||
--- | ||
|
||
## Usage | ||
|
||
If you want to use environment variables to change [supported options](#supported-options), you will have to set these in `runtimeConfig.public.i18n`. | ||
|
||
The module configuration takes precedence, options set through `runtimeConfig` will only be used if they are unset. | ||
|
||
Setting `baseUrl` through `runtimeConfig` would look like this: | ||
|
||
```ts {}[nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
runtimeConfig: { | ||
public: { | ||
i18n: { | ||
baseUrl: 'https://example.com', | ||
} | ||
} | ||
}, | ||
modules: [ | ||
'@nuxtjs/i18n' | ||
], | ||
i18n: { | ||
// Leave options unset that you want to set using `runtimeConfig` | ||
// baseUrl: 'https://example.com', | ||
} | ||
}) | ||
``` | ||
|
||
With this configuration you will be able to override the `baseUrl` option by setting the `NUXT_PUBLIC_I18N_BASE_URL` environment variable. You can read more about how this works in the [Nuxt documentation](https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables). | ||
|
||
## Supported options | ||
|
||
These options can be set using `runtimeConfig`: | ||
* [`baseUrl`](./routing#baseUrl) | ||
|
||
::alert{type=warning} | ||
Only [serializable values are supported](https://nuxt.com/docs/guide/going-further/runtime-config#serialization) in `runtimeConfig`, options set this way may not support all available types (such as functions) as would normally be possible using the default configuration. | ||
:: | ||
|
||
::alert{type=info} | ||
If you would like other options to be supported, open an issue describing your use case, or open a PR adding to add support yourself! | ||
:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters