@@ -40,12 +40,16 @@ export interface WritableComputedOptions<T, S = T> {
40
40
set : ComputedSetter < S >
41
41
}
42
42
43
+ let globalVersion = 0
44
+ let initSSR = false
45
+
43
46
/**
44
47
* @private exported by @vue/reactivity for Vue core use, but not exported from
45
48
* the main vue package
46
49
*/
47
50
export class ComputedRefImpl < T = any > implements IComputed {
48
51
_value : T | undefined = undefined
52
+ version = - 1
49
53
50
54
// Dependency
51
55
subs : Link | undefined = undefined
@@ -72,6 +76,10 @@ export class ComputedRefImpl<T = any> implements IComputed {
72
76
get effect ( ) : this {
73
77
return this
74
78
}
79
+ // for backwards compat
80
+ get dep ( ) : Dependency {
81
+ return this
82
+ }
75
83
// dev only
76
84
onTrack ?: ( event : DebuggerEvent ) => void
77
85
// dev only
@@ -95,17 +103,36 @@ export class ComputedRefImpl<T = any> implements IComputed {
95
103
}
96
104
97
105
get value ( ) : T {
98
- const dirtyLevel = this . dirtyLevel
99
- if ( dirtyLevel === ( 2 satisfies DirtyLevels . MaybeDirty ) ) {
100
- Subscriber . resolveMaybeDirty ( this )
101
- if ( this . dirtyLevel === ( 3 satisfies DirtyLevels . Dirty ) ) {
106
+ // In SSR there will be no render effect, so the computed has no subscriber
107
+ // and therefore tracks no deps, thus we cannot rely on the dirty check.
108
+ // Instead, computed always re-evaluate and relies on the globalVersion
109
+ // fast path above for caching.
110
+ if ( this . isSSR ) {
111
+ if ( ! initSSR ) {
112
+ initSSR = true
113
+ const propagate = Dependency . propagate
114
+ Dependency . propagate = ( link : Link ) => {
115
+ globalVersion ++
116
+ propagate ( link )
117
+ }
118
+ }
119
+ if ( globalVersion !== this . version ) {
120
+ this . version = globalVersion
121
+ this . update ( )
122
+ }
123
+ } else {
124
+ const dirtyLevel = this . dirtyLevel
125
+ if ( dirtyLevel === ( 2 satisfies DirtyLevels . MaybeDirty ) ) {
126
+ Subscriber . resolveMaybeDirty ( this )
127
+ if ( this . dirtyLevel === ( 3 satisfies DirtyLevels . Dirty ) ) {
128
+ this . update ( )
129
+ }
130
+ } else if (
131
+ dirtyLevel === ( 3 satisfies DirtyLevels . Dirty ) ||
132
+ dirtyLevel === ( 4 satisfies DirtyLevels . Released )
133
+ ) {
102
134
this . update ( )
103
135
}
104
- } else if (
105
- dirtyLevel === ( 3 satisfies DirtyLevels . Dirty ) ||
106
- dirtyLevel === ( 4 satisfies DirtyLevels . Released )
107
- ) {
108
- this . update ( )
109
136
}
110
137
const activeTrackId = System . activeTrackId
111
138
if ( activeTrackId !== 0 && this . subsTail ?. trackId !== activeTrackId ) {
0 commit comments