Library for clearing browser cache after react app updates
npm install react-clear-browser-cachebash
npm install --save react-clear-browser-cache
or
yarn add react-clear-browser-cache
`
Add script in package.json
Generate meta.json file with latest build version.
`json
{
"prebuild": "npm run generate-build-meta",
"generate-build-meta": "node ./node_modules/react-clear-browser-cache/bin/cli.js"
}
`
Usage
$3
ClearBrowserCacheBoundary has context provider for child elements (useClearBrowserCache), add him after your ErrorBoundary, because ClearBrowserCacheBoundary only handles certain errors like ChunkLoadError.
`tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { ClearBrowserCacheBoundary } from 'react-clear-browser-cache';
import App from './App';
import ErrorBoundary from './ErrorBoundary';
ReactDOM.render(
,
document.getElementById('root')
);
`
Props
| Name | Type | Default | More info |
| :------------ | :------- | :----------- | :---------------------------------------------------------------------------------------------------------------- |
| duration | number | | You can set the duration when to fetch for new updates. |
| auto | boolean | false | Set to true to auto-reload the page whenever an update is available. |
| fallback | any | null | You can set fallback data when fetching new version - preloader etc. |
| storageKey | string | APP_VERSION | Storage key for saving app version. |
| storage | object | localStorage | You can use another storage to save and get the version of app like store. |
| filename | string | meta.json | Filename for fetching new app version. |
| errorCheckers | array | [] | You can set custom error checkers for catching errors concerned with invalid browser caches. |
| debug | function | | You can debug state and errors. |
$3
`tsx
import React from 'react';
import { useClearBrowserCache } from 'react-clear-browser-cache';
function App() {
const contextValue = useClearBrowserCache();
return null;
}
`
$3
`tsx
import React from 'react';
import { ClearBrowserCache } from 'react-clear-browser-cache';
function App() {
return {(contextValue) => null} ;
}
``