question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-router v4 with Webpacker & SSR

See original GitHub issue

Steps to reproduce

React v5 with latest version of react-rails and using webpacker to compile assets (loaded through javascript_pack_tag and react_component)

Install react-router v4: yarn add react-router

Attempt to add some dynamic or static routes using <BrowserRouter> or <StaticRouter>.

Expected behavior

Be able to use <BrowserRouter> and/or <StaticRouter>. I know there is some documentation in the Wiki but it’s related to v3 of the router and it’s using sprockets.

Actual behavior

For <BrowserRouter>:

Encountered error "#<ExecJS::ProgramError: Invariant Violation: Browser history needs a DOM>" when prerendering REDACTED with {"title":"This is the title","body":"This is the body"}
invariant (webpack-internal:///3:40:15)
createBrowserHistory (webpack-internal:///62:49:27)
new BrowserRouter (webpack-internal:///60:38:231)
resolve (webpack-internal:///48:2085:14)
ReactDOMServerRenderer.render (webpack-internal:///48:2242:22)
ReactDOMServerRenderer.read (webpack-internal:///48:2216:19)
Object.renderToString (webpack-internal:///48:2483:25)
Object.serverRender (webpack-internal:///33:70:42)
eval (eval at <anonymous> ((execjs):1155:8), <anonymous>:6:45)
eval (eval at <anonymous> ((execjs):1155:8), <anonymous>:18:13)

For <StaticRouter>: the links either don’t work or Rails is trying to get to the linked URL and fail to load?

System configuration

Webpacker version: 3.0 React-Rails version: 2.4 Rect_UJS version: 2.4 Rails version: 5.1.4 Ruby version: 2.4.2


Trying to integrate react-routes v4 with a Webpacker setup. Can’t seem to find any documentation on how to do it with v4 and it doesn’t seem to work. If anyone got them to work, some guidance would me much appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
ersinakincicommented, Jan 5, 2018

I was having similar problems. What you need to do is make sure that your app renders with a <StaticRouter> when prerendering and a <BrowserRouter> when running in a web browser. Here’s my solution:

App view

<%= react_component('Application', {path: request.path}, {prerender: true}) %>

App root component

import { BrowserRouter, StaticRouter } from 'react-router-dom'

class Router extends React.Component {
  renderRouter = () => {
    if(typeof window !== 'undefined') {
      return(
        <BrowserRouter>
          {this.props.children}
        </BrowserRouter>
      )
    } else {
      return(
        <StaticRouter location={this.props.path} context={{}}>
          {this.props.children}
        </StaticRouter>
      )
    }
  }

  render() {
    return(this.renderRouter())
  }
}

const Application = props => (
  <Router path={props.path}>
    <h1>Hello, world!</h1>
  </Router>
)

export default Application
2reactions
ersinakincicommented, Jan 5, 2018

Side note: React Router’s documentation misleadingly implies that you should pass in the URL into <StaticRotuer>'s location prop, whereas you actually need to pass in the path. (I banged my head against that one for a while.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Rendering with React and React Router v4 - ui.dev
In this comprehensive post you'll learn how to add server side rendering to an app built with React and React Router v4.
Read more >
React SSR + Redux + code splitting + Webpack 4.xx ... - Medium
Almost everyone who is going to start their React project they having problem to merge all latest package with react js and deploy...
Read more >
Running React Router v4, Redux Saga, SSR and Code ...
We are currently migrating a customer project from a Symfony application to a React application with server-side rendering.
Read more >
Server Rendering with React and React Router v4 - YouTube
Hi. I hope you liked this video. If you didn't, well, that's unfortunate. Anyway, you should checkout Bytes - it's our take at...
Read more >
ReactJS : Server side rendering with router v4 & redux
Here <BrowserRouter> is a new component provided by react router which uses HTML5 history API. The above setup is used only on client...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found