@nuxt/docs 0.0.0 → 3.17.0

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.
Files changed (224) hide show
  1. package/.navigation.yml +2 -0
  2. package/1.getting-started/.navigation.yml +3 -0
  3. package/1.getting-started/01.introduction.md +81 -0
  4. package/1.getting-started/02.installation.md +109 -0
  5. package/1.getting-started/03.configuration.md +226 -0
  6. package/1.getting-started/04.views.md +163 -0
  7. package/1.getting-started/05.assets.md +48 -0
  8. package/1.getting-started/06.styling.md +565 -0
  9. package/1.getting-started/07.routing.md +149 -0
  10. package/1.getting-started/08.seo-meta.md +360 -0
  11. package/1.getting-started/09.transitions.md +473 -0
  12. package/1.getting-started/10.data-fetching.md +795 -0
  13. package/1.getting-started/11.state-management.md +223 -0
  14. package/1.getting-started/12.error-handling.md +233 -0
  15. package/1.getting-started/13.server.md +94 -0
  16. package/1.getting-started/14.layers.md +92 -0
  17. package/1.getting-started/15.prerendering.md +194 -0
  18. package/1.getting-started/16.deployment.md +130 -0
  19. package/1.getting-started/17.testing.md +728 -0
  20. package/1.getting-started/18.upgrade.md +997 -0
  21. package/2.guide/.navigation.yml +2 -0
  22. package/2.guide/0.index.md +22 -0
  23. package/2.guide/1.concepts/.navigation.yml +3 -0
  24. package/2.guide/1.concepts/1.auto-imports.md +205 -0
  25. package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
  26. package/2.guide/1.concepts/2.vuejs-development.md +103 -0
  27. package/2.guide/1.concepts/3.rendering.md +255 -0
  28. package/2.guide/1.concepts/4.server-engine.md +62 -0
  29. package/2.guide/1.concepts/5.modules.md +48 -0
  30. package/2.guide/1.concepts/7.esm.md +299 -0
  31. package/2.guide/1.concepts/8.typescript.md +97 -0
  32. package/2.guide/1.concepts/9.code-style.md +22 -0
  33. package/2.guide/2.directory-structure/.navigation.yml +3 -0
  34. package/2.guide/2.directory-structure/0.nuxt.md +20 -0
  35. package/2.guide/2.directory-structure/0.output.md +18 -0
  36. package/2.guide/2.directory-structure/1.assets.md +16 -0
  37. package/2.guide/2.directory-structure/1.components.md +608 -0
  38. package/2.guide/2.directory-structure/1.composables.md +121 -0
  39. package/2.guide/2.directory-structure/1.content.md +64 -0
  40. package/2.guide/2.directory-structure/1.layouts.md +180 -0
  41. package/2.guide/2.directory-structure/1.middleware.md +209 -0
  42. package/2.guide/2.directory-structure/1.modules.md +66 -0
  43. package/2.guide/2.directory-structure/1.node_modules.md +12 -0
  44. package/2.guide/2.directory-structure/1.pages.md +440 -0
  45. package/2.guide/2.directory-structure/1.plugins.md +299 -0
  46. package/2.guide/2.directory-structure/1.public.md +27 -0
  47. package/2.guide/2.directory-structure/1.server.md +546 -0
  48. package/2.guide/2.directory-structure/1.shared.md +104 -0
  49. package/2.guide/2.directory-structure/1.utils.md +49 -0
  50. package/2.guide/2.directory-structure/2.env.md +75 -0
  51. package/2.guide/2.directory-structure/2.gitignore.md +37 -0
  52. package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
  53. package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
  54. package/2.guide/2.directory-structure/3.app-config.md +177 -0
  55. package/2.guide/2.directory-structure/3.app.md +72 -0
  56. package/2.guide/2.directory-structure/3.error.md +55 -0
  57. package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
  58. package/2.guide/2.directory-structure/3.package.md +32 -0
  59. package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
  60. package/2.guide/3.going-further/.navigation.yml +3 -0
  61. package/2.guide/3.going-further/1.experimental-features.md +689 -0
  62. package/2.guide/3.going-further/1.features.md +103 -0
  63. package/2.guide/3.going-further/1.internals.md +81 -0
  64. package/2.guide/3.going-further/10.runtime-config.md +174 -0
  65. package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
  66. package/2.guide/3.going-further/2.hooks.md +98 -0
  67. package/2.guide/3.going-further/3.modules.md +811 -0
  68. package/2.guide/3.going-further/4.kit.md +51 -0
  69. package/2.guide/3.going-further/6.nuxt-app.md +64 -0
  70. package/2.guide/3.going-further/7.layers.md +227 -0
  71. package/2.guide/3.going-further/9.debugging.md +115 -0
  72. package/2.guide/3.going-further/index.md +4 -0
  73. package/2.guide/4.recipes/.navigation.yml +3 -0
  74. package/2.guide/4.recipes/1.custom-routing.md +181 -0
  75. package/2.guide/4.recipes/2.vite-plugin.md +65 -0
  76. package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
  77. package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
  78. package/3.api/.navigation.yml +3 -0
  79. package/3.api/1.components/.navigation.yml +3 -0
  80. package/3.api/1.components/1.client-only.md +76 -0
  81. package/3.api/1.components/1.dev-only.md +51 -0
  82. package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
  83. package/3.api/1.components/10.nuxt-picture.md +27 -0
  84. package/3.api/1.components/11.teleports.md +40 -0
  85. package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
  86. package/3.api/1.components/13.nuxt-time.md +173 -0
  87. package/3.api/1.components/2.nuxt-page.md +154 -0
  88. package/3.api/1.components/3.nuxt-layout.md +156 -0
  89. package/3.api/1.components/4.nuxt-link.md +322 -0
  90. package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
  91. package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
  92. package/3.api/1.components/7.nuxt-welcome.md +25 -0
  93. package/3.api/1.components/8.nuxt-island.md +70 -0
  94. package/3.api/1.components/9.nuxt-img.md +43 -0
  95. package/3.api/2.composables/.navigation.yml +3 -0
  96. package/3.api/2.composables/on-prehydrate.md +60 -0
  97. package/3.api/2.composables/use-app-config.md +19 -0
  98. package/3.api/2.composables/use-async-data.md +212 -0
  99. package/3.api/2.composables/use-cookie.md +233 -0
  100. package/3.api/2.composables/use-error.md +32 -0
  101. package/3.api/2.composables/use-fetch.md +217 -0
  102. package/3.api/2.composables/use-head-safe.md +55 -0
  103. package/3.api/2.composables/use-head.md +69 -0
  104. package/3.api/2.composables/use-hydration.md +68 -0
  105. package/3.api/2.composables/use-lazy-async-data.md +47 -0
  106. package/3.api/2.composables/use-lazy-fetch.md +55 -0
  107. package/3.api/2.composables/use-loading-indicator.md +77 -0
  108. package/3.api/2.composables/use-nuxt-app.md +294 -0
  109. package/3.api/2.composables/use-nuxt-data.md +112 -0
  110. package/3.api/2.composables/use-preview-mode.md +118 -0
  111. package/3.api/2.composables/use-request-event.md +23 -0
  112. package/3.api/2.composables/use-request-fetch.md +52 -0
  113. package/3.api/2.composables/use-request-header.md +34 -0
  114. package/3.api/2.composables/use-request-headers.md +37 -0
  115. package/3.api/2.composables/use-request-url.md +41 -0
  116. package/3.api/2.composables/use-response-header.md +48 -0
  117. package/3.api/2.composables/use-route-announcer.md +60 -0
  118. package/3.api/2.composables/use-route.md +52 -0
  119. package/3.api/2.composables/use-router.md +92 -0
  120. package/3.api/2.composables/use-runtime-config.md +142 -0
  121. package/3.api/2.composables/use-runtime-hook.md +43 -0
  122. package/3.api/2.composables/use-seo-meta.md +80 -0
  123. package/3.api/2.composables/use-server-seo-meta.md +27 -0
  124. package/3.api/2.composables/use-state.md +48 -0
  125. package/3.api/3.utils/$fetch.md +98 -0
  126. package/3.api/3.utils/.navigation.yml +3 -0
  127. package/3.api/3.utils/abort-navigation.md +73 -0
  128. package/3.api/3.utils/add-route-middleware.md +88 -0
  129. package/3.api/3.utils/call-once.md +92 -0
  130. package/3.api/3.utils/clear-error.md +29 -0
  131. package/3.api/3.utils/clear-nuxt-data.md +23 -0
  132. package/3.api/3.utils/clear-nuxt-state.md +23 -0
  133. package/3.api/3.utils/create-error.md +55 -0
  134. package/3.api/3.utils/define-nuxt-component.md +53 -0
  135. package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
  136. package/3.api/3.utils/define-page-meta.md +234 -0
  137. package/3.api/3.utils/define-route-rules.md +52 -0
  138. package/3.api/3.utils/navigate-to.md +230 -0
  139. package/3.api/3.utils/on-before-route-leave.md +11 -0
  140. package/3.api/3.utils/on-before-route-update.md +11 -0
  141. package/3.api/3.utils/on-nuxt-ready.md +25 -0
  142. package/3.api/3.utils/prefetch-components.md +28 -0
  143. package/3.api/3.utils/preload-components.md +23 -0
  144. package/3.api/3.utils/preload-route-components.md +41 -0
  145. package/3.api/3.utils/prerender-routes.md +46 -0
  146. package/3.api/3.utils/refresh-cookie.md +46 -0
  147. package/3.api/3.utils/refresh-nuxt-data.md +91 -0
  148. package/3.api/3.utils/reload-nuxt-app.md +74 -0
  149. package/3.api/3.utils/set-page-layout.md +24 -0
  150. package/3.api/3.utils/set-response-status.md +36 -0
  151. package/3.api/3.utils/show-error.md +31 -0
  152. package/3.api/3.utils/update-app-config.md +27 -0
  153. package/3.api/4.commands/.navigation.yml +3 -0
  154. package/3.api/4.commands/add.md +112 -0
  155. package/3.api/4.commands/analyze.md +41 -0
  156. package/3.api/4.commands/build-module.md +42 -0
  157. package/3.api/4.commands/build.md +46 -0
  158. package/3.api/4.commands/cleanup.md +38 -0
  159. package/3.api/4.commands/dev.md +59 -0
  160. package/3.api/4.commands/devtools.md +38 -0
  161. package/3.api/4.commands/generate.md +41 -0
  162. package/3.api/4.commands/info.md +33 -0
  163. package/3.api/4.commands/init.md +46 -0
  164. package/3.api/4.commands/module.md +84 -0
  165. package/3.api/4.commands/prepare.md +36 -0
  166. package/3.api/4.commands/preview.md +43 -0
  167. package/3.api/4.commands/typecheck.md +42 -0
  168. package/3.api/4.commands/upgrade.md +37 -0
  169. package/3.api/5.kit/.navigation.yml +3 -0
  170. package/3.api/5.kit/1.modules.md +172 -0
  171. package/3.api/5.kit/10.runtime-config.md +27 -0
  172. package/3.api/5.kit/10.templates.md +283 -0
  173. package/3.api/5.kit/11.nitro.md +409 -0
  174. package/3.api/5.kit/12.resolving.md +268 -0
  175. package/3.api/5.kit/13.logging.md +65 -0
  176. package/3.api/5.kit/14.builder.md +491 -0
  177. package/3.api/5.kit/15.examples.md +41 -0
  178. package/3.api/5.kit/2.programmatic.md +125 -0
  179. package/3.api/5.kit/3.compatibility.md +230 -0
  180. package/3.api/5.kit/4.autoimports.md +144 -0
  181. package/3.api/5.kit/5.components.md +127 -0
  182. package/3.api/5.kit/6.context.md +130 -0
  183. package/3.api/5.kit/7.pages.md +295 -0
  184. package/3.api/5.kit/8.layout.md +80 -0
  185. package/3.api/5.kit/9.plugins.md +263 -0
  186. package/3.api/6.advanced/.navigation.yml +1 -0
  187. package/3.api/6.advanced/1.hooks.md +105 -0
  188. package/3.api/6.advanced/2.import-meta.md +60 -0
  189. package/3.api/6.nuxt-config.md +12 -0
  190. package/3.api/index.md +31 -0
  191. package/5.community/.navigation.yml +3 -0
  192. package/5.community/2.getting-help.md +48 -0
  193. package/5.community/3.reporting-bugs.md +50 -0
  194. package/5.community/4.contribution.md +205 -0
  195. package/5.community/5.framework-contribution.md +142 -0
  196. package/5.community/6.roadmap.md +79 -0
  197. package/5.community/7.changelog.md +92 -0
  198. package/6.bridge/.navigation.yml +3 -0
  199. package/6.bridge/1.overview.md +137 -0
  200. package/6.bridge/10.configuration.md +96 -0
  201. package/6.bridge/2.typescript.md +46 -0
  202. package/6.bridge/3.bridge-composition-api.md +132 -0
  203. package/6.bridge/4.plugins-and-middleware.md +65 -0
  204. package/6.bridge/5.nuxt3-compatible-api.md +204 -0
  205. package/6.bridge/6.meta.md +117 -0
  206. package/6.bridge/7.runtime-config.md +38 -0
  207. package/6.bridge/8.nitro.md +102 -0
  208. package/6.bridge/9.vite.md +37 -0
  209. package/7.migration/.navigation.yml +3 -0
  210. package/7.migration/1.overview.md +24 -0
  211. package/7.migration/10.bundling.md +28 -0
  212. package/7.migration/11.server.md +17 -0
  213. package/7.migration/2.configuration.md +240 -0
  214. package/7.migration/20.module-authors.md +94 -0
  215. package/7.migration/3.auto-imports.md +18 -0
  216. package/7.migration/4.meta.md +127 -0
  217. package/7.migration/5.plugins-and-middleware.md +80 -0
  218. package/7.migration/6.pages-and-layouts.md +233 -0
  219. package/7.migration/7.component-options.md +156 -0
  220. package/7.migration/8.runtime-config.md +58 -0
  221. package/LICENSE +21 -0
  222. package/README.md +11 -0
  223. package/package.json +16 -4
  224. package/dist/.gitkeep +0 -0
@@ -0,0 +1,565 @@
1
+ ---
2
+ title: 'Styling'
3
+ description: 'Learn how to style your Nuxt application.'
4
+ navigation.icon: i-lucide-palette
5
+ ---
6
+
7
+ Nuxt is highly flexible when it comes to styling. Write your own styles, or reference local and external stylesheets.
8
+ You can use CSS preprocessors, CSS frameworks, UI libraries and Nuxt modules to style your application.
9
+
10
+ ## Local Stylesheets
11
+
12
+ If you're writing local stylesheets, the natural place to put them is the [`assets/` directory](/docs/guide/directory-structure/assets).
13
+
14
+ ### Importing Within Components
15
+
16
+ You can import stylesheets in your pages, layouts and components directly.
17
+ You can use a JavaScript import, or a CSS [`@import` statement](https://developer.mozilla.org/en-US/docs/Web/CSS/@import).
18
+
19
+ ```vue [pages/index.vue]
20
+ <script>
21
+ // Use a static import for server-side compatibility
22
+ import '~/assets/css/first.css'
23
+
24
+ // Caution: Dynamic imports are not server-side compatible
25
+ import('~/assets/css/first.css')
26
+ </script>
27
+
28
+ <style>
29
+ @import url("~/assets/css/second.css");
30
+ </style>
31
+ ```
32
+
33
+ ::tip
34
+ The stylesheets will be inlined in the HTML rendered by Nuxt.
35
+ ::
36
+
37
+ ### The CSS Property
38
+
39
+ You can also use the `css` property in the Nuxt configuration.
40
+ The natural place for your stylesheets is the [`assets/` directory](/docs/guide/directory-structure/assets). You can then reference its path and Nuxt will include it to all the pages of your application.
41
+
42
+ ```ts [nuxt.config.ts]
43
+ export default defineNuxtConfig({
44
+ css: ['~/assets/css/main.css']
45
+ })
46
+ ```
47
+
48
+ ::tip
49
+ The stylesheets will be inlined in the HTML rendered by Nuxt, injected globally and present in all pages.
50
+ ::
51
+
52
+ ### Working With Fonts
53
+
54
+ Place your local fonts files in your `~/public/` directory, for example in `~/public/fonts`. You can then reference them in your stylesheets using `url()`.
55
+
56
+ ```css [assets/css/main.css]
57
+ @font-face {
58
+ font-family: 'FarAwayGalaxy';
59
+ src: url('/fonts/FarAwayGalaxy.woff') format('woff');
60
+ font-weight: normal;
61
+ font-style: normal;
62
+ font-display: swap;
63
+ }
64
+ ```
65
+
66
+ Then reference your fonts by name in your stylesheets, pages or components:
67
+
68
+ ```vue
69
+ <style>
70
+ h1 {
71
+ font-family: 'FarAwayGalaxy', sans-serif;
72
+ }
73
+ </style>
74
+ ```
75
+
76
+ ### Stylesheets Distributed Through NPM
77
+
78
+ You can also reference stylesheets that are distributed through npm. Let's use the popular `animate.css` library as an example.
79
+
80
+ ::code-group{sync="pm"}
81
+
82
+ ```bash [npm]
83
+ npm install animate.css
84
+ ```
85
+
86
+ ```bash [yarn]
87
+ yarn add animate.css
88
+ ```
89
+
90
+ ```bash [pnpm]
91
+ pnpm install animate.css
92
+ ```
93
+
94
+ ```bash [bun]
95
+ bun install animate.css
96
+ ```
97
+
98
+ ::
99
+
100
+ Then you can reference it directly in your pages, layouts and components:
101
+
102
+ ```vue [app.vue]
103
+ <script>
104
+ import 'animate.css'
105
+ </script>
106
+
107
+ <style>
108
+ @import url("animate.css");
109
+ </style>
110
+ ```
111
+
112
+ The package can also be referenced as a string in the css property of your Nuxt configuration.
113
+
114
+ ```ts [nuxt.config.ts]
115
+ export default defineNuxtConfig({
116
+ css: ['animate.css']
117
+ })
118
+ ```
119
+
120
+ ## External Stylesheets
121
+
122
+ You can include external stylesheets in your application by adding a link element in the head section of your nuxt.config file. You can achieve this result using different methods. Note that local stylesheets can also be included like this.
123
+
124
+ You can manipulate the head with the [`app.head`](/docs/api/nuxt-config#head) property of your Nuxt configuration:
125
+
126
+ ```ts twoslash [nuxt.config.ts]
127
+ export default defineNuxtConfig({
128
+ app: {
129
+ head: {
130
+ link: [{ rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css' }]
131
+ }
132
+ }
133
+ })
134
+ ```
135
+
136
+ ### Dynamically Adding Stylesheets
137
+
138
+ You can use the useHead composable to dynamically set a value in your head in your code.
139
+
140
+ :read-more{to="/docs/api/composables/use-head"}
141
+
142
+ ```ts twoslash
143
+ useHead({
144
+ link: [{ rel: 'stylesheet', href: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css' }]
145
+ })
146
+ ```
147
+
148
+ Nuxt uses `unhead` under the hood, and you can refer to its full documentation [here](https://unhead.unjs.io).
149
+
150
+ ### Modifying The Rendered Head With A Nitro Plugin
151
+
152
+ If you need more advanced control, you can intercept the rendered html with a hook and modify the head programmatically.
153
+
154
+ Create a plugin in `~/server/plugins/my-plugin.ts` like this:
155
+
156
+ ```ts twoslash [server/plugins/my-plugin.ts]
157
+ export default defineNitroPlugin((nitro) => {
158
+ nitro.hooks.hook('render:html', (html) => {
159
+ html.head.push('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">')
160
+ })
161
+ })
162
+ ```
163
+
164
+ External stylesheets are render-blocking resources: they must be loaded and processed before the browser renders the page. Web pages that contain unnecessarily large styles take longer to render. You can read more about it on [web.dev](https://web.dev/defer-non-critical-css).
165
+
166
+ ## Using Preprocessors
167
+
168
+ To use a preprocessor like SCSS, Sass, Less or Stylus, install it first.
169
+
170
+ ::code-group
171
+
172
+ ```bash [Sass & SCSS]
173
+ npm install -D sass
174
+ ```
175
+
176
+ ```bash [Less]
177
+ npm install -D less
178
+ ```
179
+
180
+ ```bash [Stylus]
181
+ npm install -D stylus
182
+ ```
183
+
184
+ ::
185
+
186
+ The natural place to write your stylesheets is the `assets` directory.
187
+ You can then import your source files in your `app.vue` (or layouts files) using your preprocessor's syntax.
188
+
189
+ ```vue [pages/app.vue]
190
+ <style lang="scss">
191
+ @use "~/assets/scss/main.scss";
192
+ </style>
193
+ ```
194
+
195
+ Alternatively, you can use the `css` property of your Nuxt configuration.
196
+
197
+ ```ts twoslash [nuxt.config.ts]
198
+ export default defineNuxtConfig({
199
+ css: ['~/assets/scss/main.scss']
200
+ })
201
+ ```
202
+
203
+ ::tip
204
+ In both cases, the compiled stylesheets will be inlined in the HTML rendered by Nuxt.
205
+ ::
206
+
207
+ If you need to inject code in pre-processed files, like a [Sass partial](https://sass-lang.com/documentation/at-rules/use#partials) with color variables, you can do so with the Vite [preprocessors options](https://vite.dev/config/shared-options.html#css-preprocessoroptions).
208
+
209
+ Create some partials in your `assets` directory:
210
+
211
+ ::code-group{sync="preprocessor"}
212
+
213
+ ```scss [assets/_colors.scss]
214
+ $primary: #49240F;
215
+ $secondary: #E4A79D;
216
+ ```
217
+
218
+ ```sass [assets/_colors.sass]
219
+ $primary: #49240F
220
+ $secondary: #E4A79D
221
+ ```
222
+
223
+ ::
224
+
225
+ Then in your `nuxt.config` :
226
+
227
+ ::code-group
228
+
229
+ ```ts twoslash [SCSS]
230
+ export default defineNuxtConfig({
231
+ vite: {
232
+ css: {
233
+ preprocessorOptions: {
234
+ scss: {
235
+ additionalData: '@use "~/assets/_colors.scss" as *;'
236
+ }
237
+ }
238
+ }
239
+ }
240
+ })
241
+ ```
242
+
243
+ ```ts twoslash [SASS]
244
+ export default defineNuxtConfig({
245
+ vite: {
246
+ css: {
247
+ preprocessorOptions: {
248
+ sass: {
249
+ additionalData: '@use "~/assets/_colors.sass" as *\n'
250
+ }
251
+ }
252
+ }
253
+ }
254
+ })
255
+ ```
256
+
257
+ ::
258
+
259
+ Nuxt uses Vite by default. If you wish to use webpack instead, refer to each preprocessor loader [documentation](https://webpack.js.org/loaders/sass-loader).
260
+
261
+ ### Preprocessor Workers (Experimental)
262
+
263
+ Vite has made available an [experimental option](https://vite.dev/config/shared-options.html#css-preprocessormaxworkers) which can speed up using preprocessors.
264
+
265
+ You can enable this in your `nuxt.config`:
266
+
267
+ ```ts
268
+
269
+ export default defineNuxtConfig({
270
+ vite: {
271
+ css: {
272
+ preprocessorMaxWorkers: true // number of CPUs minus 1
273
+ }
274
+ }
275
+ })
276
+ ```
277
+
278
+ ::note
279
+ This is an experimental option and you should refer to the Vite documentation and [provide feedback](https://github.com/vitejs/vite/discussions/15835).
280
+ ::
281
+
282
+ ## Single File Components (SFC) Styling
283
+
284
+ One of the best things about Vue and SFC is how great it is at naturally dealing with styling. You can directly write CSS or preprocessor code in the style block of your components file, therefore you will have fantastic developer experience without having to use something like CSS-in-JS. However if you wish to use CSS-in-JS, you can find 3rd party libraries and modules that support it, such as [pinceau](https://github.com/Tahul/pinceau).
285
+
286
+ You can refer to the [Vue docs](https://vuejs.org/api/sfc-css-features.html) for a comprehensive reference about styling components in SFC.
287
+
288
+ ### Class And Style Bindings
289
+
290
+ You can leverage Vue SFC features to style your components with class and style attributes.
291
+
292
+ ::code-group
293
+
294
+ ```vue [Ref and Reactive]
295
+ <script setup lang="ts">
296
+ const isActive = ref(true)
297
+ const hasError = ref(false)
298
+ const classObject = reactive({
299
+ active: true,
300
+ 'text-danger': false
301
+ })
302
+ </script>
303
+
304
+ <template>
305
+ <div class="static" :class="{ active: isActive, 'text-danger': hasError }"></div>
306
+ <div :class="classObject"></div>
307
+ </template>
308
+ ```
309
+
310
+ ```vue [Computed]
311
+ <script setup lang="ts">
312
+ const isActive = ref(true)
313
+ const error = ref(null)
314
+
315
+ const classObject = computed(() => ({
316
+ active: isActive.value && !error.value,
317
+ 'text-danger': error.value && error.value.type === 'fatal'
318
+ }))
319
+ </script>
320
+
321
+ <template>
322
+ <div :class="classObject"></div>
323
+ </template>
324
+ ```
325
+
326
+ ```vue [Array]
327
+ <script setup lang="ts">
328
+ const isActive = ref(true)
329
+ const errorClass = ref('text-danger')
330
+ </script>
331
+
332
+ <template>
333
+ <div :class="[{ active: isActive }, errorClass]"></div>
334
+ </template>
335
+ ```
336
+
337
+ ```vue [Style]
338
+ <script setup lang="ts">
339
+ const activeColor = ref('red')
340
+ const fontSize = ref(30)
341
+ const styleObject = reactive({ color: 'red', fontSize: '13px' })
342
+ </script>
343
+
344
+ <template>
345
+ <div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
346
+ <div :style="[baseStyles, overridingStyles]"></div>
347
+ <div :style="styleObject"></div>
348
+ </template>
349
+ ```
350
+
351
+ ::
352
+
353
+ Refer to the [Vue docs](https://vuejs.org/guide/essentials/class-and-style.html) for more information.
354
+
355
+ ### Dynamic Styles With `v-bind`
356
+
357
+ You can reference JavaScript variable and expression within your style blocks with the v-bind function.
358
+ The binding will be dynamic, meaning that if the variable value changes, the style will be updated.
359
+
360
+ ```vue
361
+ <script setup lang="ts">
362
+ const color = ref("red")
363
+ </script>
364
+
365
+ <template>
366
+ <div class="text">hello</div>
367
+ </template>
368
+
369
+ <style>
370
+ .text {
371
+ color: v-bind(color);
372
+ }
373
+ </style>
374
+ ```
375
+
376
+ ### Scoped Styles
377
+
378
+ The scoped attribute allows you to style components in isolation. The styles declared with this attribute will only apply to this component.
379
+
380
+ ```vue
381
+ <template>
382
+ <div class="example">hi</div>
383
+ </template>
384
+
385
+ <style scoped>
386
+ .example {
387
+ color: red;
388
+ }
389
+ </style>
390
+ ```
391
+
392
+ ### CSS Modules
393
+
394
+ You can use [CSS Modules](https://github.com/css-modules/css-modules) with the module attribute. Access it with the injected `$style` variable.
395
+
396
+ ```vue
397
+ <template>
398
+ <p :class="$style.red">This should be red</p>
399
+ </template>
400
+
401
+ <style module>
402
+ .red {
403
+ color: red;
404
+ }
405
+ </style>
406
+ ```
407
+
408
+ ### Preprocessors Support
409
+
410
+ SFC style blocks support preprocessors syntax. Vite come with built-in support for .scss, .sass, .less, .styl and .stylus files without configuration. You just need to install them first, and they will be available directly in SFC with the lang attribute.
411
+
412
+ ::code-group
413
+
414
+ ```vue [SCSS]
415
+ <style lang="scss">
416
+ /* Write scss here */
417
+ </style>
418
+ ```
419
+
420
+ ```vue [Sass]
421
+ <style lang="sass">
422
+ /* Write sass here */
423
+ </style>
424
+ ```
425
+
426
+ ```vue [LESS]
427
+ <style lang="less">
428
+ /* Write less here */
429
+ </style>
430
+ ```
431
+
432
+ ```vue [Stylus]
433
+ <style lang="stylus">
434
+ /* Write stylus here */
435
+ </style>
436
+ ```
437
+
438
+ ::
439
+
440
+ You can refer to the [Vite CSS docs](https://vite.dev/guide/features.html#css) and the [@vitejs/plugin-vue docs](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue).
441
+ For webpack users, refer to the [vue loader docs](https://vue-loader.vuejs.org).
442
+
443
+ ## Using PostCSS
444
+
445
+ Nuxt comes with postcss built-in. You can configure it in your `nuxt.config` file.
446
+
447
+ ```ts [nuxt.config.ts]
448
+ export default defineNuxtConfig({
449
+ postcss: {
450
+ plugins: {
451
+ 'postcss-nested': {},
452
+ 'postcss-custom-media': {}
453
+ }
454
+ }
455
+ })
456
+ ```
457
+
458
+ For proper syntax highlighting in SFC, you can use the postcss lang attribute.
459
+
460
+ ```vue
461
+ <style lang="postcss">
462
+ /* Write postcss here */
463
+ </style>
464
+ ```
465
+
466
+ By default, Nuxt comes with the following plugins already pre-configured:
467
+
468
+ - [postcss-import](https://github.com/postcss/postcss-import): Improves the `@import` rule
469
+ - [postcss-url](https://github.com/postcss/postcss-url): Transforms `url()` statements
470
+ - [autoprefixer](https://github.com/postcss/autoprefixer): Automatically adds vendor prefixes
471
+ - [cssnano](https://cssnano.github.io/cssnano): Minification and purge
472
+
473
+ ## Leveraging Layouts For Multiple Styles
474
+
475
+ If you need to style different parts of your application completely differently, you can use layouts.
476
+ Use different styles for different layouts.
477
+
478
+ ```vue
479
+ <template>
480
+ <div class="default-layout">
481
+ <h1>Default Layout</h1>
482
+ <slot />
483
+ </div>
484
+ </template>
485
+
486
+ <style>
487
+ .default-layout {
488
+ color: red;
489
+ }
490
+ </style>
491
+ ```
492
+
493
+ :read-more{to="/docs/guide/directory-structure/layouts"}
494
+
495
+ ## Third Party Libraries And Modules
496
+
497
+ Nuxt isn't opinionated when it comes to styling and provides you with a wide variety of options. You can use any styling tool that you want, such as popular libraries like [UnoCSS](https://unocss.dev) or [Tailwind CSS](https://tailwindcss.com).
498
+
499
+ The community and the Nuxt team have developed plenty of Nuxt modules to make the integration easier.
500
+ You can discover them on the [modules section](/modules) of the website.
501
+ Here are a few modules to help you get started:
502
+
503
+ - [UnoCSS](/modules/unocss): Instant on-demand atomic CSS engine
504
+ - [Tailwind CSS](/modules/tailwindcss): Utility-first CSS framework
505
+ - [Fontaine](https://github.com/nuxt-modules/fontaine): Font metric fallback
506
+ - [Pinceau](https://github.com/Tahul/pinceau): Adaptable styling framework
507
+ - [Nuxt UI](https://ui.nuxt.com): A UI Library for Modern Web Apps
508
+ - [Panda CSS](https://panda-css.com/docs/installation/nuxt): CSS-in-JS engine that generates atomic CSS at build time
509
+
510
+ Nuxt modules provide you with a good developer experience out of the box, but remember that if your favorite tool doesn't have a module, it doesn't mean that you can't use it with Nuxt! You can configure it yourself for your own project. Depending on the tool, you might need to use a [Nuxt plugin](/docs/guide/directory-structure/plugins) and/or [make your own module](/docs/guide/going-further/modules). Share them with the [community](/modules) if you do!
511
+
512
+ ### Easily Load Webfonts
513
+
514
+ You can use [the Nuxt Google Fonts module](https://github.com/nuxt-modules/google-fonts) to load Google Fonts.
515
+
516
+ If you are using [UnoCSS](https://unocss.dev/integrations/nuxt), note that it comes with a [web fonts presets](https://unocss.dev/presets/web-fonts) to conveniently load fonts from common providers, including Google Fonts and more.
517
+
518
+ ## Advanced
519
+
520
+ ### Transitions
521
+
522
+ Nuxt comes with the same `<Transition>` element that Vue has, and also has support for the experimental [View Transitions API](/docs/getting-started/transitions#view-transitions-api-experimental).
523
+
524
+ :read-more{to="/docs/getting-started/transitions"}
525
+
526
+ ### Font Advanced Optimization
527
+
528
+ We would recommend using [Fontaine](https://github.com/nuxt-modules/fontaine) to reduce your [CLS](https://web.dev/cls). If you need something more advanced, consider creating a Nuxt module to extend the build process or the Nuxt runtime.
529
+
530
+ ::tip
531
+ Always remember to take advantage of the various tools and techniques available in the Web ecosystem at large to make styling your application easier and more efficient. Whether you're using native CSS, a preprocessor, postcss, a UI library or a module, Nuxt has got you covered. Happy styling!
532
+ ::
533
+
534
+ ### LCP Advanced Optimizations
535
+
536
+ You can do the following to speed-up the download of your global CSS files:
537
+
538
+ - Use a CDN so the files are physically closer to your users
539
+ - Compress your assets, ideally using Brotli
540
+ - Use HTTP2/HTTP3 for delivery
541
+ - Host your assets on the same domain (do not use a different subdomain)
542
+
543
+ Most of these things should be done for you automatically if you're using modern platforms like Cloudflare, Netlify or Vercel.
544
+ You can find an LCP optimization guide on [web.dev](https://web.dev/optimize-lcp).
545
+
546
+ If all of your CSS is inlined by Nuxt, you can (experimentally) completely stop external CSS files from being referenced in your rendered HTML.
547
+ You can achieve that with a hook, that you can place in a module, or in your Nuxt configuration file.
548
+
549
+ ```ts [nuxt.config.ts]
550
+ export default defineNuxtConfig({
551
+ hooks: {
552
+ 'build:manifest': (manifest) => {
553
+ // find the app entry, css list
554
+ const css = Object.values(manifest).find(options => options.isEntry)?.css
555
+ if (css) {
556
+ // start from the end of the array and go to the beginning
557
+ for (let i = css.length - 1; i >= 0; i--) {
558
+ // if it starts with 'entry', remove it from the list
559
+ if (css[i].startsWith('entry')) css.splice(i, 1)
560
+ }
561
+ }
562
+ },
563
+ },
564
+ })
565
+ ```
@@ -0,0 +1,149 @@
1
+ ---
2
+ title: 'Routing'
3
+ description: Nuxt file-system routing creates a route for every file in the pages/ directory.
4
+ navigation.icon: i-lucide-milestone
5
+ ---
6
+
7
+ One core feature of Nuxt is the file system router. Every Vue file inside the [`pages/`](/docs/guide/directory-structure/pages) directory creates a corresponding URL (or route) that displays the contents of the file. By using dynamic imports for each page, Nuxt leverages code-splitting to ship the minimum amount of JavaScript for the requested route.
8
+
9
+ ## Pages
10
+
11
+ Nuxt routing is based on [vue-router](https://router.vuejs.org) and generates the routes from every component created in the [`pages/` directory](/docs/guide/directory-structure/pages), based on their filename.
12
+
13
+ This file system routing uses naming conventions to create dynamic and nested routes:
14
+
15
+ ::code-group
16
+
17
+ ```bash [Directory Structure]
18
+ -| pages/
19
+ ---| about.vue
20
+ ---| index.vue
21
+ ---| posts/
22
+ -----| [id].vue
23
+ ```
24
+
25
+ ```json [Generated Router File]
26
+ {
27
+ "routes": [
28
+ {
29
+ "path": "/about",
30
+ "component": "pages/about.vue"
31
+ },
32
+ {
33
+ "path": "/",
34
+ "component": "pages/index.vue"
35
+ },
36
+ {
37
+ "path": "/posts/:id",
38
+ "component": "pages/posts/[id].vue"
39
+ }
40
+ ]
41
+ }
42
+ ```
43
+
44
+ ::
45
+
46
+ :read-more{to="/docs/guide/directory-structure/pages"}
47
+
48
+ ## Navigation
49
+
50
+ The [`<NuxtLink>`](/docs/api/components/nuxt-link) component links pages between them. It renders an `<a>` tag with the `href` attribute set to the route of the page. Once the application is hydrated, page transitions are performed in JavaScript by updating the browser URL. This prevents full-page refreshes and allows for animated transitions.
51
+
52
+ When a [`<NuxtLink>`](/docs/api/components/nuxt-link) enters the viewport on the client side, Nuxt will automatically prefetch components and payload (generated pages) of the linked pages ahead of time, resulting in faster navigation.
53
+
54
+ ```vue [pages/app.vue]
55
+ <template>
56
+ <header>
57
+ <nav>
58
+ <ul>
59
+ <li><NuxtLink to="/about">About</NuxtLink></li>
60
+ <li><NuxtLink to="/posts/1">Post 1</NuxtLink></li>
61
+ <li><NuxtLink to="/posts/2">Post 2</NuxtLink></li>
62
+ </ul>
63
+ </nav>
64
+ </header>
65
+ </template>
66
+ ```
67
+
68
+ :read-more{to="/docs/api/components/nuxt-link"}
69
+
70
+ ## Route Parameters
71
+
72
+ The [`useRoute()`](/docs/api/composables/use-route) composable can be used in a `<script setup>` block or a `setup()` method of a Vue component to access the current route details.
73
+
74
+ ```vue twoslash [pages/posts/[id\\].vue]
75
+ <script setup lang="ts">
76
+ const route = useRoute()
77
+
78
+ // When accessing /posts/1, route.params.id will be 1
79
+ console.log(route.params.id)
80
+ </script>
81
+ ```
82
+
83
+ :read-more{to="/docs/api/composables/use-route"}
84
+
85
+ ## Route Middleware
86
+
87
+ Nuxt provides a customizable route middleware framework you can use throughout your application, ideal for extracting code that you want to run before navigating to a particular route.
88
+
89
+ ::note
90
+ Route middleware runs within the Vue part of your Nuxt app. Despite the similar name, they are completely different from server middleware, which are run in the Nitro server part of your app.
91
+ ::
92
+
93
+ There are three kinds of route middleware:
94
+
95
+ 1. Anonymous (or inline) route middleware, which are defined directly in the pages where they are used.
96
+ 2. Named route middleware, which are placed in the [`middleware/`](/docs/guide/directory-structure/middleware) directory and will be automatically loaded via asynchronous import when used on a page. (**Note**: The route middleware name is normalized to kebab-case, so `someMiddleware` becomes `some-middleware`.)
97
+ 3. Global route middleware, which are placed in the [`middleware/` directory](/docs/guide/directory-structure/middleware) (with a `.global` suffix) and will be automatically run on every route change.
98
+
99
+ Example of an `auth` middleware protecting the `/dashboard` page:
100
+
101
+ ::code-group
102
+
103
+ ```ts twoslash [middleware/auth.ts]
104
+ function isAuthenticated(): boolean { return false }
105
+ // ---cut---
106
+ export default defineNuxtRouteMiddleware((to, from) => {
107
+ // isAuthenticated() is an example method verifying if a user is authenticated
108
+ if (isAuthenticated() === false) {
109
+ return navigateTo('/login')
110
+ }
111
+ })
112
+ ```
113
+
114
+ ```vue twoslash [pages/dashboard.vue]
115
+ <script setup lang="ts">
116
+ definePageMeta({
117
+ middleware: 'auth'
118
+ })
119
+ </script>
120
+
121
+ <template>
122
+ <h1>Welcome to your dashboard</h1>
123
+ </template>
124
+ ```
125
+
126
+ ::
127
+
128
+ :read-more{to="/docs/guide/directory-structure/middleware"}
129
+
130
+ ## Route Validation
131
+
132
+ Nuxt offers route validation via the `validate` property in [`definePageMeta()`](/docs/api/utils/define-page-meta) in each page you wish to validate.
133
+
134
+ The `validate` property accepts the `route` as an argument. You can return a boolean value to determine whether or not this is a valid route to be rendered with this page. If you return `false`, and another match can't be found, this will cause a 404 error. You can also directly return an object with `statusCode`/`statusMessage` to respond immediately with an error (other matches will not be checked).
135
+
136
+ If you have a more complex use case, then you can use anonymous route middleware instead.
137
+
138
+ ```vue twoslash [pages/posts/[id\\].vue]
139
+ <script setup lang="ts">
140
+ definePageMeta({
141
+ validate: async (route) => {
142
+ // Check if the id is made up of digits
143
+ return typeof route.params.id === 'string' && /^\d+$/.test(route.params.id)
144
+ }
145
+ })
146
+ </script>
147
+ ```
148
+
149
+ :read-more{to="/docs/api/utils/define-page-meta"}