I ran into this issue when using create-react-app and react-snapshot together, but it's possible to use this solution for any setup which creates this problem.

The Problem

After building static site assets, you may have rendered HTML files which link to your difference pages. When using Github to host a static site, it is important to structure these files in their own directory with an index. Using this organization, Github will respond with the correct page.

Stucture before


After structure fix


Only with this structure can you use loose routes such as:

  • https://handle.github.io/site/pages/a
  • https://handle.github.io/site/pages/b/
  • https://handle.github.io/site

In order to maintain this structure, I wrote a small script to fix the original build output from react-snapshot output.

const fs = require('fs');
const path = require('path');

const dir = 'build/post';
const posts = fs.readdirSync(dir).filter(name => name.endsWith('html'));

for (const post of posts) {
  const base = path.basename(post, '.html');
  try {
  } catch (e) {
  fs.renameSync(`${dir}/${post}`, `${dir}/${base}/index.html`);

I hope someone else finds this useful when building static sites hosted on Github!

Back to all posts