fix(vue-app): warn if promises and functions are in fetch state (#8348)

This commit is contained in:
Daniel Roe 2020-11-16 08:43:20 +00:00 committed by GitHub
parent 9a2e6868cf
commit 7278b10112
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import Vue from 'vue' import Vue from 'vue'
import { hasFetch, normalizeError, addLifecycleHook } from '../utils' import { hasFetch, normalizeError, addLifecycleHook, purifyData } from '../utils'
async function serverPrefetch() { async function serverPrefetch() {
if (!this._fetchOnServer) { if (!this._fetchOnServer) {
@ -26,7 +26,7 @@ async function serverPrefetch() {
attrs['data-fetch-key'] = this._fetchKey attrs['data-fetch-key'] = this._fetchKey
// Add to ssrContext for window.__NUXT__.fetch // Add to ssrContext for window.__NUXT__.fetch
this.$ssrContext.nuxt.fetch.push(this.$fetchState.error ? { _error: this.$fetchState.error } : this._data) this.$ssrContext.nuxt.fetch.push(this.$fetchState.error ? { _error: this.$fetchState.error } : purifyData(this._data))
} }
export default { export default {

View File

@ -25,6 +25,24 @@ export function interopDefault (promise) {
export function hasFetch(vm) { export function hasFetch(vm) {
return vm.$options && typeof vm.$options.fetch === 'function' && !vm.$options.fetch.length return vm.$options && typeof vm.$options.fetch === 'function' && !vm.$options.fetch.length
} }
export function purifyData(data) {
if (process.env.NODE_ENV === 'production') {
return data
}
return Object.entries(data).filter(
([key, value]) => {
const valid = !(value instanceof Function) && !(value instanceof Promise)
if (!valid) {
console.warn(`${key} is not able to be stringified. This will break in a production environment.`)
}
return valid
}
).reduce((obj, [key, value]) => {
obj[key] = value
return obj
}, {})
}
export function getChildrenComponentInstancesUsingFetch(vm, instances = []) { export function getChildrenComponentInstancesUsingFetch(vm, instances = []) {
const children = vm.$children || [] const children = vm.$children || []
for (const child of children) { for (const child of children) {