]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(ssr): make computed inactive during ssr, fix memory leak
authorEvan You <yyx990803@gmail.com>
Sun, 16 Jan 2022 10:22:18 +0000 (18:22 +0800)
committerEvan You <yyx990803@gmail.com>
Sun, 16 Jan 2022 10:22:18 +0000 (18:22 +0800)
fix #5208

packages/reactivity/src/computed.ts
packages/runtime-core/src/apiComputed.ts [new file with mode: 0644]
packages/runtime-core/src/index.ts

index c7f7b0515547038bb1204c0c41bb4e3022567b81..c077d84b209a61f3cde110b03c641137d769cc3c 100644 (file)
@@ -36,7 +36,8 @@ 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) {
@@ -44,6 +45,7 @@ class ComputedRefImpl<T> {
         triggerRefValue(this)
       }
     })
+    this.effect.active = !isSSR
     this[ReactiveFlags.IS_READONLY] = isReadonly
   }
 
@@ -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>
@@ -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
   }
diff --git a/packages/runtime-core/src/apiComputed.ts b/packages/runtime-core/src/apiComputed.ts
new file mode 100644 (file)
index 0000000..3804531
--- /dev/null
@@ -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
index 0d5e320e83f0df6f6c04366ba526bc0f17de6db2..65fc62b3e2f1f437da63665424759526dc9c61a6 100644 (file)
@@ -3,7 +3,6 @@
 export const version = __VERSION__
 export {
   // core
-  computed,
   reactive,
   ref,
   readonly,
@@ -34,6 +33,7 @@ export {
   getCurrentScope,
   onScopeDispose
 } from '@vue/reactivity'
+export { computed } from './apiComputed'
 export {
   watch,
   watchEffect,