Vue 2 Server Renderer

2.7.16 · abandoned · verified Sun Apr 19

The `vue-server-renderer` package provides Node.js server-side rendering (SSR) capabilities for applications built with Vue.js 2.x. As of its final release, v2.7.16 ("Swan Song"), this package is intrinsically tied to Vue 2, which reached its End of Life (EOL) on December 31st, 2023. Consequently, `vue-server-renderer` is no longer actively maintained and will not receive further updates, including bug fixes or security patches. Its primary function was to enable universal Vue 2 applications, improving initial page load times and SEO by delivering fully-rendered HTML from the server. For any ongoing or new projects, developers are strongly advised to migrate to Vue 3 and utilize its corresponding SSR solution, `@vue/server-renderer`, which is under active development and support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic Vue 2 application and render it to a static HTML string using `vue-server-renderer` for server-side rendering.

import Vue from 'vue';
import { createRenderer } from 'vue-server-renderer';

// 1. Create a Vue 2 application instance
const app = new Vue({
  template: `
    <div id="app">
      <h1>Welcome to Vue 2 SSR!</h1>
      <p>This message: {{ serverMessage }}</p>
      <button @click="increment">Client-side interaction: {{ count }}</button>
      <p><em>(Button functionality requires client-side hydration)</em></p>
    </div>
  `,
  data() {
    return {
      serverMessage: 'Rendered on the server.',
      count: 0
    };
  },
  methods: {
    increment() {
      this.count++;
      console.log('Client-side count:', this.count); // This log only appears in browser
    }
  }
});

// 2. Create a renderer instance
const renderer = createRenderer();

// 3. Render the Vue app to an HTML string
renderer.renderToString(app)
  .then(html => {
    const fullHtml = `
<!DOCTYPE html>
<html>
<head>
  <title>Vue 2 SSR Example</title>
  <style>body { font-family: sans-serif; margin: 20px; }</style>
</head>
<body>
  ${html}
  <!-- In a real application, you would hydrate this on the client:
  <script>
    // window.__INITIAL_STATE__ = { serverMessage: '...', count: 0 };
    // new Vue({ el: '#app', data: window.__INITIAL_STATE__ });
  </script> -->
</body>
</html>
    `;
    console.log(fullHtml);
  })
  .catch(err => {
    console.error('Error during Vue 2 SSR:', err);
    process.exit(1);
  });

view raw JSON →