Skip to content

Commit 97badf6

Browse files
committed
feat: add afterNavigation callback (fix: #259)
1 parent 173b31d commit 97badf6

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,24 @@ Will be called when the client `metaInfo` updates/changes. Receives the followin
657657

658658
Default `false`. If set to `true` then vue-meta will pause updating `metaInfo` during page navigation and only refresh once when navigation has finished. It does this by adding a global beforeEach and afterEach navigation guard on the vue-router instance.
659659

660+
#### `afterNavigation` (Function)
661+
662+
Will be called when the client `metaInfo` has changed after navigation occured. Receives the following parameters:
663+
- `newInfo` (Object) - The new state of the `metaInfo` object.
664+
665+
> :warning: This option only works when `refreshOnceOnNavigation: true`. Please see the [vue-router example](./examples/vue-router)
666+
667+
`this` context is the component instance `afterNavigation` is defined on.
668+
669+
```js
670+
{
671+
metaInfo: {
672+
afterNavigation (newInfo) {
673+
console.log('Meta info update finished after navigation!')
674+
}
675+
}
676+
}
677+
```
660678

661679
### How `metaInfo` is Resolved
662680

examples/vue-router/app.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@ Vue.use(VueMeta, {
77
refreshOnceOnNavigation: true
88
})
99

10+
let metaUpdated = 'no'
1011
const ChildComponent = {
1112
name: `child-component`,
1213
props: ['page'],
13-
template: `<h3>You're looking at the <strong>{{ page }}</strong> page</h3>`,
14+
template: `<div>
15+
<h3>You're looking at the <strong>{{ page }}</strong> page</h3>
16+
<p>Has metaInfo been updated? {{ metaUpdated }}</p>
17+
</div>`,
1418
metaInfo () {
1519
return {
16-
title: `${this.page} - ${this.date && this.date.toTimeString()}`
20+
title: `${this.page} - ${this.date && this.date.toTimeString()}`,
21+
afterNavigation() {
22+
metaUpdated = 'yes'
23+
}
1724
}
1825
},
1926
data() {
2027
return {
21-
date: null
28+
date: null,
29+
metaUpdated
2230
};
2331
},
2432
mounted() {

src/client/refresh.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export default function _refresh(options = {}) {
3030
metaInfo.changed.call(this, metaInfo, tags.addedTags, tags.removedTags)
3131
}
3232

33-
return metaInfo
33+
return { vm: this, metaInfo, tags }
3434
}
3535
}

src/shared/mixin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ export default function createMixin(Vue, options) {
8181
next()
8282
})
8383

84-
$router.afterEach(() => $rootMeta.resume())
84+
$router.afterEach(() => {
85+
const { vm, metaInfo } = $rootMeta.resume()
86+
if (metaInfo && metaInfo.afterNavigation && isFunction(metaInfo.afterNavigation)) {
87+
metaInfo.afterNavigation.call(vm, metaInfo)
88+
}
89+
})
8590
}
8691
}
8792
}

src/shared/options.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isObject } from './typeof'
1+
import { isObject, isFunction } from './typeof'
22

33
import {
44
keyName,
@@ -29,5 +29,15 @@ export default function setOptions(options) {
2929
}
3030
}
3131

32+
if (options.afterNavigation && !isFunction(options.afterNavigation)) {
33+
console.warn(`afterNavigation should be a function, received ${typeof options.afterNavigation} instead`) // eslint-disable-line no-console
34+
options.afterNavigation = void 0
35+
return options
36+
}
37+
38+
if (options.afterNavigation && !options.refreshOnceOnNavigation) {
39+
options.refreshOnceOnNavigation = true
40+
}
41+
3242
return options
3343
}

test/plugin-browser.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('plugin', () => {
100100
expect(batchUpdateSpy).not.toHaveBeenCalled()
101101
jest.clearAllMocks()
102102

103-
const metaInfo = wrapper.vm.$meta().resume()
103+
const { metaInfo } = wrapper.vm.$meta().resume()
104104
expect(metaInfo.title).toBe(title)
105105
})
106106
})

0 commit comments

Comments
 (0)