You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/3.guide/1.concepts/2.nuxt-lifecycle.md
+23-16Lines changed: 23 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,15 +5,16 @@ description: "Understanding the lifecycle of Nuxt applications can help you gain
5
5
6
6
The goal of this chapter is to provide a high-level overview of the different parts of the framework, their execution order, and how they work together.
7
7
8
-
## Server
8
+
## Server lifecycle
9
9
10
10
On the server, the following steps are executed for every initial request to your application:
11
11
12
-
### Step 1: Setup Nitro Server and Nitro Plugins (Once)
12
+
::steps
13
+
### Server plugins :badge[once]{color="info"class="align-middle"}
13
14
14
15
Nuxt is powered by [Nitro](https://nitro.build/), a modern server engine.
15
16
16
-
When Nitro starts, it initializes and executes the plugins under the `/server/plugins` directory. These plugins can:
17
+
When Nitro starts, it initializes and executes the plugins under the [`/server/plugins`](/docs/4.x/directory-structure/server#server-plugins) directory. These plugins can:
17
18
- Capture and handle application-wide errors.
18
19
- Register hooks that execute when Nitro shuts down.
19
20
- Register hooks for request lifecycle events, such as modifying responses.
@@ -24,7 +25,7 @@ Nitro plugins are executed only once when the server starts. In a serverless env
After initializing the Nitro server, middleware under `server/middleware/` is executed for every request. Middleware can be used for tasks such as authentication, logging, or request transformation.
30
31
@@ -34,9 +35,9 @@ Returning a value from middleware will terminate the request and send the return
### Step 3: Initialize Nuxt and Execute Nuxt App Plugins
38
+
### App plugins
38
39
39
-
The Vue and Nuxt instances are created first. Afterward, Nuxt executes its server plugins. This includes:
40
+
The Vue and Nuxt instances are created first. Afterward, Nuxt executes its app plugins. This includes:
40
41
- Built-in plugins, such as Vue Router and `unhead`.
41
42
- Custom plugins located in the `app/plugins/` directory, including those without a suffix (e.g., `myPlugin.ts`) and those with the `.server` suffix (e.g., `myServerPlugin.server.ts`).
42
43
@@ -48,7 +49,7 @@ After this step, Nuxt calls the [`app:created`](/docs/4.x/api/advanced/hooks#app
After initializing plugins and before executing middleware, Nuxt calls the `validate` method if it is defined in the `definePageMeta` function. The `validate` method, which can be synchronous or asynchronous, is often used to validate dynamic route parameters.
54
55
@@ -59,7 +60,7 @@ For more information, see the [Route Validation documentation](/docs/4.x/getting
Nuxt renders the page and its components and fetches any required data with `useFetch` and `useAsyncData` during this step. Since there are no dynamic updates and no DOM operations occur on the server, Vue lifecycle hooks such as `onBeforeMount`, `onMounted`, and subsequent hooks are **NOT** executed during SSR.
82
83
@@ -94,7 +95,7 @@ You should avoid code that produces side effects that need cleanup in root scope
94
95
Watch a video from Daniel Roe explaining Server Rendering and Global State.
95
96
::
96
97
97
-
### Step 7: Generate HTML Output
98
+
### HTML Output
98
99
99
100
After all required data is fetched and the components are rendered, Nuxt combines the rendered components with settings from `unhead` to generate a complete HTML document. This HTML, along with the associated data, is then sent back to the client to complete the SSR process.
100
101
@@ -106,11 +107,15 @@ After rendering the Vue application to HTML, Nuxt calls the [`app:rendered`](/do
106
107
Before finalizing and sending the HTML, Nitro will call the [`render:html`](/docs/4.x/api/advanced/hooks#nitro-app-hooks-runtime-server-side) hook. This hook allows you to manipulate the generated HTML, such as injecting additional scripts or modifying meta tags.
107
108
::
108
109
109
-
## Client (browser)
110
+
::
111
+
112
+
## Client lifecycle
110
113
111
114
This part of the lifecycle is fully executed in the browser, no matter which Nuxt mode you've chosen.
112
115
113
-
### Step 1: Initialize Nuxt and Execute Nuxt App Plugins
116
+
::steps
117
+
118
+
### App plugins
114
119
115
120
This step is similar to the server-side execution and includes both built-in and custom plugins.
116
121
@@ -122,17 +127,17 @@ After this step, Nuxt calls the [`app:created`](/docs/4.x/api/advanced/hooks#app
This step is the same as the server-side execution and includes the `validate` method if defined in the `definePageMeta` function.
128
133
129
-
### Step 3: Execute Nuxt App Middleware
134
+
### App middleware
130
135
131
136
Nuxt middleware runs on both the server and the client. If you want certain code to run in specific environments, consider splitting it by using `import.meta.client` for the client and `import.meta.server` for the server.
Calling `app.mount('#__nuxt')` mounts the Vue application to the DOM. If the application uses SSR or SSG mode, Vue performs a hydration step to make the client-side application interactive. During hydration, Vue recreates the application (excluding [Server Components](/docs/4.x/guide/directory-structure/app/components#server-components)), matches each component to its corresponding DOM nodes, and attaches DOM event listeners.
138
143
@@ -146,6 +151,8 @@ Before mounting the Vue application, Nuxt calls the [`app:beforeMount`](/docs/4.
146
151
After mounting the Vue application, Nuxt calls the [`app:mounted`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) hook.
147
152
::
148
153
149
-
### Step 5: Vue Lifecycle
154
+
### Vue lifecycle
150
155
151
156
Unlike on the server, the browser executes the full [Vue lifecycle](https://vuejs.org/guide/essentials/lifecycle).
0 commit comments