Skip to content

Commit

Permalink
fix(ssr): make computed inactive during ssr, fix memory leak
Browse files Browse the repository at this point in the history
fix #5208
  • Loading branch information
yyx990803 committed Jan 16, 2022
1 parent 4d07ed8 commit f4f0966
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
11 changes: 7 additions & 4 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ class ComputedRefImpl<T> {
constructor(
getter: ComputedGetter<T>,
private readonly _setter: ComputedSetter<T>,
isReadonly: boolean
isReadonly: boolean,
isSSR: boolean
) {
this.effect = new ReactiveEffect(getter, () => {
if (!this._dirty) {
this._dirty = true
triggerRefValue(this)
}
})
this.effect.active = !isSSR
this[ReactiveFlags.IS_READONLY] = isReadonly
}

Expand Down Expand Up @@ -73,7 +75,8 @@ export function computed<T>(
): WritableComputedRef<T>
export function computed<T>(
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
debugOptions?: DebuggerOptions
debugOptions?: DebuggerOptions,
isSSR = false
) {
let getter: ComputedGetter<T>
let setter: ComputedSetter<T>
Expand All @@ -91,9 +94,9 @@ export function computed<T>(
setter = getterOrOptions.set
}

const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter)
const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR)

if (__DEV__ && debugOptions) {
if (__DEV__ && debugOptions && !isSSR) {
cRef.effect.onTrack = debugOptions.onTrack
cRef.effect.onTrigger = debugOptions.onTrigger
}
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime-core/src/apiComputed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { computed as _computed } from '@vue/reactivity'
import { isInSSRComponentSetup } from './component'

export const computed = ((getterOrOptions: any, debugOptions?: any) => {
// @ts-ignore
return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
}) as typeof _computed
2 changes: 1 addition & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
export const version = __VERSION__
export {
// core
computed,
reactive,
ref,
readonly,
Expand Down Expand Up @@ -34,6 +33,7 @@ export {
getCurrentScope,
onScopeDispose
} from '@vue/reactivity'
export { computed } from './apiComputed'
export {
watch,
watchEffect,
Expand Down

0 comments on commit f4f0966

Please sign in to comment.