Skip to content

Commit

Permalink
fix(ssr): respect app.config.warnHandler during ssr
Browse files Browse the repository at this point in the history
close #11830
  • Loading branch information
yyx990803 committed Sep 6, 2024
1 parent 8e6c337 commit bf3d9a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -410,6 +411,8 @@ const _ssrUtils: {
normalizeVNode: typeof normalizeVNode
getComponentPublicInstance: typeof getComponentPublicInstance
ensureValidVNode: typeof ensureValidVNode
pushWarningContext: typeof pushWarningContext
popWarningContext: typeof popWarningContext
} = {
createComponentInstance,
setupComponent,
Expand All @@ -419,6 +422,8 @@ const _ssrUtils: {
normalizeVNode,
getComponentPublicInstance,
ensureValidVNode,
pushWarningContext,
popWarningContext,
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 11 additions & 1 deletion packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const {
setupComponent,
renderComponentRoot,
normalizeVNode,
pushWarningContext,
popWarningContext,
} = ssrUtils

export type SSRBuffer = SSRBufferItem[] & { hasAsync?: boolean }
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -207,6 +216,7 @@ function renderComponentSubTree(
push(`<!---->`)
}
}
if (__DEV__) popWarningContext()
return getBuffer()
}

Expand Down

0 comments on commit bf3d9a2

Please sign in to comment.