๐ก๏ธ Clear browser caches from client-side with ease. Service Workers, Storage, IndexedDB, Cookies & more.
npm install cache-shield-sdk> Clear browser caches from client-side with ease. Service Workers, Storage, IndexedDB, Cookies & more.



- ๐งน Clear All Cache Types - Service Workers, Cache API, localStorage, sessionStorage, IndexedDB, Cookies
- โก Lightweight - ~4KB gzipped, zero dependencies
- ๐ฏ Selective Clearing - Target specific caches, preserve essential data
- ๐ก๏ธ Mobile Optimized - BfCache protection & Smart Reloading for iOS/Android
- ๐ Framework Plugins - React, Vue, Svelte, Preact
- ๐ฆ TypeScript - Full type definitions included
- ๐ Universal - Works in any browser environment
``bash`
npm install cache-shield-sdkor
yarn add cache-shield-sdkor
pnpm add cache-shield-sdk
Check out the examples directory for complete working demos:
- Basic - Vanilla JS implementation
- React - React Hook & Component usage
- Vue 3 - Vue Composable & Plugin usage
`javascript
import CacheShield from 'cache-shield-sdk';
// Create instance
const shield = new CacheShield();
// Clear all caches
await shield.clear();
// Clear specific types
await shield.clearServiceWorkers();
await shield.clearLocalStorage();
await shield.clearCookies();
`
`javascript
import { clearCache } from 'cache-shield-sdk';
await clearCache(); // Clears everything!
`
`javascript
const shield = new CacheShield({
targets: ['localStorage', 'sessionStorage', 'cookies'],
cookies: {
preserveEssential: true,
essentialCookies: ['auth_token', 'csrf']
},
storage: {
preserveKeys: ['user_preferences']
},
debug: true,
autoReload: true
});
const result = await shield.clear();
console.log(Cleared ${result.cleared.length} cache types);`
`tsx
import { CacheShieldProvider, useCacheShield, ClearCacheButton } from 'cache-shield-sdk/react';
// Wrap your app
function App() {
return (
);
}
// Use the hook
function MyComponent() {
const { clear, isClearing, lastResult } = useCacheShield();
return (
๐ Vue 3
`typescript
// main.ts
import { createApp } from 'vue';
import { CacheShieldPlugin } from 'cache-shield-sdk/vue';const app = createApp(App);
app.use(CacheShieldPlugin, { debug: true });
app.mount('#app');
// Component
`๐งก Svelte
`typescript
// App.svelte
// Component.svelte
`โ๏ธ Preact
`tsx
import { CacheShieldProvider, useCacheShield } from 'cache-shield-sdk/preact';function App() {
return (
);
}
`๐ CDN / Browser
`html
`๐ API Reference
$3
#### Constructor Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
|
targets | CacheType[] | ['all'] | Cache types to clear |
| include | (string \| RegExp)[] | [] | Only clear matching patterns |
| exclude | (string \| RegExp)[] | [] | Skip matching patterns |
| cookies | CookieOptions | {} | Cookie-specific options |
| storage | StorageOptions | {} | Storage-specific options |
| indexedDB | IndexedDBOptions | {} | IndexedDB-specific options |
| debug | boolean | false | Enable debug logging |
| autoReload | boolean | false | Reload after clearing |
| preventBfCache | boolean | false | Force reload on back button (iOS fix) |
| hooks | CacheShieldHooks | {} | Lifecycle callbacks |#### Methods
| Method | Returns | Description |
|--------|---------|-------------|
|
clear(options?) | Promise | Clear all targeted caches |
| clearServiceWorkers() | Promise | Clear only Service Workers |
| clearCacheAPI() | Promise | Clear only Cache API |
| clearLocalStorage() | Promise | Clear only localStorage |
| clearSessionStorage() | Promise | Clear only sessionStorage |
| clearIndexedDB() | Promise | Clear only IndexedDB |
| clearCookies() | Promise | Clear only cookies |
| hardReload() | void | Force reload bypassing cache |
| clearAndReload(options?) | Promise | Clear then reload |
| getStorageEstimate() | Promise | Get storage usage |
| getCapabilities() | Capabilities | Check browser support |$3
`typescript
type CacheType =
| 'all'
| 'serviceWorker'
| 'cacheAPI'
| 'localStorage'
| 'sessionStorage'
| 'indexedDB'
| 'cookies';interface ClearResult {
success: boolean;
cleared: CacheTypeResult[];
failed: CacheTypeResult[];
timestamp: number;
duration: number;
}
`๐ฏ Common Use Cases
$3
`javascript
const shield = new CacheShield({
targets: ['serviceWorker', 'cacheAPI'],
hooks: {
onAfterClear: () => {
// Show "New version available" toast
showUpdateNotification();
}
}
});// When user clicks "Update"
await shield.clearAndReload();
`$3
`javascript
async function logout() {
await shield.clear({
targets: ['localStorage', 'sessionStorage', 'cookies', 'indexedDB'],
cookies: {
preserveEssential: false // Clear everything including auth
}
});
window.location.href = '/login';
}
`$3
`javascript
const shield = new CacheShield({
debug: true,
hooks: {
onProgress: ({ current, percentage }) => {
console.log(Clearing ${current}: ${percentage}%);
}
}
});
``| Feature | Chrome | Firefox | Safari | Edge |
|---------|--------|---------|--------|------|
| Service Workers | โ
40+ | โ
44+ | โ
11.1+ | โ
17+ |
| Cache API | โ
40+ | โ
39+ | โ
11.1+ | โ
16+ |
| localStorage | โ
All | โ
All | โ
All | โ
All |
| IndexedDB | โ
23+ | โ
10+ | โ
10+ | โ
12+ |
MIT Suneel Kumar