Giter VIP home page Giter VIP logo

react-play's Introduction

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>

react-play's People

Contributors

ssorallen avatar

Stargazers

 avatar

Watchers

 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.