Comments (11)
自动删除某些缓存可能不会是一个可预期的行为
我可以尝试增加 getCachingNodes 方法,返回缓存中的组件实例
实例上增加 createTime 和 updateTime 属性,分别表示缓存节点的创建时间和缓存状态变化时间
这样可以由使用者自己来主动拓展出上述功能,也不会影响 cache-route 本身
如何呀?
from react-router-cache-route.
v1.7.0 上增加了 getCachingComponents 功能,将返回一个 cachingComponentMap 对象结构,以 cacheKey 为键,以 CacheComponent 实例为值
值上将附带 __cacheCreateTime 及 __cacheUpdateTime 属性,分别表示缓存节点的创建时间和缓存状态变化时间
看看是否能满足功能,暂不写进 README 中
from react-router-cache-route.
可以,非常感谢!只要能识别时间先后的顺序就行。
from react-router-cache-route.
ok,一会儿我试试,好了通知
from react-router-cache-route.
@CJY0208 更新了看了下,确实有了,感谢!另外如果方便的话可以提供个回调函数吗,在缓存一个新页面后调用回调,这样会更方便。
from react-router-cache-route.
这个功能跟着 react-router 本身的 history 走就行了,用 history.listen 增加监听
cache-route 缓存状态的变化必然是因为 router 发生变化引起的
from react-router-cache-route.
OK,完美解决,非常感谢!
from react-router-cache-route.
@CJY0208 不知道是不是我写的有问题,我测试了下dropByCacheKey
好像没反应,
代码如下:
const MAX_NUMBER_OF_CACHES = 2;
const caches = getCachingComponents();
const cacheKeys = Object.keys(caches);
console.log(caches);
if (cacheKeys.length > MAX_NUMBER_OF_CACHES) {
// 找到最早更新缓存的组件,清除缓存
const timeCompare = (a, b) => caches[a].__cacheUpdateTime < caches[b].__cacheUpdateTime;
const oldestCacheKey = cacheKeys.reduce((resultKey, currentKey) =>
(!resultKey || timeCompare(currentKey, resultKey)) ? currentKey : resultKey, '');
dropByCacheKey(oldestCacheKey);
console.log('drop cache: ', oldestCacheKey);
}
这里为了测试,把最大缓存数量设置为两个,大概的逻辑是大于两个就清除最早更新的缓存,但是进入下一个页面时清除的那个还在。
from react-router-cache-route.
看起来似乎尝试清除的缓存节点是刚被新建的那个?例如正要去往 /user-center 而尝试清除 /user-center
可能由于时序原因,在 history.listen 的瞬间新建的节点处于缓存状态所以被列在了 cachingComponents 中,但其实它是即将被激活的路由,所以可能是清不掉的
from react-router-cache-route.
对,确实是这样,对比了一下我发现 __cacheUpdateTime
好像不太对劲,有的后面进入的页面的缓存更新时间反而比前面进入的更早
浏览路径为:/message
> /user-center
> /services
控制台输出:
from react-router-cache-route.
缓存更新时间貌似有点奇怪。
浏览路径还是:/message
> /user-center
> /services
控制台时间格式化输出:
from react-router-cache-route.
Related Issues (20)
- 嵌套路由页面不展示 HOT 8
- props.match not available while cached HOT 1
- CacheRoute pathname prop is gone HOT 5
- push和replace时不使用已经缓存的页面 HOT 2
- 记录启用 multiple 时 href 中有中文的一个 BUG 及解决方案 HOT 2
- 用react 17 的ReactDOM.render(), 缓存成功, 但不支持react 18的版本 ReactDOM.createRoot() 缓存失败,请问有兼容方案? HOT 6
- 带参数详情页面多份数据缓存如何做? HOT 4
- 带有参数的页面,离开页面的时候useParams会返回undefined,导致无法维持原来的页面组件状态 HOT 3
- 请问下作者如何处理跳转路径完全相同,但是state不同的情况 HOT 2
- 路由里存在中文,路由会缓存2份
- 开启multiple后,带querystring的路由缓存失败
- 请问 在cache-route中如何实现仅在切换路由列表时卸载当前页面组件 谢谢 HOT 1
- `CacheSwitch` re-mount the same component if path changes HOT 1
- item/detail/:id 这种带参数的路由缓存不了,
- 在路由back后,怎么让缓存的组件不重新render呢? HOT 1
- 怎么实现头部Tabs页签的效果?能否提供一些示例? HOT 1
- How to turn off the display:none of the component HOT 5
- 使用CacheRoute缓存路由,出现闪烁
- 【咨询】为什么 Updatable 内部的 Freeze 需要 Delay 1s HOT 2
- CacheSwitch不会触发Suspense的fallback HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-router-cache-route.