• This repository has been archived on 03/Mar/2023
  • Stars
    star
    133
  • Rank 271,613 (Top 6 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

an HTTP server that uses PhantomJS to render HTML

rndr.me is a tiny http server that eats urls and poops html. It has only one dependency, PhantomJS, which it uses evaluate each incoming url in a headless browser window, and output the html of the resulting DOM.

Build Status

Having an easy, framework-agnostic way to create html snapshots helps solve two problems in single-page JavaScript app deployment:

  1. Single-page apps suffer from poor crawlability, because Google/Bing are less likely to discover content rendered on the client. In this case, use rndr.me to render the _escape_fragment_ urls that these crawlers want, by redirecting from your backend.

  2. Single-page apps suffer from slow startup times, due to multiple round trips between the app and API. In this case, you can use rndr.me to pre-render hot pages, so that they can be inlined as HTML to improve perceived performance.

Of course, this is just one approach for folks looking for way to improve SEO and performance when rendering single-page apps. If you're using Backbone.js and want a more tightly coupled solution, check out @airbnb's rendr. If you're looking for something a bit higher-level that'll run a cluster for you, check out @bfirsh's otter.

Installation

  1. Install PhantomJS.
  2. Download server.js from this repo OR npm install rndr-me

Sample setup

In this example shell script, we:

  • create a simple JavaScript app,
  • serve it on port 8000,
  • run rndr.me on port 8001, and
  • verify the app html as rendered by rndr.me.
#!/bin/sh

# Create and save a simple JavaScript app
echo "<script>document.write(location)</script>" > ./index.html

# Spin up a server to serve it
python -m SimpleHTTPServer 8000 &
APP_PID=$!

# Spin up the rndr.me server, wait until ready
phantomjs ./server.js &
rndrme_PID=$!
sleep 1

# Pick an app URL to be rendered
URL='http://127.0.0.1:8000/#!/TESTING'

# Get the results rendered by the rndr.me server
HTML=`curl 127.0.0.1:8001 -s -G --data-urlencode href=$URL`

# Check whether the rendered file contains the random URL
echo $HTML | grep -q $URL
NOT_FOUND=$?

# Spin down, clean up, and exit
rm ./index.html
kill -9 $rndrme_PID
kill -9 $APP_PID
exit $NOT_FOUND

Quickstart on Heroku

Because rndr.me depends only on PhantomJS, it's easy to set up and run yourself. This example shell script shows you everything you need to start your own instance running on Heroku.

# Create a place for your renderer to live
mkdir my_renderer
cd my_renderer

# Create a git repo with rndr.me and a Procfile
git init
git submodule add git://github.com/jed/rndr.me.git
echo "web: phantomjs rndr.me/server.js" > Procfile

# Create a new Heroku app with the PhantomJS buildpack
heroku apps:create
heroku config:add BUILDPACK_URL=https://github.com/stomita/heroku-buildpack-phantomjs.git

# Push your code
git add .
git commit -m "first commit"
git push heroku master

# Scale your app
heroku ps:scale web=1

API

To spin up the server, run the following from the command line:

phantomjs ./server.js <config-path>

Note that config-path is optional, and if omitted will default to the provided config.js file. You may also override any options from the config file using options on the command line:

phantomjs ./server.js --port 8002 --ready_event onRender

The server exposes a single root endpoint at /. It returns generated html, based on the following parameters:

  • href: The url to be rendered. This is required, and must be fully qualified.
  • max_time: The maximum number of milliseconds until render. Any windows not already rendered by the ready_event will be rendered once this elapses. This is optional, and 30000 by default (30 seconds).
  • max_bytes: The maximum number of incoming bytes. Any windows that load more than this value will return an error without rendering. This is optional, and 1048576 by default (1 MiB).
  • load_images: This can be specified to any value to load document images. This is optional, and omitted by default.
  • ready_event: This is the name of the window event that triggers render. This is optional, and load by default. To specify when rendering occurs, such as when the DOM is not ready to be rendered until after window.onload, trigger a DOM event manually, such as follows (using jQuery in this case):
jQuery.getJSON("http://api.myapp.com", function(data) {
  myCustomRenderingCallback(data)

  var readyEvent = document.createEvent("Event")
  readyEvent.initEvent("renderReady", true, true)
  window.dispatchEvent(readyEvent)
})

Examples

The following examples assume a single-page app running in production at http:/myapp.com and rndr.me running as follows:

phantomjs ./server.js --port 8080

Let's render the app with default settings:

curl localhost:8080 -G \
  --data-urlencode 'href=http://myapp.com/#!home'

Now let's cap the maximum rendering time at 10 seconds:

curl localhost:8080 -G \
  --data-urlencode 'href=http://myapp.com/#!home'
  -d max_time=10000

We can also cap the maximum incoming bytes at 100KiB:

curl localhost:8080 -G \
  --data-urlencode 'href=http://myapp.com/#!home'
  -d max_time=10000
  -d max_bytes=102400

Now let's allow images to load, raising the maximum incoming bytes to 500KiB:

curl localhost:8080 -G \
  --data-urlencode 'href=http://myapp.com/#!home'
  -d max_time=10000
  -d max_bytes=512000
  -d load_images

Now let's use the custom rendering event render_ready, triggered on the window of the DOM, using the default fallback maximum time:

curl localhost:8080 -G \
  --data-urlencode 'href=http://myapp.com/#!home'
  -d max_bytes=512000
  -d load_images
  -d ready_event=render_ready

LICENSE

(The MIT License)

Copyright (c) 2013 Jed Schmidt <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

140bytes

Once a tweet-sized, fork-to-play, community-curated collection of JavaScript.
HTML
1,031
star
2

lave

eval in reverse: stringifying all the stuff that JSON.stringify won't
JavaScript
895
star
3

fab

a modular async web framework for node.js
JavaScript
732
star
4

browserver-client

ΰ·΄ A node.js HTTP server in your browser ΰ·΄
JavaScript
638
star
5

authom

A zero-dependency mutli-service authentication tool for node.js
JavaScript
401
star
6

browserver-node

ΰ·΄ Browserver proxy for node.js ΰ·΄
JavaScript
333
star
7

domo

Markup, style, and code in one language.
JavaScript
308
star
8

certbot-route53

Helping create Let's Encrypt certificates for AWS Route53
Shell
175
star
9

kibi

a client-side web framework in 1,024 bytes
JavaScript
144
star
10

dynamo

DynamoDB client for node.js
JavaScript
141
star
11

building-brooklynjs

My story of how we built BrooklynJS.
136
star
12

weenote

A quick/dirty/tiny tool for creating simple Takahashi-style presentations.
HTML
119
star
13

hyperspider

A declarative HATEOAS API crawler for node.js
JavaScript
114
star
14

sheet-down

A Google Spreadsheet implementation of leveldown
JavaScript
108
star
15

cookie-node

signed cookie functionality for node.js
JavaScript
82
star
16

config-leaf

Hide your sensitive node.js bits in plain sight.
JavaScript
53
star
17

sajak

Simple Authenticated JSON API Kit
JavaScript
33
star
18

dynamo-down

A leveldown API implementation on AWS DynamoDB
JavaScript
31
star
19

localhose

Hose your hosts file for easier local web development
JavaScript
29
star
20

browserver.org

The code for the browserver.org web site.
CoffeeScript
29
star
21

emit

A reactive toolkit for JavaScript
JavaScript
29
star
22

dynamo-client

A low-level client for accessing DynamoDB from node.js
JavaScript
28
star
23

osx-browser-vm

Evaluate JavaScript in local OS X browsers
JavaScript
28
star
24

dynamo-streams

A stream-flavored wrapper for the AWS DynamoDB JavaScript API
JavaScript
25
star
25

wc-jsx-runtime

A JSX transform for Web Components
JavaScript
23
star
26

tmpl-node

a template module for node.js
JavaScript
20
star
27

esbuild-plugin-http-fetch

An esbuild plugin that resolves http(s) modules
JavaScript
19
star
28

skeyma

A JavaScript parser & serializer for {key, value} objects & streams
JavaScript
18
star
29

browserver-router

A platform-agnostic router for HTTP listeners that follow the node.js spec
JavaScript
18
star
30

cfn-api-gateway-custom-domain

API Gateway custom domains as CloudFormation resources, backed by Let's Encrypt
JavaScript
18
star
31

alReady.js

a terse, embeddable, and cross-browser domReady implementation
JavaScript
17
star
32

electric-objects

A node.js API for the Electric Objects EO1 frame
JavaScript
17
star
33

monot

Unique JavaScript dates
JavaScript
15
star
34

google-worksheet-stream

A streaming interface for Google Spreadsheets
JavaScript
14
star
35

dinkumise

Keep ya JavaScripts Dinki-di!
11
star
36

pbsb

getters/setters and pub/sub in one tiny javascript function
JavaScript
11
star
37

twil-eo

Using Twilio, AWS, and Electric Objects to create an MMS-powered family photo frame.
JavaScript
10
star
38

textpanda

web-based text shortcuts for the lazy
JavaScript
8
star
39

diff-stream2

Merges multiple sorted streams into a diffed tuple stream
JavaScript
7
star
40

namedrop

minification for DOM-heavy code
JavaScript
7
star
41

eartag

Tag browsers like farmers tag livestock
JavaScript
7
star
42

autorequire

small module for autorequiring. warning: MAGIC!
JavaScript
6
star
43

sort-stream2

Array.prototype.sort for streams
JavaScript
6
star
44

dynamo-sync

Differential data synchronization for Amazon's DynamoDB
6
star
45

google-oauth-jwt-stream

A readable stream of OAuth access tokens for use with Google APIs
JavaScript
6
star
46

dom-jsx-runtime

A tiny library that turns JSX into DOM operations
JavaScript
6
star
47

node-lacrosse

A node.js streaming API for Lacrosse Alert sensors
JavaScript
6
star
48

abstract-stream-leveldown

A stream-based abstract prototype matching the LevelDOWN API
JavaScript
5
star
49

gist-in-time

a stylesheet for displaying github gist metadata on hover
5
star
50

typd.in

a web-based input method editor for japanese
JavaScript
4
star
51

esbuild-plugin-eval

An esbuild plugin that evaluates a module before importing it
JavaScript
4
star
52

one-character-identifiers

every javascript identifier that fits in a character
JavaScript
4
star
53

jed.is

personal web site
3
star
54

domogenize

Turn static HTML and CSS into declarative JavaScript
JavaScript
3
star
55

ramendan

CoffeeScript
3
star
56

census-topologies

Topologies from the U.S. Census Bureau
Shell
3
star
57

20x20

a simple pecha kucha timer optimized for my iPhone
JavaScript
3
star
58

utfn

a test of the utf-n encoding
3
star
59

esbuild-plugin-bundle

An esbuild plugin that bundles modules before importing them
JavaScript
2
star
60

deno_bundle

A temporary workaround to support bundling with sourcemaps in Deno
JavaScript
2
star
61

ordered-kv-tuple-stream

Aligns multiple ordered k/v readable streams into one
JavaScript
2
star
62

tuple-stream2

Aligns multiple readable object streams into one stream
JavaScript
2
star
63

esbuild-plugin-deno-http

An esbuild plugin that uses Deno to resolve and cache HTTP modules
JavaScript
2
star
64

esbuild-plugin-env

An esbuild plugin that exports the current environment as a module
JavaScript
2
star
65

deno-file-system-access-api

File System Access API for Deno
JavaScript
1
star
66

level-sync

One-way sync for levelup data stores
JavaScript
1
star
67

abevigoda

Abe Vigoda as a Service
JavaScript
1
star
68

esbuild-plugin-view-source

An esbuild plugin that enables built modules to be imported as source
JavaScript
1
star