]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(ssr): respect app.config.warnHandler during ssr
authorEvan You <evan@vuejs.org>
Fri, 6 Sep 2024 03:19:42 +0000 (11:19 +0800)
committerEvan You <evan@vuejs.org>
Fri, 6 Sep 2024 03:20:48 +0000 (11:20 +0800)
close #11830

packages/runtime-core/src/index.ts
packages/server-renderer/__tests__/render.spec.ts
packages/server-renderer/src/render.ts

index 7f716b5f4e8e9a4d0522e238b4dd68786ae29eb7..3871167b3eeaf4655d7fe7fd36781a1f3b560a87 100644 (file)
@@ -400,6 +400,7 @@ import { renderComponentRoot } from './componentRenderUtils'
 import { setCurrentRenderingInstance } from './componentRenderContext'
 import { isVNode, normalizeVNode } from './vnode'
 import { ensureValidVNode } from './helpers/renderSlot'
+import { popWarningContext, pushWarningContext } from './warning'
 
 const _ssrUtils: {
   createComponentInstance: typeof createComponentInstance
@@ -410,6 +411,8 @@ const _ssrUtils: {
   normalizeVNode: typeof normalizeVNode
   getComponentPublicInstance: typeof getComponentPublicInstance
   ensureValidVNode: typeof ensureValidVNode
+  pushWarningContext: typeof pushWarningContext
+  popWarningContext: typeof popWarningContext
 } = {
   createComponentInstance,
   setupComponent,
@@ -419,6 +422,8 @@ const _ssrUtils: {
   normalizeVNode,
   getComponentPublicInstance,
   ensureValidVNode,
+  pushWarningContext,
+  popWarningContext,
 }
 
 /**
index 1b1d6256e8c97cd4868d3f71655d1d93d7cbf0d4..d0a5223b2ff635b2df39b187f8f4438b01bfff38 100644 (file)
@@ -81,6 +81,18 @@ function testRender(type: string, render: typeof renderToString) {
       expect(html).toBe(`<div>foo</div>`)
     })
 
+    test('warnings should be suppressed by app.config.warnHandler', async () => {
+      const app = createApp({
+        render() {
+          return h('div', this.foo)
+        },
+      })
+      app.config.warnHandler = vi.fn()
+      await render(app)
+      expect('not defined on instance').not.toHaveBeenWarned()
+      expect(app.config.warnHandler).toHaveBeenCalledTimes(1)
+    })
+
     describe('components', () => {
       test('vnode components', async () => {
         expect(
index 97179526456d663472ddac84cd66b9951536fceb..f04080b9c3127d04024b7a18cdc499118ea55af9 100644 (file)
@@ -35,6 +35,8 @@ const {
   setupComponent,
   renderComponentRoot,
   normalizeVNode,
+  pushWarningContext,
+  popWarningContext,
 } = ssrUtils
 
 export type SSRBuffer = SSRBufferItem[] & { hasAsync?: boolean }
@@ -91,8 +93,14 @@ export function renderComponentVNode(
   parentComponent: ComponentInternalInstance | null = null,
   slotScopeId?: string,
 ): SSRBuffer | Promise<SSRBuffer> {
-  const instance = createComponentInstance(vnode, parentComponent, null)
+  const instance = (vnode.component = createComponentInstance(
+    vnode,
+    parentComponent,
+    null,
+  ))
+  if (__DEV__) pushWarningContext(vnode)
   const res = setupComponent(instance, true /* isSSR */)
+  if (__DEV__) popWarningContext()
   const hasAsyncSetup = isPromise(res)
   let prefetches = instance.sp /* LifecycleHooks.SERVER_PREFETCH */
   if (hasAsyncSetup || prefetches) {
@@ -118,6 +126,7 @@ function renderComponentSubTree(
   instance: ComponentInternalInstance,
   slotScopeId?: string,
 ): SSRBuffer | Promise<SSRBuffer> {
+  if (__DEV__) pushWarningContext(instance.vnode)
   const comp = instance.type as Component
   const { getBuffer, push } = createBuffer()
   if (isFunction(comp)) {
@@ -207,6 +216,7 @@ function renderComponentSubTree(
       push(`<!---->`)
     }
   }
+  if (__DEV__) popWarningContext()
   return getBuffer()
 }