Socket
Socket
Sign inDemoInstall

@vue/server-renderer

Package Overview
Dependencies
2
Maintainers
1
Versions
202
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vue/server-renderer

@vue/server-renderer


Version published
Weekly downloads
2.9M
increased by1.9%
Maintainers
1
Install size
4.01 MB
Created
Weekly downloads
 

Package description

What is @vue/server-renderer?

The @vue/server-renderer package is designed for server-side rendering (SSR) of Vue.js applications. It allows developers to render Vue components on the server and send the rendered HTML to the client, improving the initial load time and SEO for Vue.js applications. This package is part of the Vue.js ecosystem and is specifically tailored for Vue 3.x applications.

What are @vue/server-renderer's main functionalities?

Rendering a Vue Component to a String

This feature allows you to render a Vue component to an HTML string on the server. This is useful for generating the initial HTML for a page on the server, which can then be sent to the client for faster initial load times and better SEO.

const { createSSRApp } = require('vue');
const { renderToString } = require('@vue/server-renderer');

const app = createSSRApp({
  data: () => ({ msg: 'Hello, server-side rendering!' }),
  template: '<div>{{ msg }}</div>'
});

renderToString(app).then(html => {
  console.log(html);
});

Streaming a Vue Component

This feature enables streaming rendering of a Vue component. Instead of waiting for the entire component to be rendered before sending it to the client, the rendered output is streamed to the client as it's generated. This can improve the perceived performance of the application.

const { createSSRApp } = require('vue');
const { renderToNodeStream } = require('@vue/server-renderer');
const express = require('express');
const app = express();

const vueApp = createSSRApp({
  data: () => ({ msg: 'Hello, streaming SSR!' }),
  template: '<div>{{ msg }}</div>'
});

app.get('/', (req, res) => {
  res.setHeader('Content-Type', 'text/html');
  const stream = renderToNodeStream(vueApp);
  stream.pipe(res);
});

app.listen(3000);

Other packages similar to @vue/server-renderer

Changelog

Source

3.4.1 (2023-12-30)

Bug Fixes

  • compat: correct enum value for COMPILER_FILTERS feature (#9875) (77d33e2)
  • defineModel: always default modifiers to empty object (9bc3c7e), closes #9945
  • defineModel: support local mutation when only prop but no listener is passed (97ce041)
  • types: fix defineModel watch type error (#9942) (4af8583), closes #9939

Features

  • compiler-sfc: support passing template parsing options when parsing sfc (6fab855) (necessary to fix https://github.com/vitejs/vite-plugin-vue/issues/322)

Read this blog post for an overview of the release highlights.

Potential Actions Needed

  1. To fully leverage new features in 3.4, it is recommended to also update the following dependencies when upgrading to 3.4:

    • Volar / vue-tsc@^1.8.27 (required)
    • @vitejs/plugin-vue@^5.0.0 (if using Vite)
    • nuxt@^3.9.0 (if using Nuxt)
    • vue-loader@^17.4.0 (if using webpack or vue-cli)
  2. If using TSX with Vue, check actions needed in Removed: Global JSX Namespace.

  3. Make sure you are no longer using any deprecated features (if you are, you should have warnings in the console telling you so). They may have been removed in 3.4.

Features

Performance Improvements

  • compiler-sfc: avoid sfc source map unnecessary serialization and parsing (f15d2f6)
  • compiler-sfc: remove magic-string trim on script (e8e3ec6)
  • compiler-sfc: use faster source map addMapping (50cde7c)
  • compiler-core: optimize away isBuiltInType (66c0ed0)
  • compiler-core: optimize position cloning (2073236)
  • codegen: optimize line / column calculation during codegen (3be53d9)
  • codegen: optimize source map generation (c11002f)
  • shared: optimize makeMap (ae6fba9)

BREAKING CHANGES

Global JSX Registration Removed

Starting in 3.4, Vue no longer registers the global JSX namespace by default. This is necessary to avoid global namespace collision with React so that TSX of both libs can co-exist in the same project. This should not affect SFC-only users with latest version of Volar.

If you are using TSX, there are two options:

  1. Explicitly set jsxImportSource to 'vue' in tsconfig.json before upgrading to 3.4. You can also opt-in per file by adding a /* @jsxImportSource vue */ comment at the top of the file.

  2. If you have code that depends on the presence of the global JSX namespace, e.g. usage of types like JSX.Element etc., you can retain the exact pre-3.4 global behavior by explicitly referencing vue/jsx, which registers the global JSX namespace.

Note that this is a type-only breaking change in a minor release, which adheres to our release policy.

Deprecated Features Removed
  • Reactivity Transform was marked deprecated in 3.3 and is now removed in 3.4. This change does not require a major due to the feature being experimental. Users who wish to continue using the feature can do so via the Vue Macros plugin.
  • app.config.unwrapInjectedRef has been removed. It was deprecated and enabled by default in 3.3. In 3.4 it is no longer possible to disable this behavior.
  • @vnodeXXX event listeners in templates are now a compiler error instead of a deprecation warning. Use @vue:XXX listeners instead.
  • v-is directive has been removed. It was deprecated in 3.3. Use the is attribute with vue: prefix instead.

Readme

Source

@vue/server-renderer

Note: as of 3.2.13+, this package is included as a dependency of the main vue package and can be accessed as vue/server-renderer. This means you no longer need to explicitly install this package and ensure its version match that of vue's. Just use the vue/server-renderer deep import instead.

Basic API

renderToString

Signature

function renderToString(
  input: App | VNode,
  context?: SSRContext
): Promise<string>

Usage

const { createSSRApp } = require('vue')
const { renderToString } = require('@vue/server-renderer')

const app = createSSRApp({
  data: () => ({ msg: 'hello' }),
  template: `<div>{{ msg }}</div>`
})

;(async () => {
  const html = await renderToString(app)
  console.log(html)
})()

Handling Teleports

If the rendered app contains teleports, the teleported content will not be part of the rendered string. Instead, they are exposed under the teleports property of the ssr context object:

const ctx = {}
const html = await renderToString(app, ctx)

console.log(ctx.teleports) // { '#teleported': 'teleported content' }

Streaming API

renderToNodeStream

Renders input as a Node.js Readable stream.

Signature

function renderToNodeStream(input: App | VNode, context?: SSRContext): Readable

Usage

// inside a Node.js http handler
renderToNodeStream(app).pipe(res)

Note: This method is not supported in the ESM build of @vue/server-renderer, which is decoupled from Node.js environments. Use pipeToNodeWritable instead.

pipeToNodeWritable

Render and pipe to an existing Node.js Writable stream instance.

Signature

function pipeToNodeWritable(
  input: App | VNode,
  context: SSRContext = {},
  writable: Writable
): void

Usage

// inside a Node.js http handler
pipeToNodeWritable(app, {}, res)

renderToWebStream

Renders input as a Web ReadableStream.

Signature

function renderToWebStream(
  input: App | VNode,
  context?: SSRContext
): ReadableStream

Usage

// inside an environment with ReadableStream support
return new Response(renderToWebStream(app))

Note: in environments that do not expose ReadableStream constructor in the global scope, pipeToWebWritable should be used instead.

pipeToWebWritable

Render and pipe to an existing Web WritableStream instance.

Signature

function pipeToWebWritable(
  input: App | VNode,
  context: SSRContext = {},
  writable: WritableStream
): void

Usage

This is typically used in combination with TransformStream:

// TransformStream is available in environments such as CloudFlare workers.
// in Node.js, TransformStream needs to be explicitly imported from 'stream/web'
const { readable, writable } = new TransformStream()
pipeToWebWritable(app, {}, writable)

return new Response(readable)

renderToSimpleStream

Renders input in streaming mode using a simple readable interface.

Signature

function renderToSimpleStream(
  input: App | VNode,
  context: SSRContext,
  options: SimpleReadable
): SimpleReadable

interface SimpleReadable {
  push(content: string | null): void
  destroy(err: any): void
}

Usage

let res = ''

renderToSimpleStream(
  app,
  {},
  {
    push(chunk) {
      if (chunk === null) {
        // done
        console(`render complete: ${res}`)
      } else {
        res += chunk
      }
    },
    destroy(err) {
      // error encountered
    }
  }
)

Keywords

FAQs

Last updated on 30 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc