From 1cc3750b81fa7f64fce78c38f315cdad9ce7f382 Mon Sep 17 00:00:00 2001 From: pimlie Date: Tue, 8 Oct 2019 13:52:49 +0200 Subject: [PATCH 1/3] feat: try to detect global mixins adding meta info --- src/shared/mixin.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/shared/mixin.js b/src/shared/mixin.js index 517ce570..a2172914 100644 --- a/src/shared/mixin.js +++ b/src/shared/mixin.js @@ -41,6 +41,17 @@ export default function createMixin (Vue, options) { if (!$root[rootConfigKey]) { $root[rootConfigKey] = { appId } appId++ + + if ($root.$options[options.keyName]) { + // use nextTick so the children should be added to $root + this.$nextTick(() => { + // find the first child that lists fnOptions + const child = $root.$children.find(c => c.$vnode && c.$vnode.fnOptions) + if (child && child.$vnode.fnOptions[options.keyName]) { + warn(`VueMeta has detected a possible global mixin which adds a ${options.keyName} property to all Vue components on the page. This could cause severe performance issues. If possible, use $meta().addApp to add meta information instead`) + } + }) + } } // to speed up updates we keep track of branches which have a component with vue-meta info defined @@ -83,14 +94,18 @@ export default function createMixin (Vue, options) { $root[rootConfigKey].initialized = this.$isServer if (!$root[rootConfigKey].initialized) { - ensuredPush($options, 'beforeMount', function () { - const $root = this[rootKey] - // if this Vue-app was server rendered, set the appId to 'ssr' - // only one SSR app per page is supported - if ($root.$el && $root.$el.nodeType === 1 && $root.$el.hasAttribute('data-server-rendered')) { - $root[rootConfigKey].appId = options.ssrAppId - } - }) + if (!$root[rootConfigKey].initializedSsr) { + $root[rootConfigKey].initializedSsr = true + + ensuredPush($options, 'beforeMount', function () { + const $root = this + // if this Vue-app was server rendered, set the appId to 'ssr' + // only one SSR app per page is supported + if ($root.$el && $root.$el.nodeType === 1 && $root.$el.hasAttribute('data-server-rendered')) { + $root[rootConfigKey].appId = options.ssrAppId + } + }) + } // we use the mounted hook here as on page load ensuredPush($options, 'mounted', function () { @@ -154,6 +169,7 @@ export default function createMixin (Vue, options) { if (!this.$parent || !hasMetaInfo(this)) { return } + delete this._hasMetaInfo this.$nextTick(() => { if (!options.waitOnDestroyed || !this.$el || !this.$el.offsetParent) { From 8a8bcae5a25eff2fbdb47944caaf1e0a81b83750 Mon Sep 17 00:00:00 2001 From: pimlie Date: Tue, 8 Oct 2019 14:12:56 +0200 Subject: [PATCH 2/3] fix: add find polyfill for ie --- src/shared/mixin.js | 3 ++- src/utils/array.js | 13 +++++++++++++ test/unit/utils.test.js | 13 ++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/shared/mixin.js b/src/shared/mixin.js index a2172914..281652d8 100644 --- a/src/shared/mixin.js +++ b/src/shared/mixin.js @@ -1,5 +1,6 @@ import { triggerUpdate } from '../client/update' import { isUndefined, isFunction } from '../utils/is-type' +import { find } from '../utils/array' import { ensuredPush } from '../utils/ensure' import { rootConfigKey } from './constants' import { hasMetaInfo } from './meta-helpers' @@ -46,7 +47,7 @@ export default function createMixin (Vue, options) { // use nextTick so the children should be added to $root this.$nextTick(() => { // find the first child that lists fnOptions - const child = $root.$children.find(c => c.$vnode && c.$vnode.fnOptions) + const child = find($root.$children, c => c.$vnode && c.$vnode.fnOptions) if (child && child.$vnode.fnOptions[options.keyName]) { warn(`VueMeta has detected a possible global mixin which adds a ${options.keyName} property to all Vue components on the page. This could cause severe performance issues. If possible, use $meta().addApp to add meta information instead`) } diff --git a/src/utils/array.js b/src/utils/array.js index 41b24560..853b028b 100644 --- a/src/utils/array.js +++ b/src/utils/array.js @@ -11,6 +11,19 @@ // which means the polyfills are removed for other build formats const polyfill = process.env.NODE_ENV === 'test' +export function find (array, predicate, thisArg) { + if (polyfill && !Array.prototype.find) { + // idx needs to be a Number, for..in returns string + for (let idx = 0; idx < array.length; idx++) { + if (predicate.call(thisArg, array[idx], idx, array)) { + return array[idx] + } + } + return + } + return array.find(predicate, thisArg) +} + export function findIndex (array, predicate, thisArg) { if (polyfill && !Array.prototype.findIndex) { // idx needs to be a Number, for..in returns string diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js index 8f897e3b..0568531f 100644 --- a/test/unit/utils.test.js +++ b/test/unit/utils.test.js @@ -1,7 +1,7 @@ /** * @jest-environment node */ -import { findIndex, includes, toArray } from '../../src/utils/array' +import { find, findIndex, includes, toArray } from '../../src/utils/array' import { ensureIsArray } from '../../src/utils/ensure' import { hasGlobalWindowFn } from '../../src/utils/window' @@ -29,6 +29,17 @@ describe('shared', () => { }) /* eslint-disable no-extend-native */ + test('find polyfill', () => { + const _find = Array.prototype.find + Array.prototype.find = false + + const arr = [1, 2, 3] + expect(find(arr, (v, i) => i === 0)).toBe(1) + expect(find(arr, (v, i) => i === 3)).toBe(undefined) + + Array.prototype.find = _find + }) + test('findIndex polyfill', () => { const _findIndex = Array.prototype.findIndex Array.prototype.findIndex = false From 715e0c7aec686e9652b376d8bd6684443ad0155d Mon Sep 17 00:00:00 2001 From: pimlie Date: Wed, 9 Oct 2019 11:40:34 +0200 Subject: [PATCH 3/3] fix: only detect global mixins when Vue.devtools: true --- src/shared/mixin.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/shared/mixin.js b/src/shared/mixin.js index 281652d8..0de2d8bf 100644 --- a/src/shared/mixin.js +++ b/src/shared/mixin.js @@ -19,12 +19,13 @@ export default function createMixin (Vue, options) { const rootKey = '$root' const $root = this[rootKey] const $options = this.$options + const devtoolsEnabled = Vue.config.devtools Object.defineProperty(this, '_hasMetaInfo', { configurable: true, get () { // Show deprecation warning once when devtools enabled - if (Vue.config.devtools && !$root[rootConfigKey].deprecationWarningShown) { + if (devtoolsEnabled && !$root[rootConfigKey].deprecationWarningShown) { warn('VueMeta DeprecationWarning: _hasMetaInfo has been deprecated and will be removed in a future version. Please use hasMetaInfo(vm) instead') $root[rootConfigKey].deprecationWarningShown = true } @@ -43,7 +44,7 @@ export default function createMixin (Vue, options) { $root[rootConfigKey] = { appId } appId++ - if ($root.$options[options.keyName]) { + if (devtoolsEnabled && $root.$options[options.keyName]) { // use nextTick so the children should be added to $root this.$nextTick(() => { // find the first child that lists fnOptions