- JavaScript 39.3%
- HTML 38.9%
- CSS 17.5%
- Dockerfile 4.3%
| __build__ | ||
| components | ||
| styles | ||
| .babelrc | ||
| .eslintrc.json | ||
| .gitignore | ||
| .nojekyll | ||
| 404.html | ||
| CNAME | ||
| Dockerfile | ||
| index.html | ||
| index.js | ||
| LICENSE | ||
| package.json | ||
| README.md | ||
| webpack.config.babel.js | ||
spa-github-pages
Overview
Detected project type: Node.js / JavaScript.
This repository was migrated from upstream source github.com/shafiqalibhai/spa-github-pages and is preserved here for archival, reference, or continued local development.
At a glance
- Default branch:
gh-pages - Visibility: public
- Size: 547 KB
- Created: 2026-04-27
- Last updated: 2026-04-27
- Stars / Forks / Open issues: 0 / 0 / 0
- License: MIT
Languages
| Language | Bytes | Share |
|---|---|---|
| JavaScript | 7,112 bytes | 41.0% |
| HTML | 7,049 bytes | 40.7% |
| CSS | 3,168 bytes | 18.3% |
Repository structure
__build__/components/styles/.babelrc(49 B).eslintrc.json(199 B).gitignore(97 B).nojekyll(0 B)404.html(1,482 B)CNAME(28 B)index.html(5,567 B)index.js(928 B)LICENSE(1,082 B)package.json(829 B)README.md(9,673 B)webpack.config.babel.js(717 B)
Getting started
Clone the repository:
git clone https://forgejo.deployview.com/ssa/spa-github-pages.git
cd spa-github-pages
Installation
npm install
# or
yarn install
# or
pnpm install
Usage
Run scripts defined in package.json:
npm start # or whatever script your project defines
npm test
Original README
The content below is preserved from the previous README. Headings have been demoted so they don't compete with the new top-level sections.
Single Page Apps for GitHub Pages
This is a lightweight solution for deploying single page apps with GitHub Pages. You can easily deploy a React single page app with React Router browserHistory, like the one in the live example, or a single page app built with any frontend library or framework.
Why it's necessary
GitHub Pages doesn't natively support single page apps. When there is a fresh page load for a url like example.tld/foo, where /foo is a frontend route, the GitHub Pages server returns 404 because it knows nothing of /foo.
How it works
When the GitHub Pages server gets a request for a path defined with frontend routes, e.g. example.tld/foo, it returns a custom 404.html page. The custom 404.html page contains a script that 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. For example, example.tld/one/two?a=b&c=d#qwe, becomes example.tld/?redirect=true&pathname=%2Fone%2Ftwo&query=a=b%26c=d#qwe.
The GitHub Pages server receives the new request, e.g. example.tld?redirect=true..., ignores the query string and hash fragment and returns the index.html file, which has a script that checks for a redirect in the query string before the single page app is loaded. If a redirect is present it is converted back into the correct url and added to the browser's history with window.history.replaceState(...), but the browser won't attempt to load the new url. When the single page app is loaded further down in the index.html file, the correct url will be waiting in the browser's history for the single page app to route accordingly. (Note that these redirects are only needed with fresh page loads, and not when navigating within the single page app once it's loaded).
A quick SEO note - while it's never good to have a 404 response, it appears based on Search Engine Land's testing that Google's crawler will treat the JavaScript window.location redirect in the 404.html file the same as a 301 redirect for its indexing.
Usage instructions
For general information on using GitHub Pages please see GitHub Pages Basics, note that pages can be User, Organization or Project Pages
Basic instructions - there are two things you need from this repo for your single page app to run on GitHub Pages
- Copy over the
404.htmlfile to your repo as is - Note that you must use a custom domain if you are setting up a Project Pages site in order for GitHub Pages to serve the custom 404 page (if you are creating a User or Organization Pages site, then using a custom domain is optional) - Copy the redirect script in the
index.htmlfile and add it to yourindex.htmlfile - Note that the redirect script must be placed before your single page app script in yourindex.htmlfile
Detailed instructions - using this repo as a boilerplate for a React single page app hosted with GitHub Pages
- Clone this repo (
$ git clone https://github.com/rafrex/spa-github-pages.git) - Delete the
.gitdirectory (cdinto thespa-github-pagesdirectory and run$ rm -rf .git) - Instantiate the repository
- If you're using this boilerplate as a new repository
$ git initin thespa-github-pagesdirectory, and then$ git add .and$ git commit -m "Add SPA for GitHub Pages boilerplate"to initialize a fresh repository- If this will be a Project Pages site, then change the branch name from
mastertogh-pages($ git branch -m gh-pages), if this will be a User or Organization Pages site, then leave the branch name asmaster - Create an empty repo on GitHub.com (don't add a readme, gitignore or license), and add it as a remote to the local repo (
$ git remote add origin <your-new-github-repo-url>) - Feel free to rename the local
spa-github-pagesdirectory to anything you wnat (e.g.your-project-name) - If you're adding this boilerplate as thegh-pagesbranch of an existing repository - Create and checkout a new orphaned branch named
gh-pagesfor your existing repo ($ git checkout --orphan gh-pages), note that thegh-pagesbranch won't appear in the list of branches until you make your first commit - Delete all of the files and directories (except the
.gitdirectory) from the directory of your existing repo ($ git rm -rf .) - Copy all of the files and directories (including hidden dot files) from the cloned
spa-github-pagesdirectory into your project's now empty directory ($ mv path/to/spa-github-pages/{.[!.],}* path/to/your-projects-directory) $ git add .and$ git commit -m "Add SPA for GitHub Pages boilerplate"to instantiate thegh-pagesbranch
- Set up your custom domain - see GitHub Pages instructions for setting up a custom domain
- Note that you must use a custom domain if you are setting up a Project Pages site in order for GitHub Pages to serve the custom 404 page, however, if you are creating a User or Organization Pages site, then using a custom domain is optional (if you don't use a custom domain delete the
CNAMEfile) - Update theCNAMEfile with your custom domain, don't includehttp://, but do include a subdomain if desired, e.g.wwworyour-subdomain- Update yourCNAMEand/orArecord with your DNS provider - Run$ dig your-subdomain.your-domain.tldto make sure it's set up properly with your DNS (don't includehttp://) - Run
$ npm installto install React and other dependencies, and then run$ webpackto update the build $ git add .and$ git commit -m "Update boilerplate for use with my domain"and then push to GitHub ($ git push origin gh-pagesfor Project Pages or$ git push origin masterfor User or Organization Pages) - the example site should now be live on your domain- Creating your own site
- Write your own React components, create your own routes, and add your own style!
- Change the title in
index.htmland the title in404.htmlto your site's title - Remove the favicon links from the header ofindex.html- Remove the Google analytics script from the header ofindex.html(the analytics function is wrapped in anifstatement so that it will only run on the example site's domain (http://spa-github-pages.rafrex.com), but you don't need it, so remove it or replace it with your own analytics) - Change the readme, license and package.json as you see fit - After you update your code run$ webpack(or$ webpack -pfor production, or-dfor development) to update the build, then$ git commitand$ git pushto make your changes live- Note that
$ webpack -pis overloaded in the webpack config to strip out dead code not needed in production (e.g. PropTypes validation, comments, etc)
- Note that
Miscellaneous
- The
.nojekyllfile in this repo turns off Jekyll for GitHub Pages - Need form submission on your static site? Use Formspree
- One of the awesome things about the GitHub Pages CDN is that all files are automatically compressed with gzip, so no need to worry about compressing your JavaScript, HTML or CSS files for production
Pull requests welcome. Please open issues to report bugs.
Thoughts, questions, suggestions? Contact me via email or twitter.
Contributing
Contributions are welcome. The typical workflow is:
- Open an issue describing the change you'd like to make.
- Fork the repository (or create a feature branch if you have write access).
- Commit your changes with clear, descriptive messages.
- Open a pull request against the
gh-pagesbranch.
Please follow the existing code style and include tests or reproduction steps where relevant.
License
This project is licensed under the MIT license. See the LICENSE file for the full text.
Repository
- Browse: https://forgejo.deployview.com/ssa/spa-github-pages
- Clone (HTTPS):
https://forgejo.deployview.com/ssa/spa-github-pages.git - Clone (SSH):
ssh://git@forgejo.deployview.com:30143/ssa/spa-github-pages.git - Upstream / origin: github.com/shafiqalibhai/spa-github-pages
This README was generated automatically based on repository metadata, contents, and any prior README content. Edit any section above to add project-specific detail.