guizo
guizo

Reputation: 3095

Deploy Vue to GitHub Pages. Error with vue-router

I was experiencing some trouble while deploying a Vue application build with vue-cli v3.0. to GitHub Pages. I'm using subtree to send the dist folder only to gh-pages branch. First the problem was that the assets where not found but I fixed it using baseUrl on vue.config.js. Now the problem is that the #app element is empty. I found out that if I don't use vue-router (render the view direct instead of using <router-view/>) the app works fine with GitHub pages. I believe there is some issue with the route path option but I'm unable to figure out how to fix it.

The repository with the issue is https://github.com/guizoxxv/vue-cli-deploy and the GitHub Page link is https://guizoxxv.github.io/vue-cli-deploy/

Thank you.

Upvotes: 3

Views: 9582

Answers (5)

Anand Raja
Anand Raja

Reputation: 3106

  1. For Vue3, Please make the configuration as below:
import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
  history: createWebHistory('/your-github-repo/'),
  routes: [
   // put your routes
  ]
});
  1. For Vue2, Please make the configuration as below:
import VueRouter from 'vue-router';

const router = new VueRouter({
  mode: 'history',
  base: '/your-github-repo/',
  routes: [
    // put your routes
  ]
});

If still problem persists, please create vue.config.js file in root location of your vue app and add the following code:

module.exports = {
  publicPath: process.env.NODE_ENV === 'production'
  ? '/your-github-repo/' // note the trailing slash
  : '/',
}

Upvotes: 4

yaeru
yaeru

Reputation: 1

As they said baseUrl is deprecated, use publicPath

module.exports = { publicPath: '/your-repo/' }

Upvotes: 0

Roland
Roland

Reputation: 27769

Update 2020

baseUrl is Deprecated since Vue CLI 3.3, please use publicPath instead.

If you are using Vue CLI >= 3.3 you might want to check out this vue-cli plugin which automates github pages deployment using github actions.

By default, Vue CLI assumes your app will be deployed at the root of a domain, e.g. https://www.my-app.com/. If your app is deployed at a sub-path, you will need to specify that sub-path using this option. For example, if your app is deployed at https://www.foobar.com/my-app/, set baseUrl to '/my-app/'. So :

  1. Create a new file in root directory of your project and name it ‘vue.config.js’
  2. In ‘vue.config.js’ file paste the following code:

    module.exports = { baseUrl: ‘/my-first-project/’ }

NOTE: in baseUrl inside the // chars you have to put the name of your project.

Read more here

Also, read a full article how to deploy vue.js project in github pages here

Upvotes: 5

guizo
guizo

Reputation: 3095

I believe I discovered the cause for this issue:

As GitHub Pages URL is not served from the root

https://guizoxxv.github.io/vue-cli-deploy/ has vue-cli-deploy/ after the root /

I need to specify a base option for my app on vue-router options. Here is the documentation https://router.vuejs.org/api/#base

Upvotes: 3

Georgi Antonov
Georgi Antonov

Reputation: 1641

can you try this in your main.js

new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>',
});

Upvotes: 0

Related Questions