It turns out it was a bug in the Vue webpack template on Windows, it will be fixed soon
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.
mode: 'history'
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)
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.
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.
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.
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".
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.
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
}
}
};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With