Socket
Socket
Sign inDemoInstall

@vue/server-renderer

Package Overview
Dependencies
20
Maintainers
2
Versions
206
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
3M
increased by0.25%
Maintainers
2
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.28 (2024-06-14)

Bug Fixes

  • compat: correctly transform non-identifier expressions in legacy filter syntax (#10896) (07b3c4b), closes #10852
  • compat: ensure proper handling of render fuction from SFC using Vue.extend (#7781) (c73847f), closes #7766
  • compat: only warn ATTR_FALSE_VALUE when enabled (04729ba), closes #11126
  • compile-sfc: register props destructure rest id as setup bindings (#10888) (b2b5f57), closes #10885
  • compile-sfc: Support project reference with folder, (#10908) (bdeac37), closes #10907
  • compiler-core: allow unicode to appear in simple identifiers (#6765) (3ea9644), closes #6367
  • compiler-core: change v-for key type to match Object.keys (#10963) (9fead52), closes #8819
  • compiler-core: emit TS-compatible function declaration when requested (#9363) (5d25850)
  • compiler-core: fix :key shorthand on v-for (#10942) (29425df), closes #10882 #10939
  • compiler-core: make ForIteratorExpression's returns property optional (#11011) (5b8c1af)
  • compiler-core: should set <math> tag as block to retain MathML namespace after patching (#10891) (87c5443)
  • compiler-core: v-for expression missing source with spaces should emit error (#5821) (b9ca202), closes #5819
  • compiler-sfc: improve type resolving for the keyof operator (#10921) (293cf4e), closes #10920 #11002
  • compiler-sfc: support as keyword with template literal types (#11100) (2594b1d), closes #10962
  • compiler-sfc: support type resolve for keyof for intersection & union types (#11132) (495263a), closes #11129
  • compiler-sfc: throw error when import macro as alias (#11041) (34a97ed)
  • correct the type of <details>'s onToggle event handler (#10938) (fd18ce7), closes #10928
  • custom-element: disconnect MutationObserver in nextTick in case that custom elements are moved (#10613) (bbb5be2), closes #10610
  • custom-elements: compatibility of createElement in older versions of Chrome (#9615) (a88295d), closes #9614
  • hmr: avoid infinite recursion when reloading hmr components (#6936) (36bd9b0), closes #6930
  • hydration: log hydration error even when using async components (#9403) (5afc76c), closes #9369
  • KeepAlive: properly cache nested Suspense subtree (#10912) (07764fe)
  • npm: explicitly add @vue/reactivity as dependency of @vue/runtime-dom (#10468) (ec424f6)
  • reactivity: pass oldValue in debug info when triggering refs (#8210) (3b0a56a), closes vuejs/pinia#2061
  • runtime-core: avoid traversing static children for vnodes w/ PatchFlags.BAIL (#11115) (b557d3f), closes #10547
  • runtime-core: do not fire mount/activated hooks if unmounted before mounted (#9370) (aa156ed), closes #8898 #9264 #9617
  • runtime-core: ensure suspense creates dep component's render effect with correct optimized flag (#7689) (c521f95), closes #7688
  • runtime-core: fix missed updates when passing text vnode to <component :is> (#8304) (b310ec3), closes #8298
  • runtime-core: fix stale v-memo after v-if toggle (#6606) (edf2638), closes #6593
  • runtime-core: fix Transition for components with root-level v-if (#7678) (ef2e737), closes #7649
  • runtime-dom: also set attribute for form element state (537a571), closes #6007 #6012
  • runtime-dom: support Symbol for input value bindings (#10608) (188f3ae), closes #10597
  • shared: ensure invokeArrayFns handles undefined arguments (#10869) (9b40d0f), closes #10863
  • ssr: directive binding.instance should respect exposed during ssr (df686ab), closes #7499 #7502
  • ssr: fix hydration for node with empty text node (#7216) (d1011c0)
  • ssr: fix the bug that multi slot scope id does not work on component (#6100) (4c74302), closes #6093
  • teleport: do not throw target warning when teleport is disabled (#9818) (15ee43f)
  • transition: ensure Transition enterHooks are updated after clone (#11066) (671cf29), closes #11061
  • types/apiWatch: correct type inference for reactive array (#11036) (aae2d78), closes #9416
  • types: improve app.provide type checking (#10603) (612bbf0), closes #10602
  • types: support generic argument in setup context expose method (#8507) (635a59b)
  • v-model: fix the lazy modifier is not reset by other modifications (#8547) (a52a02f), closes #8546 #6564 #6773
  • watch: support traversing symbol properties in deep watcher (#10969) (a3e8aaf), closes #402

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 14 Jun 2024

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