Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 when reloading a Vue website published to Github pages

I have deployed the contents of my /dist folder in the master branch of christopherkade.github.io, which has deployed my website succesfully.

But when I navigate using the navbar (christopherkade.com/posts or christopherkade.com/work) and reload the page I get an error by Github pages:

404 File not found

Note that my routing is done using Vue router like so:

export default new Router({
  mode: 'history',
  routes: [    
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/work',
      name: 'Work',
      component: Work
    },
    {
      path: '/posts',
      name: 'Posts',
      component: Posts
    },
    { path: '*', component: Home }
  ]
})

And my project is built like such:

build: {
    // Template for index.html
    index: path.resolve(__dirname, '../docs/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../docs'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }

What could be causing this issue?

like image 692
Christopher Avatar asked Jan 30 '18 12:01

Christopher


People also ask

How do I fix Error 404 on GitHub?

If you saw 404 even everything looks right, try switching https/http. The original question has the url wrong, usually you can check repo settings and found the correct url for generated site.

Why is my website not working on GitHub?

To get your site working again you'll need to make sure your repository is set to public again, head to the repository settings page, make sure that GitHub Pages is enabled, then push a fresh commit to your repository.

Can you use Vue on GitHub Pages?

After you save the repository, it automatically creates a GitHub Page for you using the HTML at the root of the project. You can also set up GitHub Pages for any new or existing repositories you have on GitHub. In this post, we will create a Vue. js project and then deploy it to GitHub Pages.

How do I upload Vue project to GitHub?

The above commands merge the Vue project created in the gh-pages branch. Go to your repository's configuration page on GitHub and in Source change it to /docs . Click Save. Now, let's update our repository with GitHub and push.


1 Answers

But when I navigate using the navbar (christopherkade.com/posts or christopherkade.com/work) and reload the page 404 File not found

Let me explain why 404 File not found is being shown

When christopherkade.com/posts is triggered from web browser, the machine to which the domain christopherkade.com is mapped is contacted.

The path /posts is searched in its server. in your case, i believe the route for /posts doesn't exist in the server. As the result 404 is displayed

There are few ways to fix this

To prevent the browser from contacting the server when triggering the request christopherkade.com/posts, you can keep mode : 'hash' in your route configuration

How mode : 'hash' works? This is one way to fix your issue

mode : 'hash' makes use of default browser behavior which is to prevent http request from triggering the details that exists after #

As the result, when you trigger christopherkade.com/#/posts , christopherkade.com is being triggered by the browser and once response is received the /posts route from the route config is invoked.

Lets assume that you have control over the server and you are adamant that you need # to be removed from the URL

Then what you could do is to configure server in such a way that server responds with the same page everytime any paths is being sent. Once response is received in the browser, route will automatically kicked off.

Even in your current program, the routeConfig gets kicked off when you click any links (like work,posts) in your page. This is because the browser behavior is not being invoked at this point.

enter image description here

In your case, you use github for hosting this app with mode: 'history' i myself have to look for a specific solution to workaround this. i will update my answer once i get it.

i hope this was useful.

like image 125
divine Avatar answered Oct 14 '22 08:10

divine