I don’t have any experience using React with Java/Nashorn, but similar principles apply to using it with any non-JS languages. You can run the renderer in a more stripped back JS engine (nashorn in this case), but you’ll hit a lot of integration issues (no commonjs support) and you’ll have to roll a lot of things yourself.
A standalone node process is a good starting point. If/when you hit perf issues, node’s cluster module gives you an escape hatch as you can spawn a worker farm that listens from a single address.
The little server that I usually dump into dev boxes is https://github.com/markfinger/python-react/blob/acf86bb5b7d18538b986cee7c64902afa3fb1a4d/examples/basic_rendering/render_server.js
Thank you @markfinger for the insight and for pointing us towards a valuable solution. We’ve come up with three options for doing server-side rendering of reactJS components in our Java front-end servers:
- Delegate execution of react to an external process running Node
- Have Node run as a smart-proxy in front of Java
- Run react on the JVM with Nashorn
I wanted to get a sense of how many folks have actually done option 3 (as opposed to running a standalone node process).
@maximenajim I also started searching for this in the past days and am currently experimenting to come up with a Spring Boot - React Starter Kit. Would you care sharing information in this thread if you find something interesting?
Now in first tests that works, but it’s currently too slow (will look into that before going on with other issues). From what I’ve read one of the problems is that Nashorn is not thread-safe and thus the pre-compiled react can’t easily be shared across multiple threads (some ideas about perf in the link you’ve also posted).
[Update: as mentioned in the linked post once all workers have warmed up performance is good. With a small pooling solution the warmup-run should not affect end-users.]
FWIW, some of the links I find interesting in that context:
- Isomorphic templating with Spring Boot, Nashorn and React by Sébastien Deleuze
- Github Repo from the slides
- Go Starter Kit (not Java, but still interesting as it’s a fully integrated approach backed by a different server language and also utilizing a JS engine)
- Nashorn in the future
I successfully run react server-side rendering on Nashorn and performance is good for me. 40 requests per second on a Macbook Pro 4 core, SSD, JVM memory usage is around 1GB.
Check out more details on https://medium.com/@jimmy_shen/use-nashorn-engine-to-do-server-side-rendering-with-react-eba835e33d77