-
-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(nuxt): process middleware after plugins #4645
Conversation
β Deploy Preview for nuxt3-docs canceled.
|
Beautiful work @danielroe β€οΈ How can we add a test case to confirm the behaviour and avoid any potential regression in the future? |
Happy to merge anyway and test internally π |
I'm wondering if we can keep client/server behavior consistent by either executing middleware on initial render or not. If server-only middleware on initial is desired, it can be easily guarded with |
β¦ access to route
I am not sure about this, I believe middleware shall only be run once. This is the behavior I have done in Nuxt 2 on purpose. What is the advantage of rerunning them on client-side? |
They are. Like anything else during hydration and same as plugins, they run once on the server and once on the client side. In general, I don't think there is any advantage that we are even calling middleware on initial rendering. It is a really strange use case that could be easily replicated with a nuxt plugin already and removed all this complexity for pushing route and ordering... But if we are doing this, it should be consistent IMO. We already introduced this change on Nuxt 3 and breaking it once again doesn't makes sense. |
I think it has no sense you replicate inital middleware server side and client side. Suppose you'll use a middleware global for fetch user on load page, the fetch will be executed on both side.. first request by server side and second one by client side. There is an issue about it too: nuxt/nuxt#13679 Maybe a solution is open a Discussion about it, and lets talk with community too. |
At the moment, this PR simply fixes a couple of bugs; it doesn't change the current behaviour, which runs middleware on client and server. It's not meant to make a decision on the question of whether to run middleware on initial client-side load. Let's have that conversation in nuxt/nuxt#13679; we can always follow up with a further PR reverting (We could also support |
Why does a "global" middleware run only on the server? If people think they only need it on the server just use a process.server conditional. I have a usecase to run it twice but it seems like it is not possible with the current implementation. @pi0 What are your thoughts on this? |
Please see my immediately preceding comment above. This PR did not end up changing the fact that middleware runs on both client and server. See nuxt/nuxt#13679 for an issue requesting this to change and run only once. |
π Linked issue
resolves nuxt/nuxt#13712, see also nuxt/nuxt#13679, resolves nuxt/nuxt#13884
β Type of change
π Description
This PR changes middleware behaviour to defer processing middleware until after plugins are initialised. This means that middleware will have access to plugin injections, and that plugins have the chance to add middleware themselves.
In addition (and this is the breaking change), it skips processing middleware on client-side for server-side-rendered routes, aligning with Nuxt 2.Potential for future improvement -
.client
middleware that does run on client-side but not on serverSSR:
CSR:
π Checklist