ant-design / ant-design/pro-components

🐛[BUG]proLayout swr缓存管理错误,不应该调用cache.clear()直接把应用的SWR缓存清空掉了,应改为cache.delete。

Open
#7,670 4 comments 0 reactions 0 assignees View on GitHub
👏🏻 Welcome pr
Dominant language
TypeScript
Stars
4.8k
Forks
1.4k
Avg merge
10h 44m
Merged PRs (30d)
3

Description

提问前先看看:

### 🐛 bug 描述

proLayout组件在组件销毁的时候,把组件下的所有SWR缓存都清空掉了,导致不同layout原本要共享的SWR缓存被清空掉了。

### 📷 复现步骤

页面A使用layoutA,页面B使用LayoutB,并且两个页面逻辑上都使用SWR作为请求用户信息。因为proLayout使用了在组件销毁时的是cache.clear(),这样从页面A切换到页面B,缓存就被清空掉,用户信息的缓存就用不上了。

### 🏞 期望结果

仅删除的proLayout内部定义的【defaultId】缓存即可,不删除用户的其他SWR缓存。

### 💻 复现代码

https://github.com/ant-design/pro-components/blob/ef454041a54fe7ccf28bf9e493564522e8895fd9/packages/layout/src/ProLayout.tsx#L505C21-L505C21

useEffect(() => {
return () => {
// 将 cache.clear() 改成 cache.delete(defaultId) 即可
if (cache instanceof Map) cache.clear();
};
}, []);

### © 版本信息

- @ant-design/pro-layout 版本: [7.16.13]

Contributor guide

Open the contributing guide

Research direction

Open packages/layout/src/ProLayout.tsx around line 505 and inspect the cleanup function in the useEffect. Confirm that the cleanup removes only the layout's defaultId cache entry rather than clearing shared SWR cache entries; done means switching between layouts preserves unrelated user-information cache data.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.