2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-16 15:10:33 +03:00

feat: add option to refresh once during navigation (possible fix for #320)

chore: add es build

chore: global window detection

chore: small refactor improvements
This commit is contained in:
pimlie
2019-02-20 18:35:01 +01:00
parent 087e4abe76
commit 8e211751df
12 changed files with 145 additions and 7 deletions
+5 -2
View File
@@ -2,6 +2,9 @@ import { pause, resume } from '../shared/pausing'
import refresh from './refresh'
export default function _$meta(options = {}) {
const _refresh = refresh(options)
const inject = () => {}
/**
* Returns an injector for server-side rendering.
* @this {Object} - the Vue instance (a root component)
@@ -9,8 +12,8 @@ export default function _$meta(options = {}) {
*/
return function $meta() {
return {
inject: () => {},
refresh: refresh(options).bind(this),
refresh: _refresh.bind(this),
inject,
pause: pause.bind(this),
resume: resume.bind(this)
}
+14
View File
@@ -0,0 +1,14 @@
import batchUpdate from './batchUpdate'
// store an id to keep track of DOM updates
let batchId = null
export default function triggerUpdate(vm, hookName) {
if (vm.$root._vueMetaInitialized && !vm.$root._vueMetaPaused) {
// batch potential DOM updates to prevent extraneous re-rendering
batchId = batchUpdate(batchId, () => {
vm.$meta().refresh()
batchId = null
})
}
}
+5 -2
View File
@@ -3,6 +3,9 @@ import { pause, resume } from '../shared/pausing'
import inject from './inject'
export default function _$meta(options = {}) {
const _refresh = refresh(options)
const _inject = inject(options)
/**
* Returns an injector for server-side rendering.
* @this {Object} - the Vue instance (a root component)
@@ -10,8 +13,8 @@ export default function _$meta(options = {}) {
*/
return function $meta() {
return {
inject: inject(options).bind(this),
refresh: refresh(options).bind(this),
refresh: _refresh.bind(this),
inject: _inject.bind(this),
pause: pause.bind(this),
resume: resume.bind(this)
}
+19
View File
@@ -0,0 +1,19 @@
import isArray from './isArray'
import { isObject } from './typeof'
export function ensureIsArray(arg, key) {
if (key && isObject(arg)) {
if (!isArray(arg[key])) {
arg[key] = []
}
return arg
} else {
return isArray(arg) ? arg : []
}
}
export function ensuredPush(object, key, el) {
ensureIsArray(object, key)
object[key].push(el)
}
+1 -1
View File
@@ -1,6 +1,6 @@
import deepmerge from 'deepmerge'
import uniqueId from 'lodash.uniqueid'
import { isUndefined, isFunction, isObject } from '../shared/typeof'
import { isUndefined, isFunction, isObject } from './typeof'
import uniqBy from './uniqBy'
/**
+15 -1
View File
@@ -43,15 +43,29 @@ export default function createMixin(options) {
this.$root._vueMetaInitialized = this.$isServer
if (!this.$root._vueMetaInitialized) {
const $rootMeta = this.$root.$meta()
ensuredPush(this.$options, 'mounted', () => {
if (!this.$root._vueMetaInitialized) {
// refresh meta in nextTick so all child components have loaded
this.$nextTick(function () {
this.$root.$meta().refresh()
$rootMeta.refresh()
this.$root._vueMetaInitialized = true
})
}
})
// add vue-router navigation guard to prevent multiple updates during navigation
// only usefull on the client side
if (options.refreshOnceOnNavigation && this.$root.$router) {
const $router = this.$root.$router
$router.beforeEach((to, from, next) => {
$rootMeta.pause()
next()
})
$router.afterEach(() => $rootMeta.resume())
}
}
}
+2 -1
View File
@@ -1,4 +1,5 @@
import { isObject } from '../shared/typeof'
import { isObject } from './typeof'
import {
keyName,
attribute,
+13
View File
@@ -0,0 +1,13 @@
export function pause(refresh = true) {
this.$root._vueMetaPaused = true
return () => resume(refresh)
}
export function resume(refresh = true) {
this.$root._vueMetaPaused = false
if (refresh) {
return this.$root.$meta().refresh()
}
}
+11
View File
@@ -0,0 +1,11 @@
import { isUndefined } from './typeof'
export function hasGlobalWindowFn() {
try {
return !isUndefined(window)
} catch (e) {
return false
}
}
export const hasGlobalWindow = hasGlobalWindowFn()