Search code examples
javareactjstomcatnashorn

Universal Reactjs with Nashorn and Tomcat


For my current project I'm trying to create a universal reactjs website. My current server architecture restricts me to only use Tomcat application servers. Installing a nodejs server is not an option.

As a POC I use an example repository (https://github.com/pgrimard/spring-boot-react) that contains react with react-router that renders on the server. While running this example I get the following error:

org.springframework.scripting.support.StandardScriptEvalException: TypeError: 0 ,> u.createServerRenderContext is not a function in <eval> at line number 1

This error happens in the server.js file while setting up the react-router context. Does anyone have any experience with bigger react application within a Tomcat container and that use SSR? Or are there any other solutions?

Thanks in advance!


Solution

  • I published an article about using nashorn to do react SSR. https://medium.com/@jimmy_shen/use-nashorn-engine-to-do-server-side-rendering-with-react-eba835e33d77 or 使用Nashorn Engine进行React Server-Side Rendering

    The key is the polyfill to Nashorn about XmlHttpRequest and WebAPI. nashorn-polyfill will do it.

    And you may check out Demo. The app is with stack of React, Apollo, React-Router v4.