Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function doWatch(
}

// in SSR there is no need to setup an actual effect, and it should be noop
// unless it's eager
// unless it's eager or sync flush
if (__SSR__ && isInSSRComponentSetup) {
// we will also not call the invalidate callback (+ runner is not set up)
onCleanup = NOOP
Expand All @@ -293,7 +293,9 @@ function doWatch(
onCleanup
])
}
return NOOP
if (flush !== 'sync') {
return NOOP
}
}

let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
Expand Down
43 changes: 43 additions & 0 deletions packages/server-renderer/__tests__/ssrWatch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createSSRApp, defineComponent, h, watch, ref } from 'vue'
import { renderToString } from '../src/renderToString'

describe('ssr: watch', () => {
// #6013
test('should work w/ flush:sync', async () => {
const App = defineComponent(() => {
const count = ref(0)
let msg = ''
watch(
count,
() => {
msg = 'hello world'
},
{ flush: 'sync' }
)
count.value = 1
expect(msg).toBe('hello world')
return () => h('div', null, msg)
})
const app = createSSRApp(App)
const html = await renderToString(app)
expect(html).toMatch('hello world')
})
test('should work w/ flush:sync', async () => {
const App = defineComponent(() => {
const count = ref(0)
let msg = 'abc'
watch(
count,
() => {
msg = 'hello world'
},
{ flush: 'sync' }
)
expect(msg).toBe('abc')
return () => h('div', null, msg)
})
const app = createSSRApp(App)
const html = await renderToString(app)
expect(html).toMatch('abc')
})
})