vue-router: Redirect on 404 page in google cache

Version

3.0.1

Reproduction link

[https://webcache.googleusercontent.com/search?q=cache:6OY0sSJqj5kJ:https://my-site.com/ &cd=1&hl=ru&ct=clnk&gl=ua](https://webcache.googleusercontent.com/search?q=cache:6OY0sSJqj5kJ:https://my-site.com/ &cd=1&hl=ru&ct=clnk&gl=ua)

Steps to reproduce

What is expected?

page will not redirect to the base domain

What is actually happening?

page redirected to the base domain (instead of https://webcache.googleusercontent.com/…)


  1. i’am trying to add something like
var router = new VueRouter({
          base: window.location.pathname
          ...
  1. trying add a base tag
<base href="/">

however it’s doesnt help and the page always redirects to the base domain I found a few issues like that (“doesnt respect the <base>” and so on), but there is no certain solutions or steps to solve the problem.

(sorry, but i don’t have a permission to share my project link before release)

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 19
  • Comments: 22 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve encountered a similar issue with my company, and I think I’ve worked out the cause. Here’s a minimal reproduction: https://github.com/dansebcar/vue-router-2042

TL;DR: looks like setting scrollBehaviour() in the VueRouter constructor can trigger an unexpected pushState when the page has a <base> tag to another origin, thereby redirecting to that origin.

For @wallbanger 's original case, I think adding the <base> tag didn’t help because google injected one above which took priority, and theirs was ignored.

In continuation of issue #3482

@posva I tried to understand what is the problem. I debug the process and noticed the problem. I think the problem is that when we open page in google web cache it has route like this https://webcache.googleusercontent.com/search?q=cache%3A-AlwFLsePq8J%3Ahttps%3A%2F%2Fstrahovka.ru%2Fe-osago%20&cd=1&hl=ru&ct=clnk&gl=ru and like you see it has /search route, that is current, so $route is undefined, because it doesn’t matched that route in route list, because we don’t have that route on our site. You can see it in this part of code. It is match() method in matcher. You can see it in vue-router.js:

...
} else if (location.path) {
  location.params = {};
  for (var i = 0; i < pathList.length; i++) {
    var path = pathList[i];
    var record$1 = pathMap[path];
    if (matchRoute(record$1.regex, location.path, location.params)) {
      return _createRoute(record$1, location, redirectedFrom)
    }
  }
}
// no match
return _createRoute(null, location)

But links in web page, that represents by RouterLink component are exist and they have valid route, so in function isSameRoute(), that is used in render function of RouterLink

classes[exactActiveClass] = isSameRoute(current, compareTarget); // current is undefined but compareTarget not

the process is crushed because it only checks compareTarget (argument b in isSameRoute()):

function isSameRoute (a, b) {
  if (b === START) {
    return a === b
  } else if (!b) {
    return false
  } else if (a.path && b.path) {
    return (
      a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '') &&
      a.hash === b.hash &&
      isObjectEqual(a.query, b.query)
    )
  } else if (a.name && b.name) {
    return (
      a.name === b.name &&
      a.hash === b.hash &&
      isObjectEqual(a.query, b.query) &&
      isObjectEqual(a.params, b.params)
    )
  } else {
    return false
  }
}

So I think that you should also check current argument, because it may be undefined like in my example

This problem still happens on the newest version of nuxt