Skip to content

Commit b824a27

Browse files
committed
fix: trigger meta refresh on page load (fixes #283)
1 parent 502c89e commit b824a27

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

examples/vue-router/app.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ const ChildComponent = {
1111
template: `<h3>You're looking at the <strong>{{ page }}</strong> page</h3>`,
1212
metaInfo () {
1313
return {
14-
title: this.page
14+
title: `${this.page} - ${this.date && this.date.toTimeString()}`
1515
}
16+
},
17+
data() {
18+
return {
19+
date: null
20+
};
21+
},
22+
mounted() {
23+
setInterval(() => {
24+
this.date = new Date();
25+
}, 1000);
1626
}
1727
}
1828

src/shared/mixin.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export default function createMixin(options) {
99
const updateOnLifecycleHook = ['activated', 'deactivated', 'beforeMount']
1010

1111
const triggerUpdate = (vm) => {
12-
// batch potential DOM updates to prevent extraneous re-rendering
13-
batchID = batchUpdate(batchID, () => vm.$meta().refresh())
12+
if (vm.$root._vueMetaInitialized) {
13+
// batch potential DOM updates to prevent extraneous re-rendering
14+
batchID = batchUpdate(batchID, () => vm.$meta().refresh())
15+
}
1416
}
1517

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

51+
// force an initial refresh on page load and prevent other lifecycleHooks
52+
// to triggerUpdate until this initial refresh is finished
53+
// this is to make sure that when a page is opened in an inactive tab which
54+
// has throttled rAF/timers we still immeditately set the page title
55+
if (isUndefined(this.$root._vueMetaInitialized)) {
56+
this.$root._vueMetaInitialized = false
57+
58+
this.$root.$options.mounted = this.$root.$options.mounted || []
59+
this.$root.$options.mounted.push(() => {
60+
if (!this.$root._vueMetaInitialized) {
61+
this.$nextTick(function () {
62+
this.$root.$meta().refresh()
63+
this.$root._vueMetaInitialized = true
64+
})
65+
}
66+
})
67+
}
68+
4969
// do not trigger refresh on the server side
5070
if (!this.$isServer) {
5171
// re-render meta data when returning from a child component to parent

0 commit comments

Comments
 (0)