Reputation: 455
I want to create a server side react app, and I have a few doubts:
1) When you use renderToString from react-dom/server, do you also need to use webpack to build the project , or this does the job ?
2) do you need to add the
<script src="/bundle.js"></script>
to the html ? if so , why ? isnt renderToString already passing this file as a string ? thanks
Upvotes: 0
Views: 1113
Reputation: 452
Adding to @Brendan's answer (he correctly mentions the use of webpack):
In case you're just starting with SSR, this article might help: https://medium.com/@gagan_goku/react-and-server-side-rendering-ssr-444d8c48abfc. Have a simple hack that you might want to try out to make life simpler.
Upvotes: 1
Reputation: 2652
These two things are unrelated. All renderToString
does is get what your React component renders, as a string of HTML (instead of creating objects to represent the components and attempting to render them into the DOM, as it would on the client). It returns that string. React is typically only used to render elements inside the <body>
, so normally you would be rendering the contents of your root React component, as a string, and then inserting that string into some sort of template that includes your doctype, <html>
, <head>
, and <body>
elements. Then it's up to you to have your server return this string of text that makes up the HTML document.
The need to use webpack and the need to include the JS app as a <script>
in the HTML that you send to the client are also unrleated. Normally yes, you need to do both.
Upvotes: 1