@@ -98,13 +98,22 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
9898 const removeTabIndex = tabs . value . findIndex ( tab => tab . id === tabId ) ;
9999 if ( removeTabIndex === - 1 ) return ;
100100
101+ const removedTabRouteKey = tabs . value [ removeTabIndex ] . routeKey ;
101102 const isRemoveActiveTab = activeTabId . value === tabId ;
102103 const nextTab = tabs . value [ removeTabIndex + 1 ] || homeTab . value ;
103104
105+ // remove tab
104106 tabs . value . splice ( removeTabIndex , 1 ) ;
107+
108+ // if current tab is removed, then switch to next tab
105109 if ( isRemoveActiveTab && nextTab ) {
106110 await switchRouteByTab ( nextTab ) ;
107111 }
112+
113+ // reset route cache if cache strategy is close
114+ if ( themeStore . resetCacheStrategy === 'close' ) {
115+ routeStore . resetRouteCache ( removedTabRouteKey ) ;
116+ }
108117 }
109118
110119 /** remove active tab */
@@ -131,9 +140,26 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
131140 */
132141 async function clearTabs ( excludes : string [ ] = [ ] ) {
133142 const remainTabIds = [ ...getFixedTabIds ( tabs . value ) , ...excludes ] ;
134- const removedTabsIds = tabs . value . map ( tab => tab . id ) . filter ( id => ! remainTabIds . includes ( id ) ) ;
143+
144+ // Identify tabs to be removed and collect their routeKeys if strategy is 'close'
145+ const tabsToRemove = tabs . value . filter ( tab => ! remainTabIds . includes ( tab . id ) ) ;
146+ const routeKeysToReset : RouteKey [ ] = [ ] ;
147+
148+ if ( themeStore . resetCacheStrategy === 'close' ) {
149+ for ( const tab of tabsToRemove ) {
150+ routeKeysToReset . push ( tab . routeKey ) ;
151+ }
152+ }
153+
154+ const removedTabsIds = tabsToRemove . map ( tab => tab . id ) ;
155+
156+ // If no tabs are actually being removed based on excludes and fixed tabs, exit
157+ if ( removedTabsIds . length === 0 ) {
158+ return ;
159+ }
135160
136161 const isRemoveActiveTab = removedTabsIds . includes ( activeTabId . value ) ;
162+ // filterTabsByIds returns tabs NOT in removedTabsIds, so these are the tabs that will remain
137163 const updatedTabs = filterTabsByIds ( removedTabsIds , tabs . value ) ;
138164
139165 function update ( ) {
@@ -142,13 +168,21 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
142168
143169 if ( ! isRemoveActiveTab ) {
144170 update ( ) ;
145- return ;
171+ } else {
172+ const activeTabCandidate = updatedTabs [ updatedTabs . length - 1 ] || homeTab . value ;
173+
174+ if ( activeTabCandidate ) {
175+ // Ensure there's a tab to switch to
176+ await switchRouteByTab ( activeTabCandidate ) ;
177+ }
178+ // Update the tabs array regardless of switch success or if a candidate was found
179+ update ( ) ;
146180 }
147181
148- const activeTab = updatedTabs [ updatedTabs . length - 1 ] || homeTab . value ;
149-
150- await switchRouteByTab ( activeTab ) ;
151- update ( ) ;
182+ // After tabs are updated and route potentially switched, reset cache for removed tabs
183+ for ( const routeKey of routeKeysToReset ) {
184+ routeStore . resetRouteCache ( routeKey ) ;
185+ }
152186 }
153187
154188 const { routerPushByKey } = useRouterPush ( ) ;
0 commit comments