Repository nuxt.js

After using router.push\replace asyncData not called on new page

aldarund
258
aldarund
commented 7 months ago

Version

v1.4.0

Reproduction link

https://glitch.com/edit/#!/poised-hardware

Steps to reproduce

https://poised-hardware.glitch.me/ - if you go index page u will see render error due to undefined variable. But index page only do router.push to /page2.
If u go directly to https://poised-hardware.glitch.me/page2 all renders fine.

What is expected ?

asyncData called and variable resolved so no error happened like when page accessed directly without router.push

What is actually happening?

render error due to undefined variable that should come from asyncData

bug
0

Cancelled

7 months ago by Atinux

yoyo837
63
yoyo837
commented 7 months ago

asyncData is server only.

0
aldarund
258
aldarund
commented 7 months ago

@yoyo since when it's server only ? It's not server only

0
yoyo837
63
yoyo837
commented 7 months ago

@aldarund You are right.I will correct myself.

0
Atinux
20.9k
Atinux
commented 7 months ago

Hi @aldarund

You cannot use $router.push or $router.replace inside fetch or asyncData. To understand more, these methods are called inside router.beforeEach. You need to call redirect.

Please see your example fixed: https://glitch.com/edit/#!/lying-ring?path=pages/index.vue:2:1

0
aldarund
258
aldarund
commented 7 months ago

@Atinux thanks, that worked. Is it documented somewhere?

0
Atinux
20.9k
Atinux
commented 7 months ago

It is not, it is pretty hard to document everything, you can find more about the context here: https://nuxtjs.org/api/context

0
aldarund
258
aldarund
commented 7 months ago

@Atinux redirect works. but it seems to cause lose of store state. E.g. on first page i set some store variables, then redirect to second page. And on second page all set variables are lost.
https://glitch.com/edit/#!/furry-shop

0
Informations
Bug ReportCancelled
#c6869 - Created 7 months ago