Skip to content

Commit bf3d9a2

Browse files
committed
fix(ssr): respect app.config.warnHandler during ssr
close #11830
1 parent 8e6c337 commit bf3d9a2

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

packages/runtime-core/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ import { renderComponentRoot } from './componentRenderUtils'
400400
import { setCurrentRenderingInstance } from './componentRenderContext'
401401
import { isVNode, normalizeVNode } from './vnode'
402402
import { ensureValidVNode } from './helpers/renderSlot'
403+
import { popWarningContext, pushWarningContext } from './warning'
403404

404405
const _ssrUtils: {
405406
createComponentInstance: typeof createComponentInstance
@@ -410,6 +411,8 @@ const _ssrUtils: {
410411
normalizeVNode: typeof normalizeVNode
411412
getComponentPublicInstance: typeof getComponentPublicInstance
412413
ensureValidVNode: typeof ensureValidVNode
414+
pushWarningContext: typeof pushWarningContext
415+
popWarningContext: typeof popWarningContext
413416
} = {
414417
createComponentInstance,
415418
setupComponent,
@@ -419,6 +422,8 @@ const _ssrUtils: {
419422
normalizeVNode,
420423
getComponentPublicInstance,
421424
ensureValidVNode,
425+
pushWarningContext,
426+
popWarningContext,
422427
}
423428

424429
/**

packages/server-renderer/__tests__/render.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ function testRender(type: string, render: typeof renderToString) {
8181
expect(html).toBe(`<div>foo</div>`)
8282
})
8383

84+
test('warnings should be suppressed by app.config.warnHandler', async () => {
85+
const app = createApp({
86+
render() {
87+
return h('div', this.foo)
88+
},
89+
})
90+
app.config.warnHandler = vi.fn()
91+
await render(app)
92+
expect('not defined on instance').not.toHaveBeenWarned()
93+
expect(app.config.warnHandler).toHaveBeenCalledTimes(1)
94+
})
95+
8496
describe('components', () => {
8597
test('vnode components', async () => {
8698
expect(

packages/server-renderer/src/render.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const {
3535
setupComponent,
3636
renderComponentRoot,
3737
normalizeVNode,
38+
pushWarningContext,
39+
popWarningContext,
3840
} = ssrUtils
3941

4042
export type SSRBuffer = SSRBufferItem[] & { hasAsync?: boolean }
@@ -91,8 +93,14 @@ export function renderComponentVNode(
9193
parentComponent: ComponentInternalInstance | null = null,
9294
slotScopeId?: string,
9395
): SSRBuffer | Promise<SSRBuffer> {
94-
const instance = createComponentInstance(vnode, parentComponent, null)
96+
const instance = (vnode.component = createComponentInstance(
97+
vnode,
98+
parentComponent,
99+
null,
100+
))
101+
if (__DEV__) pushWarningContext(vnode)
95102
const res = setupComponent(instance, true /* isSSR */)
103+
if (__DEV__) popWarningContext()
96104
const hasAsyncSetup = isPromise(res)
97105
let prefetches = instance.sp /* LifecycleHooks.SERVER_PREFETCH */
98106
if (hasAsyncSetup || prefetches) {
@@ -118,6 +126,7 @@ function renderComponentSubTree(
118126
instance: ComponentInternalInstance,
119127
slotScopeId?: string,
120128
): SSRBuffer | Promise<SSRBuffer> {
129+
if (__DEV__) pushWarningContext(instance.vnode)
121130
const comp = instance.type as Component
122131
const { getBuffer, push } = createBuffer()
123132
if (isFunction(comp)) {
@@ -207,6 +216,7 @@ function renderComponentSubTree(
207216
push(`<!---->`)
208217
}
209218
}
219+
if (__DEV__) popWarningContext()
210220
return getBuffer()
211221
}
212222

0 commit comments

Comments
 (0)