Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): watchEffect onInvalidate runner initialization (close #3322) #3323

Merged
merged 7 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function doWatch(
}

let cleanup: () => void
const onInvalidate: InvalidateCbRegistrator = (fn: () => void) => {
let onInvalidate: InvalidateCbRegistrator = (fn: () => void) => {
cleanup = runner.options.onStop = () => {
callWithErrorHandling(fn, instance, ErrorCodes.WATCH_CLEANUP)
}
Expand All @@ -232,6 +232,8 @@ function doWatch(
// in SSR there is no need to setup an actual effect, and it should be noop
// unless it's eager
if (__NODE_JS__ && isInSSRComponentSetup) {
// we will also not call the invalidate callback (+ runner is not set up)
onInvalidate = NOOP
if (!cb) {
getter()
} else if (immediate) {
Expand Down
15 changes: 13 additions & 2 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
defineComponent,
createTextVNode,
createStaticVNode,
KeepAlive,
withCtx,
Transition
KeepAlive,
Transition,
watchEffect
} from 'vue'
import { escapeHtml } from '@vue/shared'
import { renderToString } from '../src/renderToString'
Expand Down Expand Up @@ -775,5 +776,15 @@ function testRender(type: string, render: typeof renderToString) {
const html = await render(app)
expect(html).toBe(`<div>hello</div>`)
})

// https://github.com/vuejs/vue-next/issues/3322
test('effect onInvalidate does not error', async () => {
const noop = () => {}
const app = createApp({
setup: () => watchEffect(onInvalidate => onInvalidate(noop)),
render: noop,
})
expect(await render(app)).toBe('<!---->')
})
})
}