Skip to main content
The render() function is used for server-side rendering (SSR) of Svelte components. It takes a component and returns an object with properties for populating HTML on the server.

Import

Signature

Parameters

component
Component
required
The Svelte component to render
options
object
Configuration options for rendering
options.props
object
Props to pass to the component (excluding $$slots and $$events)
options.context
Map<any, any>
Context map that will be available to the component via getContext()
options.idPrefix
string
Prefix for generated IDs to avoid conflicts when rendering multiple instances
options.csp
object
Content Security Policy configuration for inline scripts
options.csp.nonce
string
Nonce value to add to generated <script> tags
options.csp.hash
boolean
Whether to generate SHA-256 hashes for CSP script-src directive
options.transformError
(error: unknown) => unknown
Function to transform errors caught by error boundaries before rendering

Return Value

The render() function returns a RenderOutput object with the following properties:
head
string
HTML content that should be inserted into the <head> element, including component styles and meta tags
body
string
HTML content that should be inserted into the <body> element
html
string
deprecated
Alias for body. Use body instead
hashes
object
CSP hashes for generated scripts
hashes.script
string[]
Array of SHA-256 hash values (format: sha256-{hash}) for use in CSP script-src directive
The return value is also a PromiseLike, so you can await it to handle asynchronous rendering:

Usage

Basic Server-Side Rendering

With Context

With Content Security Policy

Asynchronous Rendering

Error Handling

Notes

  • The render() function is only available when compiling with the server option
  • Generated HTML includes hydration markers for client-side hydration
  • The function will throw if CSP options include both nonce and hash
  • Component lifecycle hooks like onMount do not run during server-side rendering
  • The html property is deprecated; use body instead