Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncData called twice in developer mode with nuxt-i18n enabled #9209

Closed
rchl opened this issue Apr 28, 2021 · 4 comments
Closed

asyncData called twice in developer mode with nuxt-i18n enabled #9209

rchl opened this issue Apr 28, 2021 · 4 comments

Comments

@rchl
Copy link

rchl commented Apr 28, 2021

During development (nuxt dev), if using nuxt-i18n (more specifically vue-i18n) and navigating to a route in another locale, the asyncData of the page component is called twice.

(And also the route object within the asyncData is not set up correctly.)

Versions

  • nuxt: v2.15.4

Reproduction

https://codesandbox.io/s/nuxtjs-bug-dev-vue-i18n-asyncdata-5fwl7

Steps to reproduce

  1. Set up nuxt-i18n with two locales
  2. Create a page component that defines an asyncData hook
  3. Create nuxt-link that navigates to a route in another locale.
  4. Click the link to navigate to a route in another locale

What is Expected?

  • The asyncData is triggered once
  • Checking the context.route.path from asyncData reports the new (target) route path.

What is actually happening?

  • The asyncData is triggered twice
  • Checking the context.route.path from asyncData reports a wrong (old) route both times.

Additional details

See also nuxt-modules/i18n#1160

Here is what is happening:

vue-i18n calls VueComponent.$forceUpdate() on locale change: https://github.com/kazupon/vue-i18n/blob/13f3e618d5a7c3c32fe99f156d1ee2b4e199a5a4/src/index.js#L250-L259

In dev mode, Nuxt implements/overrides $forceUpdate to trigger, amongst others, an asyncData call: https://github.com/nuxt/nuxt.js/blob/8e2cad5a8fd7c4fa731fea92d762b650ee5ce095/packages/vue-app/template/client.js#L740-L746

This is only a problem during development since it's related to "hot reload" which is not enabled otherwise.

That custom $forceUpdate function also does set the route on the context which doesn't seem right in this case as there is a pending transition to another route at that time already.

Maybe the $forceUpdate logic should be skipped entirely if there is a pending route transition? Can't say for sure as I don't know that code but that's the first thing that came to my mind.

@xfyre
Copy link

xfyre commented May 6, 2021

I've recently discovered this, too. This is indeed the case for development mode only. What's even more awkward - asyncData on the page is being called when navigating away from that page.

@sebbayer
Copy link

I have a single page app that loads all page contents in asyncData. My vue-i18n locale is updated inside asyncData of the page. That creates an endless http request loop in dev mode until the locale is finally set inside the watcher by $set(): locale changes -> patched $forceUpdate called -> asyncData called again -> locale changes ... and so on.

@stale
Copy link

stale bot commented Jul 13, 2021

Thanks for your contribution to Nuxt!
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.

@stale stale bot added the stale label Jul 13, 2021
@rchl
Copy link
Author

rchl commented Jul 13, 2021

I believe this is now fixed. Probably by kazupon/vue-i18n#1278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants