@nuxt/docs-nightly 5.0.0-29741513.94de156f → 5.0.0-29741587.ad7d78f0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -52,3 +52,129 @@ For example, referencing an image file that will be processed if a build tool is
|
|
|
52
52
|
::note
|
|
53
53
|
Nuxt won't serve files in the [`app/assets/`](/docs/4.x/directory-structure/app/assets) directory at a static URL like `/assets/my-file.png`. If you need a static URL, use the [`public/`](/docs/4.x/getting-started/assets#public-directory) directory.
|
|
54
54
|
::
|
|
55
|
+
|
|
56
|
+
### Static vs. Dynamic `src`
|
|
57
|
+
|
|
58
|
+
When an `src` is a static string literal in your template, the build tool rewrites it into a runtime helper that resolves the final URL. A public path such as `/img/nuxt.png` is wrapped so that your [`app.baseURL`](/docs/4.x/api/nuxt-config#baseurl) is applied when the page renders, and a bundled path such as `~/assets/img/nuxt.png` additionally becomes an import that resolves to the hashed output file.
|
|
59
|
+
|
|
60
|
+
```vue
|
|
61
|
+
<template>
|
|
62
|
+
<!-- Static paths are rewritten: app.baseURL is applied at runtime, and the bundled file is hashed. -->
|
|
63
|
+
<img src="/img/nuxt.png">
|
|
64
|
+
<img src="~/assets/img/nuxt.png">
|
|
65
|
+
</template>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Because `app.baseURL` is applied at runtime, a static public path works even when the base URL is only known at deploy time (for example set via `NUXT_APP_BASE_URL`), and it works whether or not the file is processed by the build. This resolution only happens for literal paths the build tool can see.
|
|
69
|
+
|
|
70
|
+
A bound `:src` whose value is assembled at runtime is opaque to the build tool, so none of that rewriting happens. The string is used exactly as written:
|
|
71
|
+
|
|
72
|
+
```vue
|
|
73
|
+
<template>
|
|
74
|
+
<!-- This does not work: the path is built at runtime, so Vite never sees it as an import. -->
|
|
75
|
+
<img :src="`~/assets/img/${name}.png`">
|
|
76
|
+
</template>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
A runtime-built public path like `/img/${name}.png` is therefore **not** prefixed with [`app.baseURL`](/docs/4.x/api/nuxt-config#baseurl). If your application is deployed below the origin root, prefix it yourself with [`useRuntimeConfig().app.baseURL`](/docs/4.x/api/composables/use-runtime-config) (for example via [`joinURL`](https://github.com/unjs/ufo#joinurl)).
|
|
80
|
+
|
|
81
|
+
The sections below cover how to handle each case when the path is only known at runtime.
|
|
82
|
+
|
|
83
|
+
#### Public Assets
|
|
84
|
+
|
|
85
|
+
If the files do not need to be processed or hashed, put them in the [`public/`](/docs/4.x/directory-structure/public) directory and reference them by URL:
|
|
86
|
+
|
|
87
|
+
```vue [app/app.vue]
|
|
88
|
+
<script setup lang="ts">
|
|
89
|
+
const props = defineProps<{
|
|
90
|
+
name: string
|
|
91
|
+
}>()
|
|
92
|
+
|
|
93
|
+
const imageUrl = computed(() => `/img/${props.name}.png`)
|
|
94
|
+
</script>
|
|
95
|
+
|
|
96
|
+
<template>
|
|
97
|
+
<img
|
|
98
|
+
:src="imageUrl"
|
|
99
|
+
:alt="props.name"
|
|
100
|
+
>
|
|
101
|
+
</template>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Files in `public/` keep their original filenames.
|
|
105
|
+
|
|
106
|
+
#### Bundled Assets with Vite
|
|
107
|
+
|
|
108
|
+
The approaches below are specific to Vite, Nuxt's default builder.
|
|
109
|
+
|
|
110
|
+
When the possible files are known, list their imports explicitly:
|
|
111
|
+
|
|
112
|
+
```vue [app/app.vue]
|
|
113
|
+
<script setup lang="ts">
|
|
114
|
+
const props = defineProps<{
|
|
115
|
+
theme: 'light' | 'dark'
|
|
116
|
+
}>()
|
|
117
|
+
|
|
118
|
+
const logos = {
|
|
119
|
+
light: () => import('./assets/img/logo-light.png?url'),
|
|
120
|
+
dark: () => import('./assets/img/logo-dark.png?url'),
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const logoUrl = (await logos[props.theme]()).default
|
|
124
|
+
</script>
|
|
125
|
+
|
|
126
|
+
<template>
|
|
127
|
+
<img
|
|
128
|
+
:src="logoUrl"
|
|
129
|
+
alt="Nuxt"
|
|
130
|
+
>
|
|
131
|
+
</template>
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Each import has a literal path, so Vite can find both files at build time while loading only the selected module at runtime.
|
|
135
|
+
|
|
136
|
+
When many files share a directory and extension, use a [variable dynamic import](https://vite.dev/guide/features.html#dynamic-import) instead of listing every file:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
async function getImageUrl (name: string) {
|
|
140
|
+
const image = await import(`./assets/img/${name}.png?url`)
|
|
141
|
+
return image.default
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Only the filename can be dynamic in this example. Keeping the directory and extension in the import lets Vite find the possible files at build time.
|
|
146
|
+
|
|
147
|
+
For a broader pattern or an explicit map of available files, use [`import.meta.glob`](https://vite.dev/guide/features.html#glob-import):
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const images = import.meta.glob<string>('./assets/img/*.{png,jpg,svg}', {
|
|
151
|
+
query: '?url',
|
|
152
|
+
import: 'default',
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
async function getImageUrl (name: string) {
|
|
156
|
+
const load = images[`./assets/img/${name}.png`]
|
|
157
|
+
|
|
158
|
+
if (!load) {
|
|
159
|
+
throw new Error(`Unknown image: ${name}`)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return await load()
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Glob imports are lazy by default. Add `eager: true` if the URLs must be available synchronously:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
const images = import.meta.glob<string>('./assets/img/*.{png,jpg,svg}', {
|
|
170
|
+
query: '?url',
|
|
171
|
+
import: 'default',
|
|
172
|
+
eager: true,
|
|
173
|
+
})
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Every matching asset is still included in the build output. Lazy imports load each match on demand, while an eager glob loads all matches up front and can increase the initial JavaScript size or inline small assets.
|
|
177
|
+
|
|
178
|
+
::warning
|
|
179
|
+
Await a lazy import before using its URL in server-rendered markup. Vite's [`new URL(..., import.meta.url)` pattern](https://vite.dev/guide/assets.html#new-url-url-import-meta-url) does not work with SSR.
|
|
180
|
+
::
|
|
@@ -41,7 +41,7 @@ When your module needs to perform one-time setup tasks (like generating configur
|
|
|
41
41
|
|
|
42
42
|
```ts
|
|
43
43
|
import { addServerHandler, defineNuxtModule } from 'nuxt/kit'
|
|
44
|
-
import
|
|
44
|
+
import { isLess } from 'verkit'
|
|
45
45
|
|
|
46
46
|
export default defineNuxtModule({
|
|
47
47
|
meta: {
|
|
@@ -54,7 +54,7 @@ export default defineNuxtModule({
|
|
|
54
54
|
},
|
|
55
55
|
async onUpgrade (nuxt, options, previousVersion) {
|
|
56
56
|
// Handle version-specific migrations
|
|
57
|
-
if (
|
|
57
|
+
if (isLess(previousVersion, '1.0.0')) {
|
|
58
58
|
await migrateLegacyData()
|
|
59
59
|
}
|
|
60
60
|
},
|
|
@@ -61,6 +61,10 @@ Setting the default of `runtimeConfig` values to *differently named environment
|
|
|
61
61
|
It is advised to use environment variables that match the structure of your `runtimeConfig` object.
|
|
62
62
|
::
|
|
63
63
|
|
|
64
|
+
::warning
|
|
65
|
+
Environment variable values are automatically cast to their JavaScript type using [`destr`](https://github.com/unjs/destr). For example, `NUXT_MY_VAR=4848e0` becomes the number `4848`. To keep a value a string, the environment variable value itself must contain literal double quotes: in a `.env` file, write `NUXT_MY_VAR='"4848e0"'`; when setting the variable directly (in a shell, Dockerfile, or hosting dashboard), make sure the quotes are part of the value and not stripped by the shell (for example `NUXT_MY_VAR='"4848e0"' node .output/server/index.mjs`).
|
|
66
|
+
::
|
|
67
|
+
|
|
64
68
|
::tip{icon="i-lucide-video" to="https://youtu.be/_FYV5WfiWvs" target="_blank"}
|
|
65
69
|
Watch a video from Alexander Lichter showcasing the top mistake developers make using runtimeConfig.
|
|
66
70
|
::
|
package/4.api/5.kit/1.modules.md
CHANGED
|
@@ -205,7 +205,7 @@ Lifecycle hooks run before the main `setup` function, and if a hook throws an er
|
|
|
205
205
|
|
|
206
206
|
```ts
|
|
207
207
|
import { defineNuxtModule } from '@nuxt/kit'
|
|
208
|
-
import
|
|
208
|
+
import { isLess } from 'verkit'
|
|
209
209
|
|
|
210
210
|
export default defineNuxtModule({
|
|
211
211
|
meta: {
|
|
@@ -239,7 +239,7 @@ export default defineNuxtModule({
|
|
|
239
239
|
// - Clean up deprecated files
|
|
240
240
|
// - Display upgrade notes
|
|
241
241
|
|
|
242
|
-
if (
|
|
242
|
+
if (isLess(previousVersion, '1.1.0')) {
|
|
243
243
|
console.log('⚠️ Breaking changes in 1.1.0 - please check the migration guide')
|
|
244
244
|
}
|
|
245
245
|
},
|