--- title: server-render description: Documentation of Meteor's `server-render` package. --- This package implements generic support for server-side rendering in Meteor apps, by providing a mechanism for injecting fragments of HTML into the `
` and/or `` of the application's initial HTML response. ## Usage This package exports a function named `onPageLoad` which takes a callback function that will be called at page load (on the client) or whenever a new request happens (on the server). The callback receives a `sink` object, which is an instance of either `ClientSink` or `ServerSink` depending on the environment. Both types of `sink` have the same methods, though the server version accepts only HTML strings as content, whereas the client version also accepts DOM nodes. The current interface of `{Client,Server}Sink` objects is as follows: ```js class Sink { // Appends content to the . appendToHead(content) // Appends content to the . appendToBody(content) // Appends content to the identified element. appendToElementById(id, content) // Replaces the content of the identified element. renderIntoElementById(id, content) // Redirects request to new location. redirect(location, code) // server only methods // sets the status code of the response. setStatusCode(code) // sets a header of the response. setHeader(key, value) // gets request headers getHeaders() // gets request cookies getCookies() } ``` The `sink` object may also expose additional properties depending on the environment. For example, on the server, `sink.request` provides access to the current `request` object, and `sink.arch` identifies the target architecture of the pending HTTP response (e.g. "web.browser"). Here is a basic example of `onPageLoad` usage on the server: ```js import from "react"; import { renderToString } from "react-dom/server"; import { onPageLoad } from "meteor/server-render"; import App from "/imports/Server.js"; onPageLoad(sink => { sink.renderIntoElementById("app", renderToString(