• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    JavaScript
  • Created over 10 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

A poc of rendering a React single page app on the server for the initial page load.

react-nashorn-example

Blog post: http://augustl.com/blog/2014/jdk8_react_rendering_on_server/

This is a single page web app, where the initial page is also served as plain HTML, without any extra work or code duplication required!

When you click links in the web site (and JS is enabled), the single page web app will do all the page loading with JS. But because the initial page (and all pages) is loaded from the backend, your app will be indexable by Google, and also really fast! You don't have to wait for the JS to load to see the content, since it's served statically from the backend.

Running

You need JDK1.8, see next section if you don't have it.

Run the app with:

lein server

Then open http://localhost:4567 in a browser, and enjoy!

Leiningen is Clojure's build tool, if you don't have it, see https://github.com/technomancy/leiningen

Installing JDK1.8

Linux

Use your package manager!

Alternatively, if you don't want to replace your system java, do the following:

  • curl -LO -b oraclelicense=a http://download.oracle.com/otn-pub/java/jdk/8-b132/jdk-8-linux-x64.tar.gz
  • For 32bit, replace x64 in the end of the URL with i586
  • Extract
  • export PATH=/path/to/jdk1.8.0/bin:$PATH

Mac OS X

Please contribute a pull request :)

Windows

Please contribute a pull request :)

Tradeoffs

Your React code has to use plain JS only, and not invoke any 3rd party components that requires a DOM. Facebook React supports rendering to a string without a DOM, which is what we actually do on the back-end. If a component invokes a jQuery plugin, you'll get an exception and the backend won't be able to render that page.

I'm not an experienced React user, so I'm not sure how this problem is typically solved. Pull requests are welcome.

TODO

This is a POC. It would be nice with some kind of reusable library. That can probably happen when this todo-list becomes empty.

  • Use a real router, not a simple if/return/regexp based home made thingie.
  • Is it OK that the JS performs a request to the API for the data a second time, when the JS loads? Time has passed so you risk getting different data. Serializing the props used in the HTML is a possibility. Using Datomic is another, put the datomic T that was used by the backend in the HTML somewhere so the scripts can read and use it.

Copyright

Copyright © 2014 August Lilleaas

More Repositories

1

net-http-cheat-sheet

A collection of Ruby Net::HTTP examples.
Ruby
1,149
star
2

halt

OS where everything is immutable! (Experimental)
Rust
273
star
3

ruby-openssl-cheat-sheet

A collection of use cases with examples for Ruby's OpenSSL bindings.
Ruby
247
star
4

js-epub

EPUB library for Javascript
233
star
5

live-validations

No longer maintained.
213
star
6

js-unzip

Unzip files with JavaScript
123
star
7

booktorious

Proof of concept JavaScript+web browser EPUB reader
JavaScript
84
star
8

js-inflate

Inflate (uncompress) blobs compressed with the deflate algorithm
80
star
9

ruby-prowl

Ruby interface to Prowl, http://www.prowlapp.com/
Ruby
68
star
10

unobtrusive-google-maps

jQuery plugin. Unobtrusively load interactive Google maps on top of static JPEG Google maps.
32
star
11

concui

Experiment..
Clojure
30
star
12

ansible-playbooks

Collection of my ansible-playbooks for dev setups
Shell
14
star
13

nodejs-sandboxed-fs

A wrapper around the core 'fs' module that allows for sandboxed access by whitelisting or blacklisting
JavaScript
11
star
14

path-travel-agent

Java path matching lib (HTTP, ...)
Java
9
star
15

binbin

Port of some old stuff that contained some (useful?) scripts that some people have linked to from elsewhere
Ruby
7
star
16

augustl.com

HTML
4
star
17

traffic-police

A Ring (++) routing library for Clojure
Clojure
4
star
18

react-native-camera-ios-native-module-issue

A react native app where NativeModules.CameraManager is null, and I don't understand why
Objective-C
2
star
19

gradle-warlike-plugin

Groovy
1
star
20

.emacs.d

Emacs Lisp
1
star
21

dbs-are-fn

The content and code samples for the functional database blog dbs-are-fn.com
Clojure
1
star