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

Middleware does not work on the client on the first load - v0.10.5 #457

Closed
lavsurgut opened this issue Mar 26, 2017 · 13 comments
Closed

Middleware does not work on the client on the first load - v0.10.5 #457

lavsurgut opened this issue Mar 26, 2017 · 13 comments
Labels

Comments

@lavsurgut
Copy link

lavsurgut commented Mar 26, 2017

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.

This question is available on Nuxt.js community (#c397)
@Atinux
Copy link
Member

Atinux commented Mar 26, 2017

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.

@Atinux Atinux closed this as completed Mar 26, 2017
@lavsurgut
Copy link
Author

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.

@ausir0726
Copy link

ausir0726 commented Mar 26, 2017

modernizr you can write a plugin . https://nuxtjs.org/examples/plugins/
but , if you want modernizr or else run under ie9 before plugin
( I tried if use ie8 , you will get error before plugin running )

you need put it to nuxt.config.js head.script ( but have escape issue )
maybe you should put it in app.html ( template )
#452
that I want run some code under ie9 or else browser .

@lavsurgut
Copy link
Author

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.

@rhythnic
Copy link

I second being able to run middleware on the client. My use case is authentication via feathers-vuex which is broken on the server, so I need to exchange a reset-token for an access-token on the client, but I'm unable to run the middleware on the client.

@MyMediaMagnet
Copy link

I would also like this feature. It seems like the most logical place for me to place the redirect logic coming from my CMS.

@christophmayrhofer
Copy link

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

@Jones-S
Copy link

Jones-S commented Nov 21, 2017

Same issue here: #2186

@SnirShechter
Copy link

@christophmayrhofer @Jones-S @MyMediaMagnet @rhythnic @lavsurgut

Seems like we're all in the same issue.

@Atinux is there any way around this?
I tried to use a plugin. but then redirect doesn't work(since, i suppose, router can't find the route because it is not yet available for it.

@rhythnic
Copy link

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.

@SnirShechter
Copy link

@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.

@hatashiro
Copy link

Also, plugin workaround doesn't work with async middleware.

@lock
Copy link

lock bot commented Oct 31, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 31, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

10 participants