Skip to content

Commit

Permalink
fix: trigger meta refresh on page load (fixes #283)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Feb 11, 2019
1 parent 502c89e commit b824a27
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 11 additions & 1 deletion examples/vue-router/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ const ChildComponent = {
template: `<h3>You're looking at the <strong>{{ page }}</strong> page</h3>`,
metaInfo () {
return {
title: this.page
title: `${this.page} - ${this.date && this.date.toTimeString()}`
}
},
data() {
return {
date: null
};
},
mounted() {
setInterval(() => {
this.date = new Date();
}, 1000);
}
}

Expand Down
24 changes: 22 additions & 2 deletions src/shared/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export default function createMixin(options) {
const updateOnLifecycleHook = ['activated', 'deactivated', 'beforeMount']

const triggerUpdate = (vm) => {
// batch potential DOM updates to prevent extraneous re-rendering
batchID = batchUpdate(batchID, () => vm.$meta().refresh())
if (vm.$root._vueMetaInitialized) {
// batch potential DOM updates to prevent extraneous re-rendering
batchID = batchUpdate(batchID, () => vm.$meta().refresh())
}
}

// watch for client side component updates
Expand Down Expand Up @@ -46,6 +48,24 @@ export default function createMixin(options) {
this.$options[lifecycleHook].push(() => triggerUpdate(this))
})

// force an initial refresh on page load and prevent other lifecycleHooks
// to triggerUpdate until this initial refresh is finished
// this is to make sure that when a page is opened in an inactive tab which
// has throttled rAF/timers we still immeditately set the page title
if (isUndefined(this.$root._vueMetaInitialized)) {
this.$root._vueMetaInitialized = false

this.$root.$options.mounted = this.$root.$options.mounted || []
this.$root.$options.mounted.push(() => {
if (!this.$root._vueMetaInitialized) {
this.$nextTick(function () {
this.$root.$meta().refresh()
this.$root._vueMetaInitialized = true
})
}
})
}

// do not trigger refresh on the server side
if (!this.$isServer) {
// re-render meta data when returning from a child component to parent
Expand Down

0 comments on commit b824a27

Please sign in to comment.