2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-23 21:00:34 +03:00

fix: workaround for memoryleak in destroyed hook

This commit is contained in:
pimlie
2019-08-30 18:10:15 +02:00
parent dc305440c4
commit ec7b1fbefd
+20 -14
View File
@@ -29,7 +29,10 @@ export default function createMixin (Vue, options) {
// Add a marker to know if it uses metaInfo // Add a marker to know if it uses metaInfo
// _vnode is used to know that it's attached to a real component // _vnode is used to know that it's attached to a real component
// useful if we use some mixin to add some meta tags (like nuxt-i18n) // useful if we use some mixin to add some meta tags (like nuxt-i18n)
if (!isUndefined(this.$options[options.keyName]) && this.$options[options.keyName] !== null) { if (isUndefined(this.$options[options.keyName]) || this.$options[options.keyName] === null) {
return
}
if (!this.$root._vueMeta) { if (!this.$root._vueMeta) {
this.$root._vueMeta = { appId } this.$root._vueMeta = { appId }
appId++ appId++
@@ -62,7 +65,8 @@ export default function createMixin (Vue, options) {
// when it changes (i.e. automatically handle async actions that affect metaInfo) // when it changes (i.e. automatically handle async actions that affect metaInfo)
// credit for this suggestion goes to [Sébastien Chopin](https://github.com/Atinux) // credit for this suggestion goes to [Sébastien Chopin](https://github.com/Atinux)
ensuredPush(this.$options, 'created', () => { ensuredPush(this.$options, 'created', () => {
this.$watch('$metaInfo', function () { this.$watch('$metaInfo', () => {
this.__metaInfo = undefined
triggerUpdate(this, 'watcher') triggerUpdate(this, 'watcher')
}) })
}) })
@@ -126,33 +130,35 @@ export default function createMixin (Vue, options) {
} }
// do not trigger refresh on the server side // do not trigger refresh on the server side
if (!this.$isServer) { if (this.$isServer) {
return
}
// no need to add this hooks on server side // no need to add this hooks on server side
updateOnLifecycleHook.forEach((lifecycleHook) => { updateOnLifecycleHook.forEach((lifecycleHook) => {
ensuredPush(this.$options, lifecycleHook, () => triggerUpdate(this, lifecycleHook)) ensuredPush(this.$options, lifecycleHook, () => triggerUpdate(this, lifecycleHook))
}) })
},
// TODO: move back into beforeCreate when Vue issue is resolved
destroyed () {
// do not trigger refresh:
// - on the server side
// - when the component doesnt have a parent
// - doesnt have metaInfo defined
if (this.$isServer || !this.$parent || !hasMetaInfo(this)) {
return
}
// re-render meta data when returning from a child component to parent
ensuredPush(this.$options, 'destroyed', () => {
// Wait that element is hidden before refreshing meta tags (to support animations) // Wait that element is hidden before refreshing meta tags (to support animations)
const interval = setInterval(() => { const interval = setInterval(() => {
if (this.$el && this.$el.offsetParent !== null) { if (this.$el && this.$el.offsetParent !== null) {
/* istanbul ignore next line */
return return
} }
clearInterval(interval) clearInterval(interval)
if (!this.$parent) {
/* istanbul ignore next line */
return
}
triggerUpdate(this, 'destroyed') triggerUpdate(this, 'destroyed')
}, 50) }, 50)
})
}
}
} }
} }
} }