Sending a template with markup and CSS

Once the markup has been generated, we need to check if there was a redirect rendered in the component to be sent in the markup. If there was no redirect, then we get the CSS string from sheets using sheets.toString, and, in the response, we send the Template back with the markup and CSS injected, as shown in the following code.

mern-skeleton/server/express.js:

if (context.url) {
return res.redirect(303, context.url)
}
const css = sheets.toString()
res.status(200).send(Template({
markup: markup,
css: css
}))

An example of a case where redirect is rendered in the component is when we're trying to access a PrivateRoute via a server-side render. As the server-side cannot access the auth token from the browser's sessionStorage, the redirect in PrivateRoute will render. The context.url value , in this case, will have the '/signin' route, and hence, instead of trying to render the PrivateRoute component, it will redirect to the '/signin' route.

This completes the code we need to add to the server-side to enable the basic server-side rendering of the React views. Next, we need to update the frontend so it is able to integrate and render this server-generated code. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset