2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-14 00:22:25 +03:00

fix: also refresh meta when page was redirected during load

This commit is contained in:
pimlie
2020-05-30 12:39:26 +02:00
parent 92350d5f7d
commit 9aa8c036d3
2 changed files with 21 additions and 8 deletions
+14 -1
View File
@@ -13,6 +13,8 @@ export default function createMixin (Vue, options) {
// for which Vue lifecycle hooks should the metaInfo be refreshed
const updateOnLifecycleHook = ['activated', 'deactivated', 'beforeMount']
let wasServerRendered = false
let wasRedirectedOnLoad = false
let removeRedirectListener
// watch for client side component updates
return {
@@ -35,6 +37,13 @@ export default function createMixin (Vue, options) {
})
if (this === $root) {
if ($root.$router) {
removeRedirectListener = $root.$router.beforeEach((to, from, next) => {
wasRedirectedOnLoad = true
next()
})
}
$root.$once('hook:beforeMount', function () {
wasServerRendered = this.$el && this.$el.nodeType === 1 && this.$el.hasAttribute('data-server-rendered')
@@ -149,7 +158,7 @@ export default function createMixin (Vue, options) {
// to be applied OR a metaInfo watcher was triggered before the
// current hook was called
// (during initialization all changes are blocked)
if (tags === false && $root[rootConfigKey].initialized === null) {
if ((tags === false && $root[rootConfigKey].initialized === null) || wasRedirectedOnLoad) {
this.$nextTick(() => triggerUpdate(options, $root, 'init'))
}
@@ -161,6 +170,10 @@ export default function createMixin (Vue, options) {
if (!options.refreshOnceOnNavigation && metaInfo.afterNavigation) {
addNavGuards($root)
}
if (removeRedirectListener) {
removeRedirectListener()
}
})
})
// add the navigation guards if requested
+7 -7
View File
@@ -514,28 +514,28 @@ describe('components', () => {
})
test('can enable option refreshOnceOnNavigation runtime', () => {
const guards = {}
const guards = { before: [], after: [] }
const wrapper = mount(HelloWorld, {
localVue: Vue,
mocks: {
$router: {
beforeEach (fn) {
guards.before = fn
guards.before.push(fn)
},
afterEach (fn) {
guards.after = fn
guards.after.push(fn)
}
}
}
})
expect(guards.before).toBeUndefined()
expect(guards.after).toBeUndefined()
expect(guards.before).toEqual([expect.any(Function)])
expect(guards.after).toEqual([])
wrapper.vm.$meta().setOptions({ refreshOnceOnNavigation: true })
expect(guards.before).not.toBeUndefined()
expect(guards.after).not.toBeUndefined()
expect(guards.before).toEqual([expect.any(Function), expect.any(Function)])
expect(guards.after).toEqual([expect.any(Function)])
})
test('destroyed hook calls triggerUpdate delayed', async () => {