网页 综合 更新时间 2026-11-16 03:45:48

Rendering Modes · Nuxt Concepts v4

自动蜘蛛 nuxt.com 正文约 6000 字
Rendering Modes · Nuxt Concepts v4
Nuxt supports different rendering modes, universal rendering, client-side rendering but also offers hybrid-rendering and the possibility to render your application on CDN Edge Servers.
Both the browser and server can interpret JavaScript code to turn Vue.js components into HTML elements. This step is called rendering. Nuxt supports both universal and client-side rendering. The two approaches have benefits and downsides that we will cover.
By default, Nuxt uses universal rendering to provide better user experience, performance and to optimize search engine indexing, but you can switch rendering modes in one line of configuration.
This step is similar to traditional server-side rendering performed by PHP or Ruby applications. When the browser requests a URL with universal rendering enabled, Nuxt runs the JavaScript (Vue.js) code in a server environment and returns a fully rendered HTML page to the browser. Nuxt may also return a fully rendered HTML page from a cache if the page was generated in advance. Users immediately get the entirety of the initial content of the application, contrary to client-side rendering.
Once the HTML document has been downloaded, the browser interprets this and Vue.js takes control of the document. The same JavaScript code that once ran on the server runs on the client (browser) again in the background now enabling interactivity (hence Universal rendering) by binding its listeners to the HTML. This is called Hydration. When hydration is complete, the page can enjoy benefits such as dynamic interfaces and page transitions.
Universal rendering allows a Nuxt application to provide quick page load times while preserving the benefits of client-side rendering. Furthermore, as the content is already present in the HTML document, crawlers can index it without overhead.
What's server-rendered and what's client-rendered?
It is normal to ask which parts of a Vue file runs on the server and/or the client in universal rendering mode.
On the initial request, the counter ref is initialized in the server since it is rendered inside the <p> tag. The contents of handleClick is never executed here. During hydration in the browser, the counter ref is re-initialized. The handleClick finally binds itself to the button; Therefore it is reasonable to deduce that the body of handleClick will always run in a browser environment.
Middlewares and pages run in the server and on the client during hydration. Plugins can be rendered on the server or client or both. Components can be forced to run on the client only as well. Composables and utilities are rendered based on the context of their usage.
Benefits of server-side rendering:
Downsides of server-side rendering:
Universal rendering is very versatile and can fit almost any use case, and is especially appropriate for any content-oriented websites: blogs, marketing websites, portfolios, e-commerce sites, and marketplaces.
Out of the box, a traditional Vue.js application is rendered in the browser (or client). Then, Vue.js generates HTML elements after the browser downloads and parses all the JavaScript code containing the instructions to create the current interface.
Benefits of client-side rendering:
Downsides of client-side rendering:
Client-side rendering is a good choice for heavily interactive web applications that don't need indexing or whose users visit frequently. It can leverage browser caching to skip the download phase on subsequent visits, such as SaaS, back-office applications, or online games.
You can enable client-side only rendering with Nuxt in your nuxt.config.ts:
If you deploy your app to static hosting with the nuxt generate or nuxt build --prerender commands, then by default, Nuxt will render every page as a separate static HTML file.
If you are using purely client-side rendering, then this might be unnecessary. You might only need a single index.html file, plus 200.html and 404.html fallbacks, which you can tell your static web host to serve up for all requests.
In order to achieve this we can change how the routes are prerendered. Just add this to your hooks in your nuxt.config.ts:
This will produce three files:
Static hosts need an HTML shell for client-side routes and missing paths. Nuxt emits two SPA fallbacks for that:
nuxt generate and nuxt build --prerender write these into .output/public/. A plain nuxt build without prerender does not. With hybrid route rules, add the fallbacks with route rules or run a prerender build if you need them. Point your host at the file your provider expects.
By default 404.html is an empty shell, so your error.vue (and its layout and data) only appear once the client app has booted. You can prerender it instead:
Nuxt then renders error.vue at build time with a synthetic 404 error and writes the result to 404.html, which hydrates in place at whatever URL your host served it from. Pass an array of status codes between 400 and 599 ([404, 500]) to emit additional pages, if your host can serve them.
Because a single file is served for every missing path, the error page cannot depend on the request: useRoute(), useRequestURL() and data fetched with the current path will hold the build-time values in the prerendered HTML and be corrected on hydration. Wrap request-specific markup in <ClientOnly>, and skip request-specific data fetching with import.meta.prerender:
import.meta.prerender is only true while the page is being generated, so the same error.vue still server-renders its data when the app is deployed with a server.
When prerendering a client-rendered app, Nuxt will generate index.html, 200.html and 404.html files by default. However, if you need to prevent any (or all) of these files from being generated in your build, you can use the 'prerender:generate' hook from Nitro.
Hybrid rendering allows different caching rules per route using Route Rules and decides how the server should respond to a new request on a given URL.
Previously every route/page of a Nuxt application and server mu
访问原文 返回搜索 https://nuxt.com/docs/guide/concepts/rendering
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。