Skip to content

Commit 59fb213

Browse files
committed
优化页面缓存逻辑
1 parent d4ca52e commit 59fb213

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

src/router/guards.ts

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -160,54 +160,49 @@ function setupTitle(router: Router) {
160160

161161
// 页面缓存
162162
function setupKeepAlive(router: Router) {
163-
router.afterEach((to, from) => {
163+
router.afterEach(async (to, from) => {
164164
const keepAliveStore = useKeepAliveStore()
165-
// 判断当前页面是否开启缓存,如果开启,则将当前页面的 name 信息存入 keep-alive 全局状态
166-
if (to.meta.cache) {
167-
const componentName = to.matched.at(-1)?.components?.default.name
168-
if (componentName) {
169-
keepAliveStore.add(componentName)
170-
}
171-
else {
172-
// turbo-console-disable-next-line
173-
console.warn('[Fantastic-admin] 该页面组件未设置组件名,会导致缓存失效,请检查')
174-
}
175-
}
176-
// 判断离开页面是否开启缓存,如果开启,则根据缓存规则判断是否需要清空 keep-alive 全局状态里离开页面的 name 信息
177-
if (from.meta.cache) {
178-
const componentName = from.matched.at(-1)?.components?.default.name
179-
if (componentName) {
180-
// 通过 meta.cache 判断针对哪些页面进行缓存
181-
switch (typeof from.meta.cache) {
182-
case 'string':
183-
if (from.meta.cache !== to.name) {
184-
keepAliveStore.remove(componentName)
165+
if (to.fullPath !== from.fullPath) {
166+
if (to.meta.cache) {
167+
const componentName = to.matched.at(-1)?.components?.default.name
168+
if (componentName) {
169+
// 缓存当前页面前,先判断是否需要进行清除缓存,判断依据:
170+
// 1. 如果 to.meta.cache 为 boolean 类型,并且不为 true,则需要清除缓存
171+
// 2. 如果 to.meta.cache 为 string 类型,并且与 from.name 不一致,则需要清除缓存
172+
// 3. 如果 to.meta.cache 为 array 类型,并且不包含 from.name,则需要清除缓存
173+
// 4. 如果 to.meta.noCache 为 string 类型,并且与 from.name 一致,则需要清除缓存
174+
// 5. 如果 to.meta.noCache 为 array 类型,并且包含 from.name,则需要清除缓存
175+
// 6. 如果是刷新页面,则需要清除缓存
176+
let shouldClearCache = false
177+
if (typeof to.meta.cache === 'boolean') {
178+
shouldClearCache = !to.meta.cache
179+
}
180+
else if (typeof to.meta.cache === 'string') {
181+
shouldClearCache = to.meta.cache !== from.name
182+
}
183+
else if (Array.isArray(to.meta.cache)) {
184+
shouldClearCache = !to.meta.cache.includes(from.name as string)
185+
}
186+
if (to.meta.noCache) {
187+
if (typeof to.meta.noCache === 'string') {
188+
shouldClearCache = to.meta.noCache === from.name
185189
}
186-
break
187-
case 'object':
188-
if (!from.meta.cache.includes(to.name as string)) {
189-
keepAliveStore.remove(componentName)
190+
else if (Array.isArray(to.meta.noCache)) {
191+
shouldClearCache = to.meta.noCache.includes(from.name as string)
190192
}
191-
break
192-
}
193-
// 通过 meta.noCache 判断针对哪些页面不需要进行缓存
194-
if (from.meta.noCache) {
195-
switch (typeof from.meta.noCache) {
196-
case 'string':
197-
if (from.meta.noCache === to.name) {
198-
keepAliveStore.remove(componentName)
199-
}
200-
break
201-
case 'object':
202-
if (from.meta.noCache.includes(to.name as string)) {
203-
keepAliveStore.remove(componentName)
204-
}
205-
break
206193
}
194+
if (from.name === 'reload') {
195+
shouldClearCache = true
196+
}
197+
if (shouldClearCache) {
198+
keepAliveStore.remove(componentName)
199+
await nextTick()
200+
}
201+
keepAliveStore.add(componentName)
207202
}
208-
// 如果进入的是 reload 页面,则也将离开页面的缓存清空
209-
if (to.name === 'reload') {
210-
keepAliveStore.remove(componentName)
203+
else {
204+
// turbo-console-disable-next-line
205+
console.warn('[Fantastic-admin] 该页面组件未设置组件名,会导致缓存失效,请检查')
211206
}
212207
}
213208
}

0 commit comments

Comments
 (0)