fix(watch): watchEffect clean-up with SSR #12097
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
close #11956 see #11956 (comment)
Background: #11956 (comment)
#11884 removed internal support for using
once: true
withwatchEffect
. While this was never part of the public API, it was being used internally to perform clean-up during SSR.This potentially leads to
watchEffect
leaking during SSR, though I believe this would only be a problem in practice if it was watching data that was retained across requests.Example:
Rather than adding support back into
@vue/reactivity
, I've tried to implement a fix that will only be included for SSR builds.I call the stop function immediately after creating the watcher. In the cases we care about, the watcher should have already run by this point.
I've made a big assumption:
watchEffect
withflush: 'post'
should not run during SSR. I don't know whether this assumption is valid, but the approach I've taken here won't work if that isn't correct.