Git Product home page Git Product logo

done-ssr's Introduction

Build Status npm version

done-ssr

Greenkeeper badge

Server-side rendering for DoneJS.

Install

npm install done-ssr --save

Usage

done-ssr takes a steal configuration object (the same object used by steal-tools to configure building) and returns a function that will render requests.

Pass your request into the render function and pipe the resulting stream into the response.

var http = require("http");
var ssr = require("done-ssr");
var render = ssr();

var server = http.createServer(function(request, response){
	render(request).pipe(response);
});

server.listen(8080);

Your app

done-ssr expect's your project's main to export a function that renders based on the request parameter. This will work with any module format supported by Steal.

var ViewModel = can.Map.extend( { ... });

module.exports = function(request){
  var params = can.route.deparam(location.pathname);
  var viewModel = new ViewModel(params);

  // Do whatever is needed to render
};

The request parameter is the raw Node request object so you can do things like get request headers if needed.

Additionally the location object is set globally so you can use it like you would in a browser window.

More can be found in the main module docs.

Express Middleware and Development Server

As of 0.12 can-ssr was renamed to done-ssr. The Express middleware and can-serve functionality were moved to their own projects:

API

ssr(steal, options) -> render

The ssr function contains two arguments, one for the steal object and one is an options object:

steal

Configuration options that are a StealConfig. This is the same object that is passed into steal-tools to configure the loader for building.

options

timeout : 5000

Specify a timeout in milliseconds for how long should be waited before returning whatever HTML has already been rendered. Defaults to 5000

auth: {cookie, domains}

An object for enabling JavaScript Web Tokens (JWT) support for XHR requests made by the SSR server. Must contain both of the following values:

  • cookie: A string representing the cookie name where the SSR server can look to find a JWT token. That token gets applied as the "Bearer" token in the authorization header of every outgoing XHR.

For example, if the SSR server receives a cookie like feathers-jwt=<token>;, and the authCookie option is set to "feathers-jwt", outgoing requests from the SSR server will have an authorization header of Bearer <token>

  • domains: An array of domain names to which the JWT token will be sent. Any domains not in this list will not receive the JWT token.
debug : false

Specify to turn on debug mode when used in conjunction with timeout. If rendering times out debugging information will be attached to a modal window in the document. For this reason you only want to use the debug option during development.

debug output

strategy: 'safe'

Specify the rendering strategy. In done-ssr 1.1.0 the new incremental rendering strategy was added which works by returning initial HTML immediately and incrementally updating the DOM in the client. To enable incremental rendering set this option:

var render = ssr(steal, {
  strategy: "incremental"
});
html5shiv : false

Specifies where html5shiv should be configured and attached to the document's head. This will automatically register all of the custom can.Component elements for you when using IE8.

render(request)

The render function is returned from the call to ssr and is what used to render requests. It returns a readable stream that can be piped into other streams, using the response stream.

render(request).pipe(response);

You can use request/response streams from servers created with require("http"), or Express and probably most other Node servers.

License

MIT

done-ssr's People

Contributors

matthewp avatar daffl avatar phillipskevin avatar greenkeeper[bot] avatar marshallswain avatar cleong-tc avatar chasenlehara avatar obaidott avatar bigab avatar alfredo-delgado avatar frank-dspeed avatar justinbmeyer avatar nlundquist avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.