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(config): add site.url as an alternative to baseUrl #2481

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
38 changes: 35 additions & 3 deletions docs/content/2.guide/16.migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can continue defining `vueI18n` options in `i18n.config`. Refer to the [Vue

### Change the route key rules in `pages` option

The key of route set in the `pages` option has been changed to be file-based relative to the `pages/` directory in Nuxt, and **excluding the leading `/`**.
The key of route set in the `pages` option has been changed to be file-based relative to the `pages/` directory in Nuxt, and **excluding the leading `/`**.

The reason is that it is more intuitive to match Nuxt file-based routing.

Expand Down Expand Up @@ -83,6 +83,38 @@ Deprecated for the same reason as the `localeLocation()` Option API.

Use `localeHead()` instead for the Options API style.

### `baseUrl` component option

You can use `site.url` instead of `i18n.baseUrl`.

::alert{type="info"}
The `site` configuration option stems from [Nuxt SEO's `site-config` module](https://nuxtseo.com/site-config), which `nuxt-i18n` makes use of now.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there will need to be some documentation on all the ways that site config can be set and hosted on this page. The docs aren't that friendly at the moment, something on my to-do list.

::

Nuxt2:
```vue {}[nuxt.config.js]
<script>
import Vue from 'vue'

export default Vue.extend({
nuxtI18n: {
baseUrl: 'https://localhost:3000',
}
})
</script>
```

Nuxt3:
```vue {}[nuxt.config.js]
<script setup>
defineI18nRoute({
site: {
url: 'http://localhost:3000'
}
})
</script>
```

### Deprecated `nuxtI18n` component option

Replaced by the `defineI18nRoute()` compiler macro as it can be optimized with bundler.
Expand Down Expand Up @@ -122,7 +154,7 @@ Use the `customRoutes` option. because the option name `parsePages` is not intui
modules: [
'@nuxtjs/i18n'
],

i18n: {
// ...
- parsePages: false,
Expand All @@ -141,7 +173,7 @@ Use `dynamicRouteParams` option. Because Vuex is no longer required by the Nuxt
modules: [
'@nuxtjs/i18n'
],

i18n: {
// ...
- vuex: true,
Expand Down
20 changes: 15 additions & 5 deletions docs/content/2.guide/7.seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To leverage the SEO benefits, you must configure the `locales` option as an arra
```ts {}[nuxt.config.ts]
export default defineNuxtConfig({
// ...

i18n: {
locales: [
{
Expand All @@ -41,26 +41,36 @@ export default defineNuxtConfig({
}
]
},

// ...
})
```

You must also set the `baseUrl` option to your production domain in order to make alternate URLs fully-qualified:
You must also set the `baseUrl` or `site.url` option to your production domain in order to make alternate URLs fully-qualified:

```ts {}[nuxt.config.ts]
export default defineNuxtConfig({
// ...

site: {
url: 'https://my-nuxt-app.com'
},

// or

i18n: {
baseUrl: 'https://my-nuxt-app.com'
},

// ...
})
```

(Note that `baseUrl` can also be set to a function. Check [`baseUrl` documentation](/options/routing#baseurl).)
::alert{type="info"}
The `site` configuration option stems from [Nuxt SEO's `site-config` module](https://nuxtseo.com/site-config).
::

::alert{type="info"} Note that `i18n.baseUrl` can also be set as a function as specified in [`i18n.baseUrl`'s documentation](/options/routing#baseurl).

## Setup

Expand Down
6 changes: 5 additions & 1 deletion docs/content/3.options/2.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Routing and Strategies options.

## `baseUrl`

::alert{type="info"}
You can use [Nuxt SEO's `site.url`](https://nuxtseo.com/site-config) instead of `i18n.baseUrl`.
::

- type: `string` or `function`
- default: `''`

Expand Down Expand Up @@ -132,7 +136,7 @@ Set to a path to which you want to redirect users accessing the root URL (`/`).
- type: `boolean`
- default: `false`

Whether to localize dynamic route parameters.
Whether to localize dynamic route parameters.

If `true`, you can set the dynamic route parameter to `nuxtI18n` field of `definePageMeta` for each locale:

Expand Down
16 changes: 10 additions & 6 deletions docs/content/3.options/9.runtime-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Some options can be set via the `runtimeConfig`, setting options this way makes

## Usage

If you want to use environment variables to change [supported options](#supported-options), you will have to set these in `runtimeConfig.public.i18n`.
If you want to use environment variables to change [supported options](#supported-options), you will have to set these in `runtimeConfig.public.i18n`.

```ts {}[nuxt.config.ts]
export default defineNuxtConfig({
Expand All @@ -15,12 +15,12 @@ export default defineNuxtConfig({
],
i18n: {
// Leave options unset that you want to set using `runtimeConfig`
// baseUrl: 'https://example.com',
// defaultLocale: 'en',
},
runtimeConfig: {
public: {
i18n: {
baseUrl: 'https://example.com',
defaultLocale: 'en',
locales: {},
// other options ...
}
Expand All @@ -47,13 +47,17 @@ The module configuration takes precedence, options set through `runtimeConfig` w
These options can be set using `runtimeConfig`:

### `baseUrl`
* key: `NUXT_PUBLIC_I18N_BASE_URL`

This runtime config option is the same as the [`baseUrl`](./routing#baseUrl) module option.
::alert{type="info"}
You can use [Nuxt SEO's `site.url`](https://nuxtseo.com/site-config) instead of `i18n.baseUrl`.
::

* key: `NUXT_PUBLIC_I18N_BASE_URL`

This runtime config option is the same as the [`baseUrl`](./routing#baseUrl) module option.

::alert{type=warning}
Note that the `baseUrl` module option allows you to set the function, but the runtime config does not due to limitations.
Nuxt's runtime config does not allow values to be specified as functions, so you must use the `baseUrl` module option if you need to set the value using a function.
::

### `locales`
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
"knitwork": "^1.0.0",
"magic-string": "^0.30.4",
"mlly": "^1.4.2",
"nuxt-site-config": "^1.4.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harlan-zw https://nuxtseo.com/site-config/getting-started/installation specifies that nuxt-site-config has to be installed along with nuxt-site-config-kit. As I only ever made use of a nuxt-site-config-kit import in this PR and ignoring the playground's useSiteConfig usage right now, I wonder: is it really required to install nuxt-site-config as well? And if so, why?

"nuxt-site-config-kit": "^1.4.0",
"pathe": "^1.1.1",
"ufo": "^1.3.1",
"unplugin": "^1.5.0",
Expand Down
4 changes: 3 additions & 1 deletion playground/layer-module/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default defineNuxtConfig({
i18n: {
langDir: 'locales',
lazy: true,
baseUrl: 'http://localhost:3000',
locales: [
{
code: 'en',
Expand All @@ -30,5 +29,8 @@ export default defineNuxtConfig({
name: 'Nederlands'
}
]
},
site: {
url: 'http://localhost:3000'
}
})
4 changes: 3 additions & 1 deletion playground/layers/i18n-layer/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default defineNuxtConfig({
i18n: {
langDir: 'locales',
lazy: true,
baseUrl: 'http://localhost:3000',
customRoutes: 'config',
pages: {
history: {
Expand Down Expand Up @@ -58,5 +57,8 @@ export default defineNuxtConfig({
// name: 'Français'
// }
]
},
site: {
url: 'http://localhost:3000'
}
})
5 changes: 3 additions & 2 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default defineNuxtConfig({
},
langDir: 'locales',
lazy: true,
baseUrl: 'http://localhost:3000',
locales: [
{
code: 'en',
Expand Down Expand Up @@ -117,7 +116,6 @@ export default defineNuxtConfig({
},
langDir: 'locales',
lazy: true,
baseUrl: 'http://localhost:3000',
locales: [
{
code: 'en',
Expand Down Expand Up @@ -179,5 +177,8 @@ export default defineNuxtConfig({
// // redirectOn: 'root'
// },
vueI18n: './vue-i18n.options.ts'
},
site: {
url: 'http://localhost:3000'
}
})
5 changes: 3 additions & 2 deletions playground/pages/blog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ defineI18nRoute({
}
})

const { t, locales, baseUrl } = useI18n({
const { t, locales } = useI18n({
useScope: 'local'
})
console.log('locales', locales, baseUrl)
const { url } = useSiteConfig()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: this should require enabling the nuxt-site-config module on the playground.

console.log('locales', locales, url)
</script>

<template>
Expand Down
45 changes: 45 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion specs/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default defineNuxtConfig({

i18n: {
lazy: false,
baseUrl: 'http://localhost:3000',
locales: [
{
code: 'en',
Expand All @@ -22,5 +21,8 @@ export default defineNuxtConfig({
defaultLocale: 'en',
detectBrowserLanguage: false,
vueI18n: './config/i18n.config.ts'
},
site: {
url: 'http://localhost:3000'
}
})
4 changes: 3 additions & 1 deletion specs/fixtures/different_domains/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default defineNuxtConfig({

i18n: {
lazy: false,
baseUrl: 'http://localhost:3000',
locales: [
{
code: 'en',
Expand All @@ -23,5 +22,8 @@ export default defineNuxtConfig({
],
defaultLocale: 'ja',
detectBrowserLanguage: false
},
site: {
url: 'http://localhost:3000'
}
})
4 changes: 3 additions & 1 deletion specs/fixtures/issues/2247/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
baseUrl: 'https://abwaab.com',
locales: [
{
code: 'en',
Expand All @@ -27,5 +26,8 @@ export default defineNuxtConfig({
defaultLocale: 'ar',
lazy: true,
langDir: 'i18n/'
},
site: {
url: 'https://abwaab.com'
}
})
4 changes: 3 additions & 1 deletion specs/fixtures/issues/2288/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
baseUrl: 'https://abwaab.com',
locales: [
{
code: 'en',
Expand All @@ -26,5 +25,8 @@ export default defineNuxtConfig({
defaultLocale: 'ar',
lazy: true,
langDir: 'i18n/'
},
site: {
url: 'https://abwaab.com'
}
})