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
fix(nuxt): abort navigation when updating window.location
#21521
fix(nuxt): abort navigation when updating window.location
#21521
Conversation
3fdcb7a
to
485b88b
Compare
β¦ddleware on client side
ea3be0b
to
f668d32
Compare
if (!nuxtApp.isHydrating) { | ||
return false | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a corner case that's not handled here: when the Nuxt app is loaded on a route that has a redirect middleware. The page gets rendered (and is visible during a very short time) before the location changes. If we also return false
when nuxtApp.isHydrating
, it renders a 404 error / page (that's visible during a very short time).
There's something we could do: delay the return statement, like so, for example:
if (!nuxtApp.isHydrating) {
return false
}
return new Promise(resolve => setTimeout(resolve, 10))
But it looks a bit hacky π€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what happens if you return a never-resolving promise π€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think that's what's happening with return new Promise(resolve => setTimeout(resolve, 10))
as the location changes before the resolve
function gets called.
I like the idea of returning a never-resolving promise (return new Promise(() => {})
) for this case π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danielroe I've pushed the change. Let me know if this is acceptable. I can't see a case when this would lead to an issue. Maybe if, somehow, location.href = toPath
or location.replace(toPath)
fail but can this happen?
β¦om external redirect middleware
window.location
Thank you so much β€οΈ |
π Linked issue
#21314
β Type of change
π Description
On client side, when we use the
navigateTo
function to redirect to an external URL from a middleware (and when the Nuxt application is hydrated => while navigating fromNuxtLink
ornavigateTo
, ...), we abort the navigation so that the page component is not rendered before location changes.Resolves #21314
π Checklist