rafrex / spa-github-pages
- пятница, 8 июля 2016 г. в 03:13:59
JavaScript
host single page apps with 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.
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
.
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.
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
404.html
file to your repo as is
index.html
file and add it to your index.html
file
index.html
file
Detailed instructions - using this repo as a boilerplate for a React single page app hosted with GitHub Pages
$ git clone https://github.com/rafrex/spa-github-pages.git
).git
directory (cd
into the spa-github-pages
directory and run $ rm -rf .git
)$ git init
in the spa-github-pages
directory, and then $ git add .
and $ git commit -m "Add SPA for GitHub Pages boilerplate"
to initialize a fresh repositorymaster
to gh-pages
($ git branch -m gh-pages
), if this will be a User or Organization Pages site, then leave the branch name as master
$ git remote add origin <your-new-github-repo-url>
)spa-github-pages
directory to anything you wnat (e.g. your-project-name
)gh-pages
branch of an existing repository
gh-pages
for your existing repo ($ git checkout --orphan gh-pages
), note that the gh-pages
branch won't appear in the list of branches until you make your first commit.git
directory) from the directory of your existing repo ($ git rm -rf .
)spa-github-pages
directory 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 the gh-pages
branchCNAME
file)CNAME
file with your custom domain, don't include http://
, but do include a subdomain if desired, e.g. www
or your-subdomain
CNAME
and/or A
record with your DNS provider$ dig your-subdomain.your-domain.tld
to make sure it's set up properly with your DNS (don't include http://
)$ npm install
to install React and other dependencies, and then run $ webpack
to 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-pages
for Project Pages or $ git push origin master
for User or Organization Pages) - the example site should now be live on your domainindex.html
and the title in 404.html
to your site's titleindex.html
index.html
(the analytics function is wrapped in an if
statement 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)$ webpack
(or $ webpack -p
for production, or -d
for development) to update the build, then $ git commit
and $ git push
to make your changes live
$ webpack -p
is overloaded in the webpack config to strip out dead code not needed in production (e.g. PropTypes validation, comments, etc).nojekyll
file in this repo turns off Jekyll for GitHub PagesPull requests welcome. Please open issues to report bugs.
Thoughts, questions, suggestions? Contact me via email or twitter.