Skip to content
Matthew Phillips edited this page Nov 20, 2015 · 8 revisions

can-ssr is able to handle server-side rendering of your application by loading it in a custom module loader based on StealJS. In order to work can-ssr expects a couple of functions to be implemented by your application's main (defined in your package.json). These functions look like (note that the examples are in ES6, but all formats are supported):

main.js

import initApp from './init';

export function createState(request) {
  return { 
    page: request.url.split("/").pop()
  };
}

export function render(document, state) {
  let title = document.createElement("title");
  title.appendChild(document.createTextNode("Hello world"));
  document.head.appendChild(title);

  document.body.appendChild(initApp(state));
}

createState

The main must export a function createState which receives a request object and returns a state object. What type of object depends on the framework you are using, but this is an object that represents the application state for a given url.

render

render is a function that receives a document (which looks just like window.document and the state object created in createState.

Clone this wiki locally