本文目录导读:

浏览器端自动清除脚本
方案A:通过 localStorage 清除
// 在 docsify 配置中添加
window.$docsify = {
// ... 其他配置
plugins: [
function(hook, vm) {
hook.init(function() {
// 清除所有 docsify 相关的 localStorage
Object.keys(localStorage).forEach(key => {
if (key.startsWith('docsify')) {
localStorage.removeItem(key);
}
});
console.log('✅ Docsify 缓存已清理');
});
}
]
};
方案B:带版本控制的清除
// 在 index.html 中添加
<script>
const CACHE_VERSION = '1.0.1'; // 修改此值触发缓存清除
(function() {
const storedVersion = localStorage.getItem('docsify_cache_version');
if (storedVersion !== CACHE_VERSION) {
// 清除旧缓存
Object.keys(localStorage).forEach(key => {
if (key.startsWith('docsify') || key.startsWith('local_')) {
localStorage.removeItem(key);
}
});
localStorage.setItem('docsify_cache_version', CACHE_VERSION);
console.log('🔄 缓存已更新至版本:', CACHE_VERSION);
}
})();
</script>
Service Worker 自动清理
// sw.js
self.addEventListener('activate', event => {
const cacheWhitelist = ['docsify-core'];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
// 清理不在白名单中的缓存
if (cacheName.startsWith('docsify') && !cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
);
});
// 定时清理(每24小时)
self.addEventListener('message', event => {
if (event.data === 'CLEAR_CACHE') {
caches.keys().then(cacheNames => {
cacheNames.forEach(cacheName => {
if (cacheName.startsWith('docsify')) {
caches.delete(cacheName);
}
});
});
}
});
构建时自动清理脚本
Node.js 脚本
// clean-docsify-cache.js
const fs = require('fs-extra');
const path = require('path');
// 要清理的目录
const cacheDirs = [
path.join(process.cwd(), '.docsify'),
path.join(process.cwd(), 'node_modules/.cache/docsify')
];
async function cleanCache() {
for (const dir of cacheDirs) {
try {
if (await fs.pathExists(dir)) {
await fs.remove(dir);
console.log(`✅ 已清理: ${dir}`);
}
} catch (error) {
console.error(`❌ 清理失败: ${dir}`, error.message);
}
}
}
// 添加到 package.json
// "scripts": {
// "clean": "node clean-docsify-cache.js",
// "build": "npm run clean && docsify build"
// }
cleanCache();
自动检测并清理脚本
// auto-clear-docsify-cache.js
(function() {
'use strict';
// 配置
const CONFIG = {
maxCacheSize: 5 * 1024 * 1024, // 5MB
cacheDuration: 24 * 60 * 60 * 1000, // 24小时
checkInterval: 60 * 60 * 1000 // 每小时检查一次
};
function getCacheSize() {
let totalSize = 0;
Object.keys(localStorage).forEach(key => {
if (key.startsWith('docsify')) {
totalSize += localStorage.getItem(key).length * 2; // UTF-16
}
});
return totalSize;
}
function clearExpiredCache() {
const now = Date.now();
Object.keys(localStorage).forEach(key => {
if (key.startsWith('docsify')) {
try {
const item = JSON.parse(localStorage.getItem(key));
if (item && item.timestamp && (now - item.timestamp > CONFIG.cacheDuration)) {
localStorage.removeItem(key);
console.log(`🗑️ 已清理过期缓存: ${key}`);
}
} catch (e) {
// 解析失败则删除
localStorage.removeItem(key);
}
}
});
}
// 自动清理逻辑
setInterval(() => {
clearExpiredCache();
const cacheSize = getCacheSize();
if (cacheSize > CONFIG.maxCacheSize) {
console.warn(`⚠️ 缓存超过 ${CONFIG.maxCacheSize},执行强制清理`);
Object.keys(localStorage).forEach(key => {
if (key.startsWith('docsify')) {
localStorage.removeItem(key);
}
});
}
}, CONFIG.checkInterval);
// 页面加载时立即执行一次
clearExpiredCache();
console.log('🔍 Docsify 缓存自动清理已启动');
})();
一键清理书签脚本
// 保存为书签脚本(bookmarklet)
javascript:(function(){
var keys = Object.keys(localStorage);
var count = 0;
keys.forEach(function(key){
if(key.startsWith('docsify')){
localStorage.removeItem(key);
count++;
}
});
alert('已清理 ' + count + ' 个 docsify 缓存项');
})();
使用建议
- 开发环境:使用方案3(构建时自动清理)
- 生产环境:使用方案1或2(版本控制 + Service Worker)
- 用户端:推荐方案4(自动检测清理)
选择适合你项目的方案即可。