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_V4: ${{ matrix.version == 'v4' }}
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
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 { RouteLocationNormalizedLoaded } from '#vue-router'
import type { HookCallback, Hookable } from 'hookable'
@ -252,12 +252,11 @@ export function createNuxtApp (options: CreateOptions) {
get nuxt () { return __NUXT_VERSION__ },
get vue () { return nuxtApp.vueApp.version },
},
payload: reactive({
data: {},
state: {},
payload: shallowReactive({
data: shallowReactive({}),
state: reactive({}),
once: new Set<string>(),
_errors: {},
...(import.meta.client ? window.__NUXT__ ?? {} : { serverRendered: true }),
_errors: shallowReactive({}),
}),
static: {
data: {},
@ -288,11 +287,32 @@ export function createNuxtApp (options: CreateOptions) {
}
},
_asyncDataPromises: {},
_asyncData: {},
_asyncData: shallowReactive({}),
_payloadRevivers: {},
...options,
} 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.hook = nuxtApp.hooks.hook