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.

Create React App builds empty document with react-router

See original GitHub issue

I am using the current version of Create React App and I’m having a weird problem. I just started using react-router and everything is working perfectly in npm start. However, when I build the file with npm run build, the resultant HTML is entirely blank. The #root element exists, but contains only a comment with the text react-empty: 1.

This behavior is consistent regardless of how I set the homepage field in package.json, and whether I open it locally or host it on my website.

My app.js file is as follows:

... imports ...
ReactDOM.render(
    (<Router history={browserHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={Home} />
            <Route path="/news" component={News} />
            <Route path="/current" component={Current} />
            <Route path="/previous" component={Previous} />
            <Route path="/about" component={About} />
            <Route path="/enter" component={Enter} />
            <Route path="/login" component={Login} />
            <Route path="/profile" component={Profile} />
        </Route>
    </Router>),
    document.querySelector('#root')
);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
gaearoncommented, Feb 23, 2017

Your route definition is declared against /. But your app is served from /hypercomp/. React Router doesn’t know anything about the homepage setting, so it can’t match the URLs.

You can work around this by doing something like:

const baseUrl = process.env.PUBLIC_URL; // will be /hypercomp
ReactDOM.render(
    (<Router history={browserHistory}>
        <Route path={baseUrl + "/"} component={App}>
            <IndexRoute component={Home} />
            <Route path={baseUrl + "/news"} component={News} />
            <Route path={baseUrl + "/current"} component={Current} />
            <Route path={baseUrl + "/previous"} component={Previous} />
            <Route path={baseUrl + "/about"} component={About} />
            <Route path={baseUrl + "/enter"} component={Enter} />
            <Route path={baseUrl + "/login"} component={Login} />
            <Route path={baseUrl + "/profile"} component={Profile} />
        </Route>
    </Router>),
    document.querySelector('#root')
);

In the future, we might change it so that the app would get served from the relative path in development too, so that you’d learn about the problem earlier. See discussion in https://github.com/facebookincubator/create-react-app/issues/1582.

Hope this helps!

2reactions
gaearoncommented, Apr 23, 2017

Also consider sharing what your mistake was. Somebody else might repeat it 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create React App builds empty document (blank screen) with ...
The const 'condition' in the connectScreen component is undefined. Your authentication logic then prevents the component from rendering.
Read more >
Create a New React App
Create React App doesn't handle backend logic or databases; it just creates a frontend build pipeline, so you can use it with any...
Read more >
React | IntelliJ IDEA Documentation - JetBrains
The recommended way to start building a new React single page application is create-react-app package, which IntelliJ IDEA downloads and runs ...
Read more >
Create a React App with React Router Dom v6
Hi guys! In this post, I will be giving a complete walkthrough on how to create a React App with the help of...
Read more >
How To Build A React JS Website With React Router DOM
Follow along and see how I built this data security website in React JS. We will be using a couple react packages such...
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