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
Middleware does not work on the client on the first load - v0.10.5 #457
Comments
This is normal because the first load is on the server side, so the middleware is called from the server-side. We don't run again the middleware on the client, we simply hydrate the DOM. |
And why don't you run middleware on the client? You could have some code you want to run on the client. Like modernizr, for example. With modernizr it is not clear where to put it. |
modernizr you can write a plugin . https://nuxtjs.org/examples/plugins/ you need put it to |
What I thought of is that Middleware has an access to context and therefore it is convenient to use it and do for example redirect to a certain route. Anyways, for now, I implemented modernizr check on the server side and indeed I use middleware for that. Thanks. |
I second being able to run middleware on the client. My use case is authentication via |
I would also like this feature. It seems like the most logical place for me to place the redirect logic coming from my CMS. |
same issue. I want to use the middleware to fetch data and redirect if I am unauthorized. Right now I have to do it in the components created() method |
Same issue here: #2186 |
@christophmayrhofer @Jones-S @MyMediaMagnet @rhythnic @lavsurgut Seems like we're all in the same issue. @Atinux is there any way around this? |
I used $router.redirect inside of a try catch inside of the created callback. $router.redirect throws an error on the server but it succeeds on the client. Then redirect to another route where you actually do what you need to do. It sucks having to create another route and it's fragile in that it depends on the server throwing an error on $router.redirect, but it's the only workaround I could figure out. |
@Atinux @rhythnic @Jones-S @christophmayrhofer @MyMediaMagnet Looks like the best way is making an init.js plugin (ssr:false) and doing the initialization logic there. However, redirect() does not work(router.onReady(redirect) will change page but not layout). seems like this is fixed in nuxt-edge. |
Also, plugin workaround doesn't work with async middleware. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
For example,
export default function (context) {
console.log(context.isClient)
}
All subsequent route changes are executed, but not the first load. This bug invalidates the use of middleware on the client side.
The text was updated successfully, but these errors were encountered: