perf(nuxt): mark payload as shallowReactive (#27214)

This commit is contained in:
Daniel Roe 2024-05-16 09:14:25 -05:00 committed by GitHub
parent b3aaa57811
commit c545c1da5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 8 deletions

View File

@ -246,7 +246,7 @@ jobs:
TEST_CONTEXT: ${{ matrix.context }} TEST_CONTEXT: ${{ matrix.context }}
TEST_V4: ${{ matrix.version == 'v4' }} TEST_V4: ${{ matrix.version == 'v4' }}
TEST_PAYLOAD: ${{ matrix.payload }} TEST_PAYLOAD: ${{ matrix.payload }}
SKIP_BUNDLE_SIZE: ${{ github.event_name != 'push' || matrix.env == 'dev' || matrix.builder == 'webpack' || matrix.context == 'default' || runner.os == 'Windows' }} SKIP_BUNDLE_SIZE: ${{ github.event_name != 'push' || matrix.env == 'dev' || matrix.builder == 'webpack' || matrix.context == 'default' || matrix.payload == 'js' || runner.os == 'Windows' }}
- uses: codecov/codecov-action@6d798873df2b1b8e5846dba6fb86631229fbcb17 # v4.4.0 - uses: codecov/codecov-action@6d798873df2b1b8e5846dba6fb86631229fbcb17 # v4.4.0
if: github.event_name != 'push' && matrix.env == 'built' && matrix.builder == 'vite' && matrix.context == 'default' && matrix.os == 'ubuntu-latest' && matrix.manifest == 'manifest-on' if: github.event_name != 'push' && matrix.env == 'built' && matrix.builder == 'vite' && matrix.context == 'default' && matrix.os == 'ubuntu-latest' && matrix.manifest == 'manifest-on'

View File

@ -1,4 +1,4 @@
import { effectScope, getCurrentInstance, getCurrentScope, hasInjectionContext, reactive } from 'vue' import { effectScope, getCurrentInstance, getCurrentScope, hasInjectionContext, reactive, shallowReactive } from 'vue'
import type { App, EffectScope, Ref, VNode, onErrorCaptured } from 'vue' import type { App, EffectScope, Ref, VNode, onErrorCaptured } from 'vue'
import type { RouteLocationNormalizedLoaded } from '#vue-router' import type { RouteLocationNormalizedLoaded } from '#vue-router'
import type { HookCallback, Hookable } from 'hookable' import type { HookCallback, Hookable } from 'hookable'
@ -252,12 +252,11 @@ export function createNuxtApp (options: CreateOptions) {
get nuxt () { return __NUXT_VERSION__ }, get nuxt () { return __NUXT_VERSION__ },
get vue () { return nuxtApp.vueApp.version }, get vue () { return nuxtApp.vueApp.version },
}, },
payload: reactive({ payload: shallowReactive({
data: {}, data: shallowReactive({}),
state: {}, state: reactive({}),
once: new Set<string>(), once: new Set<string>(),
_errors: {}, _errors: shallowReactive({}),
...(import.meta.client ? window.__NUXT__ ?? {} : { serverRendered: true }),
}), }),
static: { static: {
data: {}, data: {},
@ -288,11 +287,32 @@ export function createNuxtApp (options: CreateOptions) {
} }
}, },
_asyncDataPromises: {}, _asyncDataPromises: {},
_asyncData: {}, _asyncData: shallowReactive({}),
_payloadRevivers: {}, _payloadRevivers: {},
...options, ...options,
} as any as NuxtApp } as any as NuxtApp
if (import.meta.server) {
nuxtApp.payload.serverRendered = true
}
// TODO: remove/refactor in https://github.com/nuxt/nuxt/issues/25336
if (import.meta.client && window.__NUXT__) {
for (const key in window.__NUXT__) {
switch (key) {
case 'data':
case 'state':
case '_errors':
// Preserve reactivity for non-rich payload support
Object.assign(nuxtApp.payload[key], window.__NUXT__[key])
break
default:
nuxtApp.payload[key] = window.__NUXT__[key]
}
}
}
nuxtApp.hooks = createHooks<RuntimeNuxtHooks>() nuxtApp.hooks = createHooks<RuntimeNuxtHooks>()
nuxtApp.hook = nuxtApp.hooks.hook nuxtApp.hook = nuxtApp.hooks.hook