jsrenderer

package
v0.1.1-0...-25437bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2016 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package jsrenderer is an abstract API for server-side react rendering with several implementations.

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeOut = errors.New("Timed out")

The javascript rendering engine timed out.

Functions

This section is empty.

Types

type Params

type Params struct {
	// Url is used directly by react-router to determine what to render.
	Url string `json:"url"`
	// Headers specifies additional headers to be included for any HTTP requests
	// to the local server during rendering.  For exampe, if the rendering code
	// needs to make an authenticated API call, it's important that the
	// authentication be made on behalf of the correct user.
	Headers http.Header `json:"headers"`
	UUID    string      `json:"uuid"`
}

Params describe the options that can be pass to the react rendering code.

type Pool

type Pool struct {
	New func() Renderer
	// contains filtered or unexported fields
}

Pool is a dynamically-sized pool of Renderers that itself implements the Renderer interface. You must provide a New() function that will construct and initialize a new Renderer when necessary. It will grow the pool as necessary to accommodate rendering demands, and will tend to re-use renderers as much as possible. A Pool Renderer is safe for concurrent use.

func (*Pool) Render

func (p *Pool) Render(params Params) (Result, error)

type Renderer

type Renderer interface {
	Render(Params) (Result, error)
}

Renderer is the primary interface for rendering react content. Given a set of parameters, it will execute the react js code and return the result and error (if any). A given Renderer is typically NOT SAFE for concurrent use. Instead, use a Pool.

func NewDefaultOrDie

func NewDefaultOrDie(jsCode string, local http.Handler) Renderer

NewDefaultOrDie constructs and initializes a Renderer with the default implementation for the current platform. On windows the default implementation is the duktape-based renderer. On any other platform, the default is the V8-based renderer.

func NewDukTape

func NewDukTape(jsCode string, local http.Handler) (Renderer, error)

NewDukTape constructs a Renderer backed by the duktape javascript engine. A given Renderer instance is not safe for concurrent usage.

The provided javascript code should be all of the javascript necessary to render the desired react page, including all dependencies bundled together. It is assumed that the react code exposes a single function:

main(params, callback)

where params corresponds to the Params struct in this package and callback is a function that receives a Result struct serialized to a JSON string.

In addition to the javascript code, you should also provide an http.Handler to call for any requests to the local server.

func NewV8

func NewV8(jsCode string, local http.Handler) (Renderer, error)

NewV8 constructs a Renderer backed by the V8 javascript engine. A given Renderer instance is not safe for concurrent usage.

The provided javascript code should be all of the javascript necessary to render the desired react page, including all dependencies bundled together. It is assumed that the react code exposes a single function:

main(params, callback)

where params corresponds to the Params struct in this package and callback is a function that receives a Result struct serialized to a JSON string. For example:

function main(params, callback) {
  result = { app: '<div>hi there!</div>', title: '<title>The app</title>' };
  callback(JSON.stringify(result));
}

In addition to the javascript code, you should also provide an http.Handler to call for any requests to the local server.

type Result

type Result struct {
	// The rendered react content, if any.
	Rendered string `json:"app"`
	// The URL that the client should be redirected to (if non-empty).
	Redirect string `json:"redirect"`
	// The title of the page.
	Title string `json:"title"`
	// Meta HTML tags that should be included on the page.
	Meta string `json:"meta"`
	// Initial JSON data that should be included on the page, if any.
	Initial string `json:"initial"`
}

Result is returned from a successful react rendering.

func (Result) HTMLApp

func (r Result) HTMLApp() template.HTML

func (Result) HTMLMeta

func (r Result) HTMLMeta() template.HTML

func (Result) HTMLTitle

func (r Result) HTMLTitle() template.HTML

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL