Deploy React App with React Router to Github Pages ( blank/404 page problem solved )

Hello guys ,

In this article we will explain you how to deploy react app with react routers on github pages.

Interesting thing about react-router is,  it won’t work directly on github pages, so this article is published.

Initially we will create a react app with CRA tool by facebook and deploy it on github pages.

Follow below article to create react app and deploy it on github .(click on the post here)

Now lets see the result.

Here Instead of loading home page ,it shows blank.

 This  is because a fresh page load for a url like ‘test_repository/’, where ‘/’ is a frontend route, the GitHub Pages server returns null because it knows nothing of ‘/’

Let’s solve blank page problem

We need to add basename to router as shown below and point it to repository name

<Router basename="/test_repository"> //add basename
      <Switch>
        <Route path='/' exact component={Home} />
        <Route path='/about' component={About} />
        <Route path='/services' component={Services} />
        <Route path='/contact-us' component={Contact} />
      </Switch>
</Router>

Now let’s check the result.

another problem is if you try other URL like ‘/about’ this will show 404 page error.

Let’s solve 404 page error:

Add below two codes in your react app ,

1.Create 404.html under public folder:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Single Page Apps for GitHub Pages</title>
    <script type="text/javascript">
      // Single Page Apps for GitHub Pages
      // MIT License
      // https://github.com/rafgraph/spa-github-pages
      // This script takes the current url and converts the path and query
      // string into just a query string, and then redirects the browser
      // to the new url with only a query string and hash fragment,
      // e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
      // https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
      // Note: this 404.html file must be at least 512 bytes for it to work
      // with Internet Explorer (it is currently > 512 bytes)

      // If you're creating a Project Pages site and NOT using a custom domain,
      // then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
      // This way the code will only replace the route part of the path, and not
      // the real directory in which the app resides, for example:
      // https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
      // https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
      // Otherwise, leave pathSegmentsToKeep as 0.
      var pathSegmentsToKeep = 1;

      var l = window.location;
      l.replace(
        l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
        l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
        l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
        (l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
        l.hash
      );

    </script>
  </head>
  <body>
  </body>
</html>

set pathSegmentsToKeep=0 if your using custom domain for github pages.

Now, add below script in index.html in the head section.

<script type="text/javascript">
      // Single Page Apps for GitHub Pages
      // MIT License
      // https://github.com/rafgraph/spa-github-pages
      // This script checks to see if a redirect is present in the query string,
      // converts it back into the correct url and adds it to the
      // browser's history using window.history.replaceState(...),
      // which won't cause the browser to attempt to load the new url.
      // When the single page app is loaded further down in this file,
      // the correct url will be waiting in the browser's history for
      // the single page app to route accordingly.
      (function(l) {
        if (l.search[1] === '/' ) {
          var decoded = l.search.slice(1).split('&').map(function(s) { 
            return s.replace(/~and~/g, '&')
          }).join('?');
          window.history.replaceState(null, null,
              l.pathname.slice(0, -1) + decoded + l.hash
          );
        }
      }(window.location))
    </script>

Now Everything works fine…

Above code snippets are basically used to explain git where to redirect on particular URL.

Note:Above code snippets belong to https://github.com/rafgraph/spa-github-pages.

Hope this article helped you guys!!!
Drop a comment if it worked for u!!!

Leave a Reply