Nuxt.js: beforeRouteEnter with redirect to sub-route causes nuxt-child to error

Created on 3 Jun 2019  路  13Comments  路  Source: nuxt/nuxt.js

Version

v2.8.0

Reproduction link

https://codesandbox.io/embed/codesandboxnuxt-z4ovb

Steps to reproduce

Open a page containing a nuxt-child with a beforeEnter route guard redirecting to a sub-page

What is expected ?

When programmatically navigating there (e.g. by clicking a link) there is no error. If opening the page on first view, an error is thrown.

What is actually happening?

Error aborting the view.
nuxtchilderror.PNG

This bug report is available on Nuxt community (#c9303)
bug-confirmed bug-report pending

All 13 comments

This exact error should be fixed in v2.8.1, but there might be another issue still. Eg with the nested-routes example I get a bailing hydration error. Can you please check if that happens for you too with 2.8.1?

Installing latest nuxt now, will let you know today :)

I think this problem is solved, but when I re-route to the parent page, then routeBeforeEnter is not triggered..

I get no more error but I still don't get redirected properly.
When navigating to the parent page while the subpage is open, the beforeRouteEnter hook does not get triggered. Not sure if this is a nuxt or vue-router bug :S

nuxtbug

And I have another bug, where when directly navigating to the parent page, the child route gets loaded but then discards the state. But I save that for another issue.
Another issue gets visible on page load: As you can see, the child page is loaded BEFORE the URL gets updated. Is beforeRouteEnter neglected from serverside? I'd expect that if I have a redirect using beforeRouteEnter, that the serverside already sends me the new page

Thanks in advance :)

Not a bug I think, it probably re-uses the same parent component so you also need to call beforeRouteUpdate

Also I have another problem: If I load /assets (the parent route) directly, I get a client-side hydration error:

image

Even though the <nuxt /> and <nuxt-child /> element contains a div for testing. if I load /assets/devices directly, I don't get this hydration problem :(

I looked into this earlier and I think this is what happens:

On the server:

  • load url /assets
  • redirect to /assets/devices in beforeRouteEnter
  • render /assets/devices page
  • return (ssr) data for /assets/devices page

Then next on the client the following:

  • nuxt app created for url /assets
  • loads /assets page component
  • applies ssrData
  • nuxt app is mounted
  • redirect to /assets/devices in beforeRouteEnter
  • load /assets/device component
  • renders _without_ applying any ssrData

Not sure if this is fixable, you could try to use redirect in asyncData of /assets instead. But if /assets needs to return asyncData as well you are probably out of luck.

I don't need asyncData in /assets _yet_. I think I will solve it like that. 馃憤

However! my setup page decides depending on a fetch method, whether to show a tab or not.

    computed: {
        ...serverconfig.mapState(['serverconfig', 'error'])
    },
    async fetch({ store }) {
        store.dispatch('serverconfig/getServerconfig')
    },
    created() {
        if (this.serverconfig.SystemType == 'onBox') {
            this.pages.splice(2, 0, {
                name: 'Network',
                icon: 'device_hub',
                path: '/setup/network'
            })
        }
    },
    beforeRouteEnter(to, from, next) {
        if (to.name == 'setup') return next('/setup/system')
        next()
    }

works perfectly! Thank you for your help :)
Then I would advise in the docs to only use asyncData redirects. 馃憤

The only problem I have now, is that when clicking on the parent page route again, the asyncData method is not used to redirect - maybe I have to use in combination with the route guard then, I will further try and report.

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.

Just as a reminder, I still have this problem :/
Using in combination with the route guard does not work
Thanks for putting the pending label.
I will put up a sandbox

Hi @MartinMuzatko

I recommend you to use the middleware instead for beforeRouteEnter, so Nuxt can call it when needed and correctly :)

See your demo updated to use the middleware: https://codesandbox.io/s/codesandbox-nuxt-p4knd

This is brilliant! thank you so much @Atinux

Was this page helpful?
0 / 5 - 0 ratings

Related issues

o-alexandrov picture o-alexandrov  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments

maicong picture maicong  路  3Comments

mikekidder picture mikekidder  路  3Comments

jaredreich picture jaredreich  路  3Comments