Nuxt.js: Allow middleware to run client-side only in SSR mode

Created on 6 Jun 2020  路  4Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

Middleware frequently need to check state such as whether a user is logged in.
If you want to redirect users to a login page based on their logged in status on every page, you cannot do this via a middleware in SSR mode.

Changing the whole app to SPA mode for this one feature will slow the entire app down.

What does the proposed changes look like?

A flag in nuxt.config.json to run middlewares in client side only.

This feature request is available on Nuxt community (#c10739)
feature-request

Most helpful comment

I would suggest the feature of using middleware.client.js and middleware.server.js vs this flag so it matches the same feature of plugins. This would also eliminate a bunch of if(process.client) stuff as well. I may be working on a pull on this at some point but currently don't have time.

All 4 comments

I would suggest the feature of using middleware.client.js and middleware.server.js vs this flag so it matches the same feature of plugins. This would also eliminate a bunch of if(process.client) stuff as well. I may be working on a pull on this at some point but currently don't have time.

We are using nuxt in full static with mode 'universal' to have fully pre-rendered pages and we assumed the middleware will only be executed client-side, well, because it is static. But apparently this is not the case. I do not understand why we cannot have client-side only middleware (with full static). In Static mode server-side rendering should mean _rendering_ not execution of middleware -- this is highly confusing.
The weirdest was that some middleware are executed on client-side on the first page load and others are not. Switching to SPA mode fixes it, but this slows down a static page unnecessarily -- and defeats the purpose of a static build.

@theDevelopper what makes you think middleware is being ran server side with full static?

when building the static pages I see the output from the middlewares during the build process in the terminal. The middleware is clearly being executed during build.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gary149 picture gary149  路  3Comments

jaredreich picture jaredreich  路  3Comments

vadimsg picture vadimsg  路  3Comments

vadimsg picture vadimsg  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments