mauricedb / server-side-rendering-with-create-react-app

Server-side Rendering with Create-React-App
https://medium.com/@maurice.de.beijer/react-server-side-rendering-with-webpack-c4da49c87c34
MIT License
32 stars 16 forks source link

How to add support here React Router 4 #1

Closed gHashTag closed 6 years ago

gHashTag commented 6 years ago

Very interesting solution, but how to add support to it React Router 4 This is docs


const context = {}

  const html = ReactDOMServer.renderToString(
    <StaticRouter
      location={req.url}
      context={context}
    >
      <App/>
    </StaticRouter>
  )
mauricedb commented 6 years ago

@gHashTag

Sorry for the delay, missed your message for some reason.

Ust add the StaticRouter code snippet here.

You will need to change the code here. Last time I checked using renderToNodeStream(reactElement) didn't work with the StaticRouter context. And logically it coudn't really because by the time you can check context.url for a redirect most of the page has been streamed to the client.

const context = {};
const markup = renderToString(reactElement);

if (context.url) {
  // Somewhere a `<Redirect>` was rendered
  redirect(301, context.url);
} else {
  re.write(markup);
}