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

fix(nuxt): ensure middleware is processed when returning true #22905

Merged

Conversation

Mini-ghost
Copy link
Contributor

πŸ”— Linked issue

Resolve #22898

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

When switching routes, all middleware should run to completion unless there is a middleware redirection or an error occurs. However, currently, if we return true in one of the middleware, the loop will terminate without continuing.

As shown below, when return true is encountered, the loop exits at line 183, even though there are other unexecuted middlewares afterward:

if (result || result === false) {
return result
}

Therefore, I added a check in the previous code. If result is true, it allows the loop to continue, executing the next middleware.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@stackblitz
Copy link

stackblitz bot commented Aug 30, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@Mini-ghost
Copy link
Contributor Author

Mini-ghost commented Aug 30, 2023

@danielroe

There are two files in the source code that deal with middleware:

I'm not certain about the differences between these two files. Should I make changes to both of them?

In packages/nuxt/src/app/plugins/router.ts, it appears that the middleware type differs from Vue Router. I'm unsure if this difference is intentional.

Copy link
Member

danielroe commented Aug 30, 2023

@Mini-ghost One of them applies when the vue-router integration is not enabled. (Basically when there is no ~/pages directory.) The other one applies when it is.

This is because we have a generally isomorphic integration for routing that can save on bundle size when full-featured routing isn't needed for a site.

@Mini-ghost
Copy link
Contributor Author

@danielroe

Thank you for your explanation. I have updated the relevant code. I appreciate your help!

@danielroe danielroe changed the title fix(nuxt): ensure middleware behavior is consistent with vue-router fix(nuxt): ensure middleware is processed when returning true Aug 31, 2023
@danielroe danielroe merged commit 2d46971 into nuxt:main Aug 31, 2023
25 checks passed
@github-actions github-actions bot mentioned this pull request Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The behavior of middleware is inconsistent with vue-router's beforeEach
2 participants