Skip to content

Commit e4b5fcc

Browse files
authored
fix(ssr): watchEffect onInvalidate runner initialization (#3323)
close #3322
1 parent bf34e33 commit e4b5fcc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/runtime-core/src/apiWatch.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function doWatch(
223223
}
224224

225225
let cleanup: () => void
226-
const onInvalidate: InvalidateCbRegistrator = (fn: () => void) => {
226+
let onInvalidate: InvalidateCbRegistrator = (fn: () => void) => {
227227
cleanup = runner.options.onStop = () => {
228228
callWithErrorHandling(fn, instance, ErrorCodes.WATCH_CLEANUP)
229229
}
@@ -232,6 +232,8 @@ function doWatch(
232232
// in SSR there is no need to setup an actual effect, and it should be noop
233233
// unless it's eager
234234
if (__NODE_JS__ && isInSSRComponentSetup) {
235+
// we will also not call the invalidate callback (+ runner is not set up)
236+
onInvalidate = NOOP
235237
if (!cb) {
236238
getter()
237239
} else if (immediate) {

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import {
88
defineComponent,
99
createTextVNode,
1010
createStaticVNode,
11-
KeepAlive,
1211
withCtx,
13-
Transition
12+
KeepAlive,
13+
Transition,
14+
watchEffect
1415
} from 'vue'
1516
import { escapeHtml } from '@vue/shared'
1617
import { renderToString } from '../src/renderToString'
@@ -775,5 +776,15 @@ function testRender(type: string, render: typeof renderToString) {
775776
const html = await render(app)
776777
expect(html).toBe(`<div>hello</div>`)
777778
})
779+
780+
// https://github.com/vuejs/vue-next/issues/3322
781+
test('effect onInvalidate does not error', async () => {
782+
const noop = () => {}
783+
const app = createApp({
784+
setup: () => watchEffect(onInvalidate => onInvalidate(noop)),
785+
render: noop,
786+
})
787+
expect(await render(app)).toBe('<!---->')
788+
})
778789
})
779790
}

0 commit comments

Comments
 (0)