Vue Server-Side Rendering (SSR)
Introduction to Server-Side Rendering with Vue.js:
Server-side rendering (SSR) is a way of pre-rendering Vue.js applications on the server before sending them to the client.
As such, this enables search engines and social media crawlers to get a fully rendered page that improves SEO and performance.
On top of that, it speeds up perceived performance for users by rapidly transmitting HTML content to their browser, which can be progressively enhanced using client-side JavaScript.
Setting up SSR with Nuxt.js:
Nuxt.js is a framework built on top of Vue.js that makes setup for SSR in Vue apps easier.
Therefore, Nuxt.js simplifies the configuration for SSR and provides file-based routing system, code splitting automatically and server-side rendering or static site generation (SSG) out of the box.
Example:
Bash:
# Create a new Nuxt.js project
npx create-nuxt-app my-nuxt-app
Javascript:
// pages/index.vue
<template>
<div>
<h1>{{ message }}</h1>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello from Nuxt.js!'
};
}
};
</script>
In creating a new project called “my-nuxt-app” with create-nuxt-app based on Nuxt.js as shown in this example. Thus, nuxt js creates a project structure complete with all required configuration files and folders.
Afterward we build an index.vue page component within the pages directory indicating our application’s home page. And there we use data property message defined in this component then itself uses it within template.
With Nuxt.js, SSR is configured out of the box, so when you run the Nuxt.js application (npm run dev), it will automatically render the Vue components on the server and send the pre-rendered HTML to the client.
Nuxt.js Besides that, js also offers other capabilities like code automatic splitting, lazy loading, utilities running on the server and SSG, and this makes it as one of the powerful frameworks to use during Vue building. js programs now require SSR technology as well.