@@ -12,11 +12,14 @@ import {
1212 type RootNode ,
1313 type SimpleExpressionNode ,
1414 type SlotFunctionExpression ,
15+ type SlotsObjectProperty ,
1516 type TemplateChildNode ,
1617 type TemplateNode ,
1718 type TextCallNode ,
1819 type VNodeCall ,
1920 createArrayExpression ,
21+ createObjectProperty ,
22+ createSimpleExpression ,
2023 getVNodeBlockHelper ,
2124 getVNodeHelper ,
2225} from '../ast'
@@ -140,6 +143,7 @@ function walk(
140143 }
141144
142145 let cachedAsArray = false
146+ const slotCacheKeys = [ ]
143147 if ( toCache . length === children . length && node . type === NodeTypes . ELEMENT ) {
144148 if (
145149 node . tagType === ElementTypes . ELEMENT &&
@@ -163,6 +167,7 @@ function walk(
163167 // default slot
164168 const slot = getSlotNode ( node . codegenNode , 'default' )
165169 if ( slot ) {
170+ slotCacheKeys . push ( context . cached . length )
166171 slot . returns = getCacheExpression (
167172 createArrayExpression ( slot . returns as TemplateChildNode [ ] ) ,
168173 )
@@ -186,6 +191,7 @@ function walk(
186191 slotName . arg &&
187192 getSlotNode ( parent . codegenNode , slotName . arg )
188193 if ( slot ) {
194+ slotCacheKeys . push ( context . cached . length )
189195 slot . returns = getCacheExpression (
190196 createArrayExpression ( slot . returns as TemplateChildNode [ ] ) ,
191197 )
@@ -196,10 +202,31 @@ function walk(
196202
197203 if ( ! cachedAsArray ) {
198204 for ( const child of toCache ) {
205+ slotCacheKeys . push ( context . cached . length )
199206 child . codegenNode = context . cache ( child . codegenNode ! )
200207 }
201208 }
202209
210+ // put the slot cached keys on the slot object, so that the cache
211+ // can be removed when component unmounting to prevent memory leaks
212+ if (
213+ slotCacheKeys . length &&
214+ node . type === NodeTypes . ELEMENT &&
215+ node . tagType === ElementTypes . COMPONENT &&
216+ node . codegenNode &&
217+ node . codegenNode . type === NodeTypes . VNODE_CALL &&
218+ node . codegenNode . children &&
219+ ! isArray ( node . codegenNode . children ) &&
220+ node . codegenNode . children . type === NodeTypes . JS_OBJECT_EXPRESSION
221+ ) {
222+ node . codegenNode . children . properties . push (
223+ createObjectProperty (
224+ `__` ,
225+ createSimpleExpression ( JSON . stringify ( slotCacheKeys ) , false ) ,
226+ ) as SlotsObjectProperty ,
227+ )
228+ }
229+
203230 function getCacheExpression ( value : JSChildNode ) : CacheExpression {
204231 const exp = context . cache ( value )
205232 // #6978, #7138, #7114
0 commit comments