14

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?

5
  • 2
    It's not possible. You need node.js to perform server-side rendering for React or any other Single Page Application framework. Commented Dec 6, 2018 at 13:21
  • 1
    thanks for your comment. can you provide a link or some resource pointing to your statement? Commented Dec 6, 2018 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 github.com/sdeleuze/spring-react-isomorphic from spring team. You will find more example to do so with node.js Commented Dec 6, 2018 at 14:00
  • They have it configured in this tutorial spring.io/guides/tutorials/react-and-spring-data-rest but they are not rendering it server side stackoverflow.com/questions/46388210/…. I want it to render it on server and then send the html to the client(more like a isomorphic apps). Commented Dec 6, 2018 at 14:10
  • Well that exactly what is done in the project I gave you. They setup a ScriptTemplateConfigurer and a ScriptTemplateViewResolver Commented Dec 6, 2018 at 14:20

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, Baledung has a nice article on this topic: 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.
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.