@nuxt/docs-nightly 4.0.2-29219763.831e65b8 → 4.0.2-29223962.d8d2f3e1
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.
|
@@ -39,19 +39,19 @@ Open a terminal (if you're using [Visual Studio Code](https://code.visualstudio.
|
|
|
39
39
|
::code-group{sync="pm"}
|
|
40
40
|
|
|
41
41
|
```bash [npm]
|
|
42
|
-
npm create nuxt <project-name>
|
|
42
|
+
npm create nuxt@latest <project-name>
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
```bash [yarn]
|
|
46
|
-
yarn create nuxt <project-name>
|
|
46
|
+
yarn create nuxt@latest <project-name>
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
```bash [pnpm]
|
|
50
|
-
pnpm create nuxt <project-name>
|
|
50
|
+
pnpm create nuxt@latest <project-name>
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
```bash [bun]
|
|
54
|
-
bun create nuxt <project-name>
|
|
54
|
+
bun create nuxt@latest <project-name>
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
```bash [deno]
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
---
|
|
2
|
+
navigation.title: 'Nuxt and hydration'
|
|
3
|
+
title: Nuxt and hydration
|
|
4
|
+
description: Why fixing hydration issues is important
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
When developing, you may face hydration issues. Don't ignore those warnings.
|
|
8
|
+
|
|
9
|
+
# Why is it important to fix them?
|
|
10
|
+
|
|
11
|
+
Hydration mismatches are not just warnings - they are indicators of serious problems that can break your application:
|
|
12
|
+
|
|
13
|
+
## Performance Impact
|
|
14
|
+
|
|
15
|
+
- **Increased time to interactive**: Hydration errors force Vue to re-render the entire component tree, which will increase the time for your Nuxt app to become interactive
|
|
16
|
+
- **Poor user experience**: Users may see content flashing or unexpected layout shifts
|
|
17
|
+
|
|
18
|
+
## Functionality Issues
|
|
19
|
+
|
|
20
|
+
- **Broken interactivity**: Event listeners may not attach properly, leaving buttons and forms non-functional
|
|
21
|
+
- **State inconsistencies**: Application state can become out of sync between what the user sees and what the application thinks is rendered
|
|
22
|
+
- **SEO problems**: Search engines may index different content than what users actually see
|
|
23
|
+
|
|
24
|
+
# How to detect them
|
|
25
|
+
|
|
26
|
+
## Development Console Warnings
|
|
27
|
+
|
|
28
|
+
Vue will log hydration mismatch warnings in the browser console during development:
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
# Common reasons
|
|
33
|
+
|
|
34
|
+
## Browser-only APIs in Server Context
|
|
35
|
+
|
|
36
|
+
**Problem**: Using browser-specific APIs during server-side rendering.
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<template>
|
|
40
|
+
<div>User preference: {{ userTheme }}</div>
|
|
41
|
+
</template>
|
|
42
|
+
|
|
43
|
+
<script setup>
|
|
44
|
+
// This will cause hydration mismatch!
|
|
45
|
+
// localStorage doesn't exist on the server!
|
|
46
|
+
const userTheme = localStorage.getItem('theme') || 'light'
|
|
47
|
+
</script>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Solution**: You can use [`useCookie`](/docs/api/composables/use-cookie):
|
|
51
|
+
|
|
52
|
+
```html
|
|
53
|
+
<template>
|
|
54
|
+
<div>User preference: {{ userTheme }}</div>
|
|
55
|
+
</template>
|
|
56
|
+
|
|
57
|
+
<script setup>
|
|
58
|
+
// This works on both server and client
|
|
59
|
+
const userTheme = useCookie('theme', { default: () => 'light' })
|
|
60
|
+
</script>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Inconsistent Data
|
|
64
|
+
|
|
65
|
+
**Problem**: Different data between server and client.
|
|
66
|
+
|
|
67
|
+
```html
|
|
68
|
+
<template>
|
|
69
|
+
<div>{{ Math.random() }}</div>
|
|
70
|
+
</template>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**Solution**: Use SSR-friendly state:
|
|
74
|
+
|
|
75
|
+
```html
|
|
76
|
+
<template>
|
|
77
|
+
<div>{{ state }}</div>
|
|
78
|
+
</template>
|
|
79
|
+
|
|
80
|
+
<script setup>
|
|
81
|
+
const state = useState('random', () => Math.random())
|
|
82
|
+
</script>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Conditional Rendering Based on Client State
|
|
86
|
+
|
|
87
|
+
**Problem**: Using client-only conditions during SSR.
|
|
88
|
+
|
|
89
|
+
```html
|
|
90
|
+
<template>
|
|
91
|
+
<div v-if="window?.innerWidth > 768">
|
|
92
|
+
Desktop content
|
|
93
|
+
</div>
|
|
94
|
+
</template>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Solution**: Use media queries or handle it client-side:
|
|
98
|
+
|
|
99
|
+
```html
|
|
100
|
+
<template>
|
|
101
|
+
<div class="responsive-content">
|
|
102
|
+
<div class="hidden md:block">Desktop content</div>
|
|
103
|
+
<div class="md:hidden">Mobile content</div>
|
|
104
|
+
</div>
|
|
105
|
+
</template>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Third-party Libraries with Side Effects
|
|
109
|
+
|
|
110
|
+
**Problem**: Libraries that modify the DOM or have browser dependencies (this happens a LOT with tag managers).
|
|
111
|
+
|
|
112
|
+
```html
|
|
113
|
+
<script setup>
|
|
114
|
+
if (import.meta.client) {
|
|
115
|
+
const { default: SomeBrowserLibrary } = await import('browser-only-lib')
|
|
116
|
+
SomeBrowserLibrary.init()
|
|
117
|
+
}
|
|
118
|
+
</script>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Solution**: Initialise libraries after hydration has completed:
|
|
122
|
+
|
|
123
|
+
```html
|
|
124
|
+
<script setup>
|
|
125
|
+
onMounted(async () => {
|
|
126
|
+
const { default: SomeBrowserLibrary } = await import('browser-only-lib')
|
|
127
|
+
SomeBrowserLibrary.init()
|
|
128
|
+
})
|
|
129
|
+
</script>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Dynamic Content Based on Time
|
|
133
|
+
|
|
134
|
+
**Problem**: Content that changes based on current time.
|
|
135
|
+
|
|
136
|
+
```html
|
|
137
|
+
<template>
|
|
138
|
+
<div>{{ greeting }}</div>
|
|
139
|
+
</template>
|
|
140
|
+
|
|
141
|
+
<script setup>
|
|
142
|
+
const hour = new Date().getHours()
|
|
143
|
+
const greeting = hour < 12 ? 'Good morning' : 'Good afternoon'
|
|
144
|
+
</script>
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**Solution**: Use [`NuxtTime`](/docs/api/components/nuxt-time) component or handle it client-side:
|
|
148
|
+
|
|
149
|
+
```html
|
|
150
|
+
<template>
|
|
151
|
+
<div>
|
|
152
|
+
<NuxtTime :date="new Date()" format="HH:mm" />
|
|
153
|
+
</div>
|
|
154
|
+
</template>
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```html
|
|
158
|
+
<template>
|
|
159
|
+
<div>
|
|
160
|
+
<ClientOnly>
|
|
161
|
+
{{ greeting }}
|
|
162
|
+
<template #fallback>
|
|
163
|
+
Hello!
|
|
164
|
+
</template>
|
|
165
|
+
</ClientOnly>
|
|
166
|
+
</div>
|
|
167
|
+
</template>
|
|
168
|
+
|
|
169
|
+
<script setup>
|
|
170
|
+
const greeting = ref('Hello!')
|
|
171
|
+
|
|
172
|
+
onMounted(() => {
|
|
173
|
+
const hour = new Date().getHours()
|
|
174
|
+
greeting.value = hour < 12 ? 'Good morning' : 'Good afternoon'
|
|
175
|
+
})
|
|
176
|
+
</script>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## In summary
|
|
180
|
+
|
|
181
|
+
1. **Use SSR-friendly composables**: [`useFetch`](/docs/api/composables/use-fetch), [`useAsyncData`](/docs/api/composables/use-async-data), [`useState`](/docs/api/composables/use-state)
|
|
182
|
+
2. **Wrap client-only code**: Use [`ClientOnly`](/docs/api/components/client-only) component for browser-specific content
|
|
183
|
+
3. **Consistent data sources**: Ensure server and client uses the same data
|
|
184
|
+
4. **Avoid side effects in setup**: Move browser-dependent code to `onMounted`
|
|
185
|
+
|
|
186
|
+
::tip
|
|
187
|
+
You can read the [Vue documentation on SSR hydration mismatch](https://vuejs.org/guide/scaling-up/ssr.html#hydration-mismatch) for a better understanding of hydration.
|
|
188
|
+
::
|
|
@@ -80,6 +80,24 @@ If we mark a PR as 'pending', that means we likely have another task to do in re
|
|
|
80
80
|
|
|
81
81
|
We'll do our best to follow [our PR decision making flowchart](https://mermaid.live/view#pako:eNp9VE1v2kAQ_SsjXzBSEqlALlaUisSh0ACK2l4qcVm8Y9hi7672Iwly-O-ZtYPt5FAOCHbee_PmzdpVlCmOURLlhXrJ9sw4-JNuJNBnWs1UQafIQVjrERyWumAOv58-AJeXt29_0b7BXbWwwL0uRPa1vlZvcB_fF8oiMMmB2QM4BXkt3UoON7Lh3LWaDz2SVkK6QGt7DHvw0CKt5sxCKaQoWQEGtVHcZ04oGdw04LTVngW_LHOeFcURGGz97mw6PSv-iJdsi0UCA4nI7SfNwc3W3JZit3eQ1SZFDlKB15yswQ2MgbOjbYeatY3n8bcr-IWlekYYaJRcyB04I9gOB1CEfkF5dAVTzmFAtnqn4-bUYAiMMmHZgWhNPRhgus5mW2BATxq0NkIZ4Y4NbNjzE2ZchBzcHmGLe_ZMSKCcyRXyLrVFa_5n_PBK2xKy3kk9eOjULUdltk6C8kI-7NFDr8f4EVGDoqlp-wa4sJm3ltIMIuZ_mTQXJyTSkQZtunPqsKxShV9GKdkBYe1fHXjpbcjlvONlO9Kqx_M7YHmOmav_luxfE5zKwVs09hM5DLSupgYDlr5flDkwo7ykixKG-xDsUly1LZ-uY32dgDc7lG7YqwbNp0msJwmIUivjWFtfd-xRrEcJ7Omydz37qFplHOtxEp4GskI2qB5dRCWakglOz3oV8JuITJa4iRL6yZk5bKKNPBGOead-H2UWJc54vIiaW53SPgwrz4fIhVNm1bw76lfI6R2_MW21) when responding and reviewing to pull requests.
|
|
82
82
|
|
|
83
|
+
### AI-Assisted Contributions
|
|
84
|
+
|
|
85
|
+
We welcome the thoughtful use of AI tools when contributing to Nuxt, yet ask all contributors to follow [two core principles](https://roe.dev/blog/using-ai-in-open-source).
|
|
86
|
+
|
|
87
|
+
#### Never let an LLM speak for you
|
|
88
|
+
|
|
89
|
+
* All comments, issues, and pull request descriptions should be written in your own voice
|
|
90
|
+
* We value clear, human communication over perfect grammar or spelling
|
|
91
|
+
* Avoid copy-pasting AI-generated summaries that don't reflect your own understanding
|
|
92
|
+
|
|
93
|
+
#### Never let an LLM think for you
|
|
94
|
+
|
|
95
|
+
* Feel free to use AI tools to generate code or explore ideas
|
|
96
|
+
* Only submit contributions you fully understand and can explain
|
|
97
|
+
* Contributions should reflect your own reasoning and problem-solving
|
|
98
|
+
|
|
99
|
+
Our aim is ensuring quality and maintaining the joy of collaborating and communicating with real people. If you have ideas for improving our policy on AI in the Nuxt community, we'd love to hear them! ❤️
|
|
100
|
+
|
|
83
101
|
### Create a Module
|
|
84
102
|
|
|
85
103
|
If you've built something with Nuxt that's cool, why not [extract it into a module](/docs/guide/going-further/modules), so it can be shared with others? We have [many excellent modules already](/modules), but there's always room for more.
|