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

feat(history): Remove event listeners when all apps are destroyed. #3172

Merged
merged 7 commits into from May 19, 2020

Conversation

joeldenning
Copy link
Contributor

This resolves #3152 and resolves #2341

This is my first PR to vue-router and I am not sure if this was the best approach to solving the problem - please let me know! I also need to think of a good way of testing this behavior in either the e2e or unit tests. The unit tests only use the the abstract mode, but the feature only applies to html5/hash modes. And for e2e tests, I need to brainstorm the best way to verify that the hashchange and popstate listeners are being removed.

src/history/html5.js Show resolved Hide resolved
src/index.js Outdated
}
history.transitionTo(history.getCurrentLocation(), setupListeners, setupListeners)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's some new behavior here - previously transitionTo() would only get called during app initialization if using html5 or hash history, but now it is being called for abstract mode, too. I did so not because abstract mode needs this, but rather because it simplified the if/else code that existed before and appeared to have no harmful side effects.

Copy link
Member

Choose a reason for hiding this comment

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

For this part, it's better to keep the previous behavior because abstract mode cannot always directly transition: e.g. during SSR whereas History and Hash can retrieve the current location on browsers

Copy link
Member

Choose a reason for hiding this comment

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

I also realized we also need to keep the previous behavior for hash history for the hash event not to fire too early on some browsers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's better to keep the previous behavior because abstract mode cannot always directly transition:

👍 updated

I also realized we also need to keep the previous behavior for hash history for the hash event not to fire too early on some browsers

That behavior still exists in this PR, as far as I can tell. HashHistory does not set up listeners until setupListeners() is called, and setupListeners() is not called until the first history.transitionTo() finishes up (as shown in the line of code above)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thing that's different here is that now that behavior for HashHistory is also applying to Html5History - beforehand the popstate listener was set up before the first transition, but it now is set up only after the first transition.

I think that new behavior is completely fine. My guess as to why HashHistory needed that special behavior is that hashchange events are fired as a result of programmatic navigation (via location.hash = '#/new'), whereas the browser never fires popstate events never for programmatic navigation (via history.pushState(state, title, '/new'))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops I made the changes related to Abstract History weeks ago but forgot to push!! I commented and said "Updated" even though I didn't push. I have now pushed my changes.

@joeldenning
Copy link
Contributor Author

This is ready for another review.

@posva
Copy link
Member

posva commented May 12, 2020

Sorry for the delay on this. I will give it a deeper look and hopefully get it merged soon

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

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

This is a great PR! Thanks a lot for the thought and work put on this.

I think we could create a new e2e test specifically for multi apps and add a few more cases. Similar to what I have for vue-router-next (https://github.com/vuejs/vue-router-next/blob/master/e2e/multi-app/index.ts) where we can mount and unmount apps on demand. The code is a bit different because it's for Vue 3, but let me know if you need help for it. I can also be available on Discord to chat about it

src/history/html5.js Show resolved Hide resolved
src/index.js Outdated
@@ -98,6 +98,12 @@ export default class VueRouter {
// ensure we still have a main app or null if no apps
// we do not release the router so it can be reused
if (this.app === app) this.app = this.apps[0] || null

if (this.apps.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

we should be able to just check !this.app

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 done

@joeldenning
Copy link
Contributor Author

Thanks for the review - I have updated this with your feedback.

Comment on lines 33 to 34
const expectScroll = router.options.scrollBehavior
const supportsScroll = supportsPushState && expectScroll
Copy link
Member

Choose a reason for hiding this comment

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

can't we remove these lines since they are right above? They shouldn't change


methods: {
teardown () {
this.$destroy()
Copy link
Member

Choose a reason for hiding this comment

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

It's better to keep this code outside of the instance being destroyed:

  • adding unmount buttons next to the mounting ones
  • keep an array of the instances created by new Vue
  • remove the entry from the array when destroying

@joeldenning
Copy link
Contributor Author

I updated this with your feedback.

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this @joeldenning

@posva posva merged commit 4c81be8 into vuejs:dev May 19, 2020
@posva posva moved this from Long term road (high prio, low complex) to Done in Longterm May 19, 2020
@joeldenning joeldenning deleted the issue-3152 branch May 19, 2020 14:57
@posva posva added the micro frontends Features to handle micro frontends label Aug 15, 2020
@shichuanpo
Copy link

shichuanpo commented Dec 10, 2020

when function setupListeners execute after "hook:destroyed" event, this cannot work correctly!
It works if add code as below:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
micro frontends Features to handle micro frontends
Projects
Development

Successfully merging this pull request may close these issues.

Ability to detach and reattach popstate listeners Remove all beforeEach, afterEach, beforeResolve
3 participants