-
-
Notifications
You must be signed in to change notification settings - Fork 5k
feat(history): Remove event listeners when all apps are destroyed. #3172
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
Changes from 1 commit
037a643
f923d37
6fdde38
db3e347
9d0ba02
97470df
1c90043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,37 @@ import { setupScroll, handleScroll } from '../util/scroll' | |
import { pushState, replaceState, supportsPushState } from '../util/push-state' | ||
|
||
export class HTML5History extends History { | ||
+initLocation: string | ||
|
||
constructor (router: Router, base: ?string) { | ||
super(router, base) | ||
|
||
this.initLocation = getLocation(this.base) | ||
} | ||
|
||
setupListeners () { | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.listeners.length > 0) { | ||
return | ||
} | ||
|
||
const router = this.router | ||
const expectScroll = router.options.scrollBehavior | ||
const supportsScroll = supportsPushState && expectScroll | ||
|
||
if (supportsScroll) { | ||
setupScroll() | ||
this.listeners.push(setupScroll()) | ||
} | ||
|
||
const initLocation = getLocation(this.base) | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
window.addEventListener('popstate', e => { | ||
const handleRoutingEvent = () => { | ||
const expectScroll = router.options.scrollBehavior | ||
const supportsScroll = supportsPushState && expectScroll | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
const current = this.current | ||
|
||
// Avoiding first `popstate` event dispatched in some browsers but first | ||
// history route not updated since async guard at the same time. | ||
const location = getLocation(this.base) | ||
if (this.current === START && location === initLocation) { | ||
if (this.current === START && location === this.initLocation) { | ||
return | ||
} | ||
|
||
|
@@ -34,6 +47,10 @@ export class HTML5History extends History { | |
handleScroll(router, route, current, true) | ||
} | ||
}) | ||
} | ||
window.addEventListener('popstate', handleRoutingEvent) | ||
this.listeners.push(() => { | ||
window.removeEventListener('popstate', handleRoutingEvent) | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should be able to just check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 done |
||
// clean up event listeners | ||
// https://github.com/vuejs/vue-router/issues/2341 | ||
this.history.teardownListeners() | ||
} | ||
}) | ||
|
||
// main app previously initialized | ||
|
@@ -110,18 +116,10 @@ export default class VueRouter { | |
|
||
const history = this.history | ||
|
||
if (history instanceof HTML5History) { | ||
history.transitionTo(history.getCurrentLocation()) | ||
} else if (history instanceof HashHistory) { | ||
const setupHashListener = () => { | ||
history.setupListeners() | ||
} | ||
history.transitionTo( | ||
history.getCurrentLocation(), | ||
setupHashListener, | ||
setupHashListener | ||
) | ||
const setupListeners = () => { | ||
history.setupListeners() | ||
} | ||
history.transitionTo(history.getCurrentLocation(), setupListeners, setupListeners) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's some new behavior here - previously There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍 updated
That behavior still exists in this PR, as far as I can tell. HashHistory does not set up listeners until There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
history.listen(route => { | ||
this.apps.forEach((app) => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.