Reputation: 328
I'm new to server side rendering & I want to render React components on server side and pass down it in html.
I followed this tutorial on https://spring.io/guides/tutorials/react-and-spring-data-rest/. It is still rendering on client.
I cannot find a way to do renderToString
in the thymeleaf template.
How do I do this?
Upvotes: 14
Views: 25612
Reputation: 516
GraalVM comes in super handy for server side rendering a React app with benchmarks proving comparable and outstanding results with respect to V8. Below comes as a superb implementation: https://github.com/davehancock/thymeleaf-reactjs-graalvm
An example project demonstrating how to render both vanilla Thymeleaf templates and ReactJS components together.
Upvotes: 0
Reputation: 1
It won't be good in term of resources to run V8 engine to render react application on server side. Best approach is to make pre-render HTML page by Java spring and return such page to client with adjusted React to run on such page
Upvotes: 0
Reputation: 1988
Using Nashorn you can do, Nashorn is a JavaScript engine packaged by default with Java.
Nashorn has been released as part of Java 8. This is primarily focused on allowing embedded JavaScript applications in Java.
Nashorn compiles JavaScript in-memory to Java Bytecode and passes it to the JVM for execution.
And more details you can check below link:-
https://www.baeldung.com/react-nashorn-isomorphic-app.
**
Nashorn is deprecated now, so you can use GraalVM for this
**
https://medium.com/graalvm/improve-react-js-server-side-rendering-by-150-with-graalvm-58a06ccb45df
Upvotes: 3