Skip to content

Commit

Permalink
feat: add afterNavigation callback (fix: #259)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Mar 5, 2019
1 parent 173b31d commit 97badf6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,24 @@ Will be called when the client `metaInfo` updates/changes. Receives the followin

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.

#### `afterNavigation` (Function)

Will be called when the client `metaInfo` has changed after navigation occured. Receives the following parameters:
- `newInfo` (Object) - The new state of the `metaInfo` object.

> :warning: This option only works when `refreshOnceOnNavigation: true`. Please see the [vue-router example](./examples/vue-router)
`this` context is the component instance `afterNavigation` is defined on.

```js
{
metaInfo: {
afterNavigation (newInfo) {
console.log('Meta info update finished after navigation!')
}
}
}
```

### How `metaInfo` is Resolved

Expand Down
14 changes: 11 additions & 3 deletions examples/vue-router/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ Vue.use(VueMeta, {
refreshOnceOnNavigation: true
})

let metaUpdated = 'no'
const ChildComponent = {
name: `child-component`,
props: ['page'],
template: `<h3>You're looking at the <strong>{{ page }}</strong> page</h3>`,
template: `<div>
<h3>You're looking at the <strong>{{ page }}</strong> page</h3>
<p>Has metaInfo been updated? {{ metaUpdated }}</p>
</div>`,
metaInfo () {
return {
title: `${this.page} - ${this.date && this.date.toTimeString()}`
title: `${this.page} - ${this.date && this.date.toTimeString()}`,
afterNavigation() {
metaUpdated = 'yes'
}
}
},
data() {
return {
date: null
date: null,
metaUpdated
};
},
mounted() {
Expand Down
2 changes: 1 addition & 1 deletion src/client/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export default function _refresh(options = {}) {
metaInfo.changed.call(this, metaInfo, tags.addedTags, tags.removedTags)
}

return metaInfo
return { vm: this, metaInfo, tags }
}
}
7 changes: 6 additions & 1 deletion src/shared/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ export default function createMixin(Vue, options) {
next()
})

$router.afterEach(() => $rootMeta.resume())
$router.afterEach(() => {
const { vm, metaInfo } = $rootMeta.resume()
if (metaInfo && metaInfo.afterNavigation && isFunction(metaInfo.afterNavigation)) {
metaInfo.afterNavigation.call(vm, metaInfo)
}
})
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/shared/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from './typeof'
import { isObject, isFunction } from './typeof'

import {
keyName,
Expand Down Expand Up @@ -29,5 +29,15 @@ export default function setOptions(options) {
}
}

if (options.afterNavigation && !isFunction(options.afterNavigation)) {
console.warn(`afterNavigation should be a function, received ${typeof options.afterNavigation} instead`) // eslint-disable-line no-console
options.afterNavigation = void 0
return options
}

if (options.afterNavigation && !options.refreshOnceOnNavigation) {
options.refreshOnceOnNavigation = true
}

return options
}
2 changes: 1 addition & 1 deletion test/plugin-browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('plugin', () => {
expect(batchUpdateSpy).not.toHaveBeenCalled()
jest.clearAllMocks()

const metaInfo = wrapper.vm.$meta().resume()
const { metaInfo } = wrapper.vm.$meta().resume()
expect(metaInfo.title).toBe(title)
})
})

0 comments on commit 97badf6

Please sign in to comment.