Nuxt.js: watchQuery fires twice on $router.push

Created on 19 Feb 2020  路  3Comments  路  Source: nuxt/nuxt.js

Version

v2.11.0

Reproduction link

https://codesandbox.io/s/sparkling-platform-di095

Steps to reproduce

Create a method that calls $router.push with a new query parameter value:

<template>
  <div>
    <nuxt-link to="#" @click.native.prevent="incX">increment x</nuxt-link>
    <pre>{{x}}</pre>
  </div>
</template>

<script>
export default {
  data() {
    return {
      x: 0
    };
  },

  methods: {
    async incX() {
      console.log("incX:", this.x);
      this.x += 1;

      this.$router.push({ query: { x: this.x } });
    }
  },

  watch: {
    "$route.query": {
      handler(query) {
        console.log("$route.query:", query);
      }
    }
  },

  watchQuery(newQuery, oldQuery) {
    console.log("watchQuery", newQuery);
    return false;
  }
};
</script>

What is expected ?

watchQuery should run once every time the link is clicked.

What is actually happening?

Clicking the link to increment the value results in watchQuery getting called twice even though the $route.query watcher is only called once, e.g.:

current x: 4
new x: 5
watchQuery Object {x: 5}
watchQuery Object {x: 5}
$route.query: Object {x: 5}
bug-report stale

Most helpful comment

Also having this issue.

Edit : Solved the issue by disabling watchQuery provided by Nuxt and creating custom query watcher.

watch: {
    $route(to, from) {
      // react to route changes...
    }
  }

All 3 comments

Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
  2. Comment the steps to reproduce it

Issues that are labeled as pending will not be automatically marked as stale.

+1

Also having this issue.

Edit : Solved the issue by disabling watchQuery provided by Nuxt and creating custom query watcher.

watch: {
    $route(to, from) {
      // react to route changes...
    }
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shyamchandranmec picture shyamchandranmec  路  3Comments

VincentLoy picture VincentLoy  路  3Comments

uptownhr picture uptownhr  路  3Comments

bimohxh picture bimohxh  路  3Comments

vadimsg picture vadimsg  路  3Comments