diff --git a/src/shared/mixin.js b/src/shared/mixin.js index e39aac5..be0b1f1 100644 --- a/src/shared/mixin.js +++ b/src/shared/mixin.js @@ -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 diff --git a/test/unit/components.test.js b/test/unit/components.test.js index 9dc35e2..82353e8 100644 --- a/test/unit/components.test.js +++ b/test/unit/components.test.js @@ -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 () => {