• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 7 years ago

Reviews

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

Repository Details

Render React components in the Play Framework with JDK8's JavaScript engine

React.js on the Play Framework

With these powers combined, Play can use the same JavaScript sent to the client to render its templates on the server.

To try it out:

  1. Install JDK8 for your platform
  2. Clone this repository
  3. Follow the instructions to install and get started with Play 2.3.x
  4. Run the app with play run
  5. View http://localhost:9000/ in your browser

What am I looking at?

This app is using the JavaScript from my React Reddit Client but with one key, huge, fantastic change: the first view you see is rendered on the server.

The magic is in the Nashorn

The part that makes this all work is the Nashorn JavaScript engine that ships with Java 8. By eval'ing components on the server, React can produce a string that's rendereable as plain old HTML.

The server:

def index = Action {
  // Pass 'null' to force the correct class loader. Without passing any param,
  // the "nashorn" JavaScript engine is not found by the `ScriptEngineManager`.
  //
  // See: https://github.com/playframework/playframework/issues/2532
  val engine = new ScriptEngineManager(null).getEngineByName("nashorn")

  if (engine == null) {
    BadRequest("Nashorn script engine not found. Are you using JDK 8?")
  } else {
    // React expects `window` or `global` to exist. Create a `global` pointing
    // to Nashorn's context to give React a place to define its global namespace.
    engine.eval("var global = this;")

    // Evaulate React and the application code.
    engine.eval(new FileReader("target/web/web-modules/main/webjars/lib/react/react-with-addons.js"))
    engine.eval(new FileReader("target/web/public/main/javascripts/components/App.js"))

    Ok(views.html.main("React on Play") {
      play.twirl.api.Html(engine.eval("React.renderToString(React.createElement(App));").toString)
    })
  }
}

The client receives the same React and App JavaScript files and executes the same code the server did. Unlike most other use cases, it will be working on pre-rendered HTML as opposed to an empty container.

The client:

<div id="application">
  @content
</div>
<script src="@routes.WebJarAssets.at(WebJarAssets.locate("react-with-addons.min.js"))"></script>
<script src="@routes.Assets.at("javascripts/components/App.js")"></script>
<script>
  React.render(
    React.createElement(App, null),
    document.getElementById("application")
  );
</script>

More Repositories

1

turbo-react

A JavaScript library that transitions between static HTML pages on navigation; no app server required.
HTML
273
star
2

jquery-scrollstop

A jQuery plugin that fires events when scrolling stops and starts.
HTML
156
star
3

finance

🤑 A free, fast, in-browser stock portfolio tracker
JavaScript
69
star
4

react-reddit-client

A Reddit client written with React.js
TypeScript
40
star
5

react-todos

Backbone's example TODO app with React Components for views
JavaScript
29
star
6

redux-persist-webextension-storage

A WebExtension Storage storage engine for redux-persist
JavaScript
29
star
7

hackernews-react

A Hacker News API Client in React + Firebase
JavaScript
19
star
8

googly-eyed-mona-lisa

Mona Lisa's eyes follow your face via your computer's camera
HTML
7
star
9

turbo_react-rails

TurboReact for the Rails Asset Pipeline.
Ruby
6
star
10

dotfiles

Ross Allen's dotfiles and preferred settings
Shell
4
star
11

blob-feature-check

Small feature check for "blob:" URL support.
JavaScript
4
star
12

blacklotusproject

Parts of Black Lotus Project that might be useful for other people interested in some Magic development.
JavaScript
3
star
13

cat-face

A custom element for delivering happiness via Cat Face episodes.
HTML
2
star
14

vanillajs-keyboard

A keyboard rendered with JS and no 3rd-party libraries
JavaScript
2
star
15

polymer-reddit-client

A simple Reddit client written with Polymer.js.
HTML
1
star
16

camtime

Take and review pictures all from inside your mobile web browser
JavaScript
1
star
17

gifs-r-us

All the GIFs you'll ever need are at Gifs-R-Us
TypeScript
1
star
18

airdryer

A Chrome Extension that makes airbrake.io a little more friendly
JavaScript
1
star