Vue-router: Production build: when route accessed manually through URL it doesn't work!!

Created on 15 Mar 2017  ·  2Comments  ·  Source: vuejs/vue-router

Vue.js / vue-router versions

2.1.10 / 2.3.0

Reproduction Link



Unfortunately the code is long and private, but here is what I use, when a login is fired it gives a route change in the $router using vm.$router.push('/') it checks as in the router file for authentication and continues to the next route. but when I try to enter a link manually it doesnt work in production build. but it works in development.
N.B.: I use npm run build and I changed nothing in the webpack.base.conf.js file.

Steps to reproduce

router/index.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Auth from '../config/auth'
// Components
import Home from '../components/Home'
import Login from '../components/Login'

Vue.use(VueRouter)

export default new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: Home,
      beforeEnter (to, from, next) {
        if (!Auth.checkAuth()) {
          next('/login')
        } else {
          next()
        }
      }
    },
    {
      path: '/login',
      component: Login,
      beforeEnter (to, from, next) {
        if (Auth.checkAuth()) {
          next('/')
        } else {
          next()
        }
      }
    }
  ]
})

Now, when trying to login or logout everything works fine but when trying to open the link to login directly from the navigator it gives a 404 error. using vm.$router.go push or replace it gives the same result. and this only happens in production build everything works fine in development.

What is Expected?

It's expected to return the same result as in development.

What is actually happening?

the URL is giving an 404 error but when redirected to it automatically from the router it works normally. thus creating a bug.

All 2 comments

Thank you for your interest in this project.

However, your issue is a usage/support question, and the issue tracker is reserved exclusively for bug reports and feature requests (as outlined in our Contributing Guide).

We encourage you to ask it on the forum , Stack Overflow or on gitter and are happy to help you out there. Come back open a new issue if you find out there's a real issue

If using Apache, add this to .htaccess


RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

Was this page helpful?
0 / 5 - 0 ratings