Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

vue3 createSSRApp() Function

Vue3 createSSRApp() Detailed Explanation of the Function


Vue3 Global API Vue 3 Global API

createSSRApp() is a function specifically designed for server-side rendering (SSR, Server-Side Rendering).

createSSRApp() By generating HTML content on the server, it improves the page's first-load speed and SEO performance.

The difference between createApp() Compared tocreateSSRApp() is more suitable for application scenarios that require server-side rendering.


1. What is Server-Side Rendering (SSR)?

Server-Side Rendering (SSR) refers to the process of generating HTML on the server and then sending the generated HTML to the client for display. Unlike traditional client-side rendering (CSR, Client-Side Rendering), SSR helps improve the page's first-load speed and enhances search engine optimization (SEO).

In client-side rendering, the browser first downloads an empty HTML file and then dynamically generates page content via JavaScript. In server-side rendering, the server has already generated the complete HTML content, and the browser only needs to display it.


2. createSSRApp() The Role of

In Vue.js 3,createApp() is the standard function for creating client-side Vue applications. While createSSRApp() is a variant of createApp() specifically designed to create Vue applications that support server-side rendering.

createSSRApp() Its main role is to generate the Vue application's HTML content on the server and send it to the client. It ensures consistency between the server-rendered and client-rendered Vue application, thereby avoiding inconsistencies between client-side and server-side rendering results.


3. Using createSSRApp() to Create an Application

Use createSSRApp() The steps to create a Vue application using createApp() are similar to those using createApp, but you need to pay special attention to some SSR-related configurations.

3.1 Basic Usage

Example

// Import createSSRApp

import

{

createSSRApp

}

from

'vue'

;

// Import the root component

import

App from

'./App.vue'

;

// Create an SSR application

const

app

=

createSSRApp

(

App

)

;

// mount application

app.

mount

(

'#app'

)

;

In this example, we use createSSRApp() to create a Vue application that supports server-side rendering and mount it to the specified DOM element.

3.2 Differences Between Server-Side and Client-Side

On the server side and the client side,createSSRApp() behaves differently:

3.3 Configuring an SSR Application

When configuring an SSR application, note the following points:

Example

export

default

{

  async onServerPrefetch

(

)

{

   

// Fetch data in advance on the server side

   

this

.

data

=

await fetchData

(

)

;

 

}

}

;


4. createSSRApp() The difference between createApp() and

Feature createApp() createSSRApp()
Rendering Environment Client-Side Rendering (CSR) Server-Side Rendering (SSR)
Initial Load Speed Slower, depends on client-side JavaScript execution Faster, HTML is pre-generated by the server
SEO Poor, search engines have difficulty crawling dynamic content Good, search engines can crawl the complete HTML
Applicable scenarios Single-page application (SPA) Applications that need SEO and fast loading

Vue3 Global API Vue 3 Global API