10

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?

yogupta
  • 188
  • 1
  • 3
  • 9
  • 1
    It's not possible. You need node.js to perform server-side rendering for React or any other Single Page Application framework. – JEY Dec 06 '18 at 13:21
  • 1
    thanks for your comment. can you provide a link or some resource pointing to your statement? – yogupta Dec 06 '18 at 13:50
  • My bad you need the js to be interpreted on server side. Maybe you can use nashorn to do so in java. Take a look at this repo https://github.com/sdeleuze/spring-react-isomorphic from spring team. You will find more example to do so with node.js – JEY Dec 06 '18 at 14:00
  • They have it configured in this tutorial https://spring.io/guides/tutorials/react-and-spring-data-rest/ but they are not rendering it server side https://stackoverflow.com/questions/46388210/server-side-rendering-react-using-spring-boot. I want it to render it on server and then send the html to the client(more like a isomorphic apps). – yogupta Dec 06 '18 at 14:10
  • Well that exactly what is done in the project I gave you. They setup a ScriptTemplateConfigurer and a ScriptTemplateViewResolver – JEY Dec 06 '18 at 14:20

2 Answers2

2

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

Rathore
  • 1,926
  • 3
  • 19
  • 29
  • Actually, Baledung has a nice article on this topic: https://www.baeldung.com/react-nashorn-isomorphic-app . You would have to execute JS code on the backend, through a JS engine run by Java, in order to populate some components before sending them to the client. The user gets more performance at the cost of your server computational resources. – Gabriel Robaina Jun 14 '20 at 00:24
0

GraajVM comes in super handy for server side rendering a react app with benchamarks proving comparable and outstanding results with respect to V8. Bellow comes as a superb implementation: https://github.com/davehancock/thymeleaf-reactjs-graalvm

user3250183
  • 506
  • 6
  • 19