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
required
The Svelte component to render
object
Configuration options for rendering
object
Props to pass to the component (excluding
$$slots and $$events)Map<any, any>
Context map that will be available to the component via
getContext()string
Prefix for generated IDs to avoid conflicts when rendering multiple instances
object
(error: unknown) => unknown
Function to transform errors caught by error boundaries before rendering
Return Value
Therender() function returns a RenderOutput object with the following properties:
string
HTML content that should be inserted into the
<head> element, including component styles and meta tagsstring
HTML content that should be inserted into the
<body> elementstring
deprecated
Alias for
body. Use body insteadobject
CSP hashes for generated scripts
string[]
Array of SHA-256 hash values (format:
sha256-{hash}) for use in CSP script-src directivePromiseLike, 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 theserveroption - Generated HTML includes hydration markers for client-side hydration
- The function will throw if CSP options include both
nonceandhash - Component lifecycle hooks like
onMountdo not run during server-side rendering - The
htmlproperty is deprecated; usebodyinstead