How to deploy on GitHub Pages? – NuxtJS

Nuxt.js gives you the possibility to host your web application on any static hosting like GitHub Pages for example.

To deploy on GitHub Pages, you need to generate your static web application:

npm run generate

It will create a dist folder with everything inside ready to be deployed on GitHub Pages hosting. Branch gh-pages for project repository OR branch master for user or organization site

Deploying to GitHub Pages for repository

If you are creating GitHub Pages for one specific repository, and you don’t have any custom domain, the URL of the page will be in this format: http://<username><repository-name>.

If you deployed dist folder without adding router base, when you visit the deployed site you will find that the site is not working due to missing assets. This is because we assume that the website root will be /, but in this case it is /<repository-name>.

To fix the issue we need to add router base configuration in nuxt.config.js:

export default { router: { base: ‘/<repository-name>/’ } }

This way, all generated path asset will be prefixed with /<repository-name>/, and the next time you deploy the code to repository GitHub Pages, the site should be working properly.

There is a downside adding router.base as the default setting in nuxt.config.js though, when you are running npm run dev, it won’t be working properly since the base path changes. To fix this issue, we want to create a conditional for router.base whether to include <repository-name>:

/* nuxt.config.js */ // only add `router.base = ‘/<repository-name>/’` if `DEPLOY_ENV` is `GH_PAGES` const routerBase = process.env.DEPLOY_ENV === ‘GH_PAGES’ ? { router: { base: ‘/<repository-name>/’ } } : {} export default { …routerBase }

READ: Hosting Reviews September 2022 - Features, Pricing

and now we just need to set DEPLOY_ENV=’GH_PAGES’ to build the site for GitHub Pages:

For Nuxt >= v2.13

/* package.json */ “scripts”: { “build:gh-pages”: “DEPLOY_ENV=GH_PAGES nuxt build”, “generate:gh-pages”: “DEPLOY_ENV=GH_PAGES nuxt build && nuxt export” },

For Nuxt <= v2.12

/* package.json */ “scripts”: { “build:gh-pages”: “DEPLOY_ENV=GH_PAGES nuxt build”, “generate:gh-pages”: “DEPLOY_ENV=GH_PAGES nuxt generate” },

Command line deployment

You can also use push-dir package:

First install it via npm:

npm install push-dir -save-dev

Add a deploy command to your package.json with the branch as gh-pages for project repository OR master for user or organization site.

For Nuxt >= v2.13

“scripts”: { “dev”: “nuxt”, “build”: “nuxt build”, “start”: “nuxt start”, “generate”: “nuxt export && nuxt build”, “deploy”: “push-dir -dir=dist -branch=gh-pages -cleanup” },

For Nuxt <= v2.12

“scripts”: { “dev”: “nuxt”, “build”: “nuxt build”, “start”: “nuxt start”, “generate”: “nuxt generate”, “deploy”: “push-dir -dir=dist -branch=gh-pages -cleanup” },

Then generate and deploy your static application:

npm run generate npm run deploy

Build server deployment

You can take deployment one step further and rather than having to manually compile and deploy the files from your local install, you can make use of a build server to monitor your GitHub repository for new commits and then checkout, compile and deploy everything for you automatically.

Before you configure the build server, you’ll first need to generate a GitHub personal access token in order to grant the build server permission to perform tasks on your behalf. Once you have created your token, keep a copy of it safe ready to use a little later on.

READ:  What is the Difference Between Email Hosting and Web Hosting?

Travis CI

To deploy with Travis CI, a free for open source projects build server, sign in via your GitHub account, granting Travis access to view your repositories, and then enable the build server for your repository by toggling the switch next to your repositories name in the list displayed.

Travis Builder Server Enable

Next, click the cog icon beside your repository name to configure the general settings of the build sever and enable the ‘Build only if .travis.yml is present’ feature by toggling the switch.

Travis Builder Server Settings

On the same screen, scroll down to the Environment Variables section and create a new variables named GITHUB_ACCESS_TOKEN and in the value field paste a copy of the GitHub personal access token your created earlier and click the ‘Add’ button.

Travis Builder Server Environment Variables

Finally, create a .travis.yml configuration file in the root of your repository with the following contents

language: node_js node_js: – “8” cache: directories: – “node_modules” branches: only: – master install: – npm install – npm run generate script: – echo “Skipping tests” deploy: provider: pages skip-cleanup: true github-token: $GITHUB_ACCESS_TOKEN # Set in dashboard, marked secure target-branch: gh-pages local-dir: dist on: branch: master

and then commit this to your repository

git add .travis.yml git commit -m “Adding travis deploy configuration” git push origin

Now, whenever you commit any changes to your repository, from within Travis, you’ll see a new build start up

Travis Builder Server Output

and on completion, you’ll see your GitHub pages site automatically updated.


To deploy via Appveyor, another free for open source projects build server, sign up for a new account choosing the GitHub authentication option to sign in using your GitHub account.

READ:  How to migrate web hosts without losing SEO

Once signed in, click the ‘New project’ link and then click the ‘Add’ button beside your repository name in the list displayed to enable the build server on your repository.

Appveyor Builder Server Enable

Next, in the root of your repository, create an appveyor.yml configuration file with the following contents

environment: # Nuxt requires node v8 minimum nodejs_version: “8” # Encrypt sensitive data ( github_access_token: secure: ENCRYPTED_GITHUB_ACCESS_TOKEN github_email: secure: ENCRYPTED_GITHUB_EMAIL # Only run on master branch branches: only: – master # Install scripts. (runs after repo cloning) install: # switch nodejs version – ps: Install-Product node $env:nodejs_version # install modules – npm install # generate static files – npm run generate # configure global git credentials store ( – git config -global credential.helper store – ps: Add-Content “$env:USERPROFILE.git-credentials” “https://$($env:github_access_token)`n” – git config -global $env:github_email # deploy to GitHub pages – npm run deploy # No tests to run test: off # Don’t actually build. build: off

NB This configuration assumes you’ve configured your package.json file as per the Command line deployment instructions

Before you commit this file however, you’ll need to change the ENCRYPTED_GITHUB_ACCESS_TOKEN and ENCRYPTED_GITHUB_EMAIL variables with your GitHub personal access token from earlier and your GitHub email address, encrypted using the Appveyor encryption tool.

Once updated, commit the file to your repository

git add appveyor.yml git commit -m “Adding appveyor deploy configuration” git push origin

Now, whenever you commit any changes to your repository, from within Appveyor, you’ll see a new build start up

Appveyor Builder Server Output

and on completion, you’ll see your GitHub pages site automatically updated.