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(vue-app): skip page render early on error or navigation #20719

Merged
merged 5 commits into from May 22, 2023

Conversation

rchl
Copy link

@rchl rchl commented May 8, 2023

πŸ”— Linked issue

#20691

❓ 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

This change attempts to interrupt page "rendering" early if the route is navigated away from while middleware or asyncData is in progress. Additionally, also interrupts as early as possible when error() is called so that the error page is not shown after user has navigated to another page already.

Fixed by creating a plain renderState object that has an aborted property set to false initially, and passing that object to the internal render function and then also down to function that executes middlewares. If a new render is triggered (through the router.beforeEach hook) while the original one is in progress, the aborted property is set to true so that the original render pass can abort as soon as possible (once currently executing async function is done).

I realize that this is not 100% proof solution as the user could do many weird things that would affect the state of the app from the middlewares or the asyncData handlers, but I think this should cover most common cases.

Added tests that verify that the error page is not shown after navigating away from the page when the page triggers error() call or throws an exception. Unfortunately those have to rely on arbitrary 2s delay but I haven't found a better way to test that.

πŸ“ Checklist

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

@codesandbox
Copy link

codesandbox bot commented May 8, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@@ -995,5 +995,6 @@ async function mountApp (__app) {
errorHandler(err)
}
})
})
},
{ aborted: false })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not put too much thought into that part of the code (the SPA case). I could look more if needed but it could be just left like that, without supporting early abort.

@danielroe danielroe requested a review from manniL May 9, 2023 22:49
@rchl
Copy link
Author

rchl commented May 22, 2023

@manniL I see you've been assigned. Could you have a look at this?

@manniL
Copy link
Member

manniL commented May 22, 2023

Looks like a good solution to me for covering basic use cases πŸ‘

@danielroe danielroe merged commit db0e155 into nuxt:2.x May 22, 2023
13 checks passed
@danielroe danielroe changed the title fix(vue-app): interrupt page render early on error or navigating away fix(vue-app): skip page render early on error or navigation May 22, 2023
@danielroe danielroe mentioned this pull request Jun 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants