Nuxt.js: Middleware promise to resolve prior to rendering page

Created on 2 May 2019  路  11Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

When using a promise within a middleware, we cannot wait for it to be resolved prior rendering the page. It can be an issue when using an auth API based on promises.

What does the proposed changes look like?

We should be able to use following code.

export default function({ route, redirect }) {
  my_API.getAuthenticatedUser() // Promise
    .catch(() => {
      return null
    })
    .then(user => {
      // Anonymous users
      if (!user) redirect('/login')

      // Authenticated users
      if (user) do_something_else()
    })

  // This does not seem to change anything at the moment
  return Promise.resolve
}

We should be able to choose whether the middleware promise needs to resolve or not.

With vue-router, we can choose whether the promise needs to be resolved or not depending on where we use next() (in then section or after the promise).

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

Most helpful comment

@borisdayma the page bundle is imported anyway, which makes the console.log being called.

The middleware is protecting the creating of the page (ie: new Vue(page)), so you want to add these console.log in the page hooks (ie: created, mounted, asyncData...).

All 11 comments

Problem solved by doing return my_API.getAuthenticatedUser()...
Thanks to @rafal on Discord.

Note: page shows a logging screen but parts of it are still executing. For example I still get a console output from protected page (which shows a loading screen) if I include the following code in it:

<script>
console.log('THIS SHOULD NOT APPEAR')
[...]
</script>

I expected that no code at all from this page would be executed.

I guess we can close this issue @borisdayma

I agree, can you just confirm that executing the

I am also encountering problems with this.
This should be re-opened and an option to have the middleware as blocking should be added.

I'm being hit by this issue as well! Would like for the middleware promise to resolve prior to rendering page! Or am I missing something?

@Atinux @pi0 can something do that?

Well I put in a work around where I'm watching the store property (which gets updated form the middleware) and then running the rest of the code, but I'm not a fan of this approach.

I managed to make the middleware return the promise by using the suggested change directly in the middleware. Now a page doesn鈥檛 load until the promise is resolved.
The approach is ok but I still think it would have been nice to have a way to do that out of the box.

@KGBemployee do you mind sharing some code and elaborate more? Thank you so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikekidder picture mikekidder  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments

mattdharmon picture mattdharmon  路  3Comments

VincentLoy picture VincentLoy  路  3Comments

msudgh picture msudgh  路  3Comments