2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-24 12:50:34 +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 // for which Vue lifecycle hooks should the metaInfo be refreshed
const updateOnLifecycleHook = ['activated', 'deactivated', 'beforeMount'] const updateOnLifecycleHook = ['activated', 'deactivated', 'beforeMount']
let wasServerRendered = false let wasServerRendered = false
let wasRedirectedOnLoad = false
let removeRedirectListener
// watch for client side component updates // watch for client side component updates
return { return {
@@ -35,6 +37,13 @@ export default function createMixin (Vue, options) {
}) })
if (this === $root) { if (this === $root) {
if ($root.$router) {
removeRedirectListener = $root.$router.beforeEach((to, from, next) => {
wasRedirectedOnLoad = true
next()
})
}
$root.$once('hook:beforeMount', function () { $root.$once('hook:beforeMount', function () {
wasServerRendered = this.$el && this.$el.nodeType === 1 && this.$el.hasAttribute('data-server-rendered') 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 // to be applied OR a metaInfo watcher was triggered before the
// current hook was called // current hook was called
// (during initialization all changes are blocked) // (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')) this.$nextTick(() => triggerUpdate(options, $root, 'init'))
} }
@@ -161,6 +170,10 @@ export default function createMixin (Vue, options) {
if (!options.refreshOnceOnNavigation && metaInfo.afterNavigation) { if (!options.refreshOnceOnNavigation && metaInfo.afterNavigation) {
addNavGuards($root) addNavGuards($root)
} }
if (removeRedirectListener) {
removeRedirectListener()
}
}) })
}) })
// add the navigation guards if requested // add the navigation guards if requested
+7 -7
View File
@@ -514,28 +514,28 @@ describe('components', () => {
}) })
test('can enable option refreshOnceOnNavigation runtime', () => { test('can enable option refreshOnceOnNavigation runtime', () => {
const guards = {} const guards = { before: [], after: [] }
const wrapper = mount(HelloWorld, { const wrapper = mount(HelloWorld, {
localVue: Vue, localVue: Vue,
mocks: { mocks: {
$router: { $router: {
beforeEach (fn) { beforeEach (fn) {
guards.before = fn guards.before.push(fn)
}, },
afterEach (fn) { afterEach (fn) {
guards.after = fn guards.after.push(fn)
} }
} }
} }
}) })
expect(guards.before).toBeUndefined() expect(guards.before).toEqual([expect.any(Function)])
expect(guards.after).toBeUndefined() expect(guards.after).toEqual([])
wrapper.vm.$meta().setOptions({ refreshOnceOnNavigation: true }) wrapper.vm.$meta().setOptions({ refreshOnceOnNavigation: true })
expect(guards.before).not.toBeUndefined() expect(guards.before).toEqual([expect.any(Function), expect.any(Function)])
expect(guards.after).not.toBeUndefined() expect(guards.after).toEqual([expect.any(Function)])
}) })
test('destroyed hook calls triggerUpdate delayed', async () => { test('destroyed hook calls triggerUpdate delayed', async () => {