Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Vue router / Webpack dev server shows Cannot GET /path on page refresh now?

UPDATE:

It turns out it was a bug in the Vue webpack template on Windows, it will be fixed soon


Question:

I've started a new Vue project by installing it with vue init webpack projectName and now I keep getting: Cannot GET /path (on localhost) when I refresh a page.

For example, if I go to /contact I'll see the page's content, but as soon as I hit refresh I get Cannot GET /contact

More than that, you cannot simply type the route in the address bar manually now, you get the same error.


  • I have mode: 'history'
  • I see this on localhost (npm run dev)

It did work properly a few days ago. How do I make it work now?

All the answers on the internet are not useful (well maybe I didn't applied them properly)


index.js

import Vue from 'vue'
import Router from 'vue-router'
import Vuetify from 'vuetify'
import 'vuetify/dist/vuetify.min.css'
import Home from '@/components/Home'
import Contact from '@/components/Contact'

Vue.use(Router)
Vue.use(Vuetify)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/contact',
      name: 'Contact',
      component: Contact
    }
  ]
})

And because of this I cannot make this redirect work now as well:

{
  path: '*',
  redirect: '/'
}

I get the same error.

So I figured it might be the Vue Webpack's template fault, I've noticed that webpack.dev.conf.js is a bit different now and has lines like this now:

devServer: {
    clientLogLevel: 'warning',
    historyApiFallback: {
      rewrites: [
        { from: /.*/, to: path.join(config.dev.assetsPublicPath, 'index.html') },
      ],
    },
    [...]

Which I would assume has something to do with it.

like image 679
Un1 Avatar asked Jan 06 '18 01:01

Un1


People also ask

Why NPM run Dev is displayed on page cannot get /?

After the webpack dev server is installed and configured, NPM run dev is displayed on the page Cannot get /. The video runs normally. I guess it is because the installed versions are different.

What is callback in vue router?

Below the official Vue router docs: This method queues a callback to be called when the router has completed the initial navigation, which means it has resolved all async enter hooks and async components that are associated with the initial route. This is useful in server-side rendering to ensure consistent output on both the server and the client.

Can it catch the correct path after Webpack Hot-reloads?

It can, however, catch the correct path after Webpack hot-reloads. It catches "/" on first visit of panda, but after I change something in source code, webpack-dev-server hot-reloads, then it gets "/panda".

Why is node node throwing cannot get error in Vue?

Node is throwing the error and make sure your vue router is configured properly,Cannot GET simply means you have not assigned a view to your url e.g on express we use app.use ('/', your route) and on connect we use app.get or app.post All it's saying is there is no content/view assigned to that url.


2 Answers

for those who have stumbled upon this problem via VUE CLI 3, add below code to vue.config.js and then re-run yarn run serve:

module.exports = {

  configureWebpack: {
    devServer: {
      historyApiFallback: true
    }
  }

};
like image 174
iraj jelodari Avatar answered Oct 05 '22 06:10

iraj jelodari


This issue is coming when mode: "history" is being used with the Router. I know this is not the exact solution but yeah, if you are looking for some workaround, probably this might help.

Just remove mode: "history" and your issue will be resolved.

like image 40
hardik chugh Avatar answered Oct 05 '22 08:10

hardik chugh