From eb56cc7a4a7e132ece57fe14b79331901b6b8409 Mon Sep 17 00:00:00 2001 From: Jim Date: Wed, 13 Apr 2016 12:53:34 -0700 Subject: [PATCH] Added docs for environment integration. --- _data/nav_docs.yml | 2 + docs/09-tooling-integration.md | 2 +- docs/09.2-package-management.md | 2 +- docs/09.3-environments.md | 80 +++++++++++++++++++++++++++++++++ docs/10-addons.md | 2 +- 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 docs/09.3-environments.md diff --git a/_data/nav_docs.yml b/_data/nav_docs.yml index c679cb51..85d43105 100644 --- a/_data/nav_docs.yml +++ b/_data/nav_docs.yml @@ -53,6 +53,8 @@ title: Language Tooling - id: package-management title: Package Management + - id: environments + title: Server-side Environments - id: addons title: Add-Ons subitems: diff --git a/docs/09-tooling-integration.md b/docs/09-tooling-integration.md index a1e97e70..b6de0a38 100644 --- a/docs/09-tooling-integration.md +++ b/docs/09-tooling-integration.md @@ -10,4 +10,4 @@ We've tried to make React as environment-agnostic as possible. People use React * [Language Tooling](/react/docs/language-tooling.html) describes how to set up tools like Babel to transpile JSX for a better development experience. * [Package Management](/react/docs/package-management.html) describes how to configure React as a dependency of your project. - +* [Server-side Environments](/react/docs/environments.html) describes how to configure your environment for server-side rendering with React. diff --git a/docs/09.2-package-management.md b/docs/09.2-package-management.md index b19a176a..93599d25 100644 --- a/docs/09.2-package-management.md +++ b/docs/09.2-package-management.md @@ -3,7 +3,7 @@ id: package-management title: Package Management permalink: package-management.html prev: language-tooling.html -next: addons.html +next: environments.html --- ## CDN-hosted React diff --git a/docs/09.3-environments.md b/docs/09.3-environments.md new file mode 100644 index 00000000..a19aa2bf --- /dev/null +++ b/docs/09.3-environments.md @@ -0,0 +1,80 @@ +--- +id: environments +title: Server-side Environments +permalink: environments.html +prev: package-management.html +next: addons.html +--- + +One of the great things about React is that it doesn't require the DOM as a dependency, which means it is possible to render a React application on the server and send the HTML markup down to the client. There are a few things that React expects, so this guide will help you get started in your preferred environment. + + +## Nashorn + +Nashorn is a lightweight high-performance JavaScript runtime that runs within the JVM. React should run out of the box in Java 8+. + +Example: + +```java +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.apache.commons.io.IOUtils; // https://commons.apache.org/proper/commons-io/download_io.cgi + +public class ReactRender +{ + public static void main(String[] args) throws ScriptException, IOException { + ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn"); + + nashorn.eval(getScript("https://fb.me/react-15.0.1.js")); + nashorn.eval(getScript("https://fb.me/react-dom-server-15.0.1.js")); + + System.out.println(nashorn.eval("ReactDOMServer.renderToString(React.createElement('div', {}, 'Hello World!'));")); + } + + public static String getScript(String url) throws MalformedURLException, IOException { + InputStream stream = new URL(url).openStream(); + try { + return IOUtils.toString(stream); + } + finally { + stream.close(); + } + } +} +``` + +If your application uses node modules, or you want to transform JSX in Nashorn, you will need to do some additional environment setup. The following resources may be helpful in getting you started: + + * [http://winterbe.com/posts/2015/02/16/isomorphic-react-webapps-on-the-jvm/](http://winterbe.com/posts/2015/02/16/isomorphic-react-webapps-on-the-jvm/) + * [https://github.com/nodyn/jvm-npm](https://github.com/nodyn/jvm-npm) + * [https://gist.github.com/aesteve/883e0fd33390451cb8eb](https://gist.github.com/aesteve/883e0fd33390451cb8eb) + +>Note: Using Babel within Nashorn will require Java 8u72+, as update 72 fixed [JDK-8135190](https://bugs.openjdk.java.net/browse/JDK-8135190). + + + +## Node + +You can also use React within a node server-side environment. + +Example: + +```js +var React = require('react'); +var ReactDOMServer = require('react-dom/server'); + +var element = React.createElement('div', {}, 'Hello World!'); +console.log(ReactDOMServer.renderToString(element)); +``` + +>Note: Some versions of node have an `Object.assign` implementation that does not preserve key order. This can cause errors when validating the markup, creating a warning that says "React attempted to reuse markup in a container but the checksum was invalid". If you run into this issue, you can override `Object.assign` to use a polyfill that preserves key order. For more details, see [Issue #6451](https://github.com/facebook/react/issues/6451). + + + diff --git a/docs/10-addons.md b/docs/10-addons.md index 5aec660d..9911a005 100644 --- a/docs/10-addons.md +++ b/docs/10-addons.md @@ -2,7 +2,7 @@ id: addons title: Add-ons permalink: addons.html -prev: package-management.html +prev: environments.html next: animation.html ---