Socket
Socket
Sign inDemoInstall

react-clear-cache-v2

Package Overview
Dependencies
10
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-clear-cache-v2

A component to manage application updates with an enhancement to the [react-clear-cache](https://www.npmjs.com/package/react-clear-cache) package.


Version published
Weekly downloads
3
increased by50%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

react-clear-cache-v2

A component to manage application updates with an enhancement to the react-clear-cache package.

NPM JavaScript Style Guide

Demo

  • Fetched on window focus
  • Stop fetching on window blur

Demo

See demo

Install

npm install react-clear-cache-v2
yarn add react-clear-cache-v2

Add script in package.json

This will generate meta.json file. This will have the version key with the latest build.

{
  "prebuild": "react-clear-cache-v2 --destination=<path of your dist folder>"
}

Usage

Using Context API:

import * as React from 'react';

import { ClearCacheProvider, useClearCacheCtx } from 'react-clear-cache-v2';

const App: React.FC = () => {
  const { isLatestVersion, emptyCacheStorage } = useClearCacheCtx();
  return (
    <div>
      {!isLatestVersion && (
        <p>
          <a
            href="#"
            onClick={(e) => {
              e.preventDefault();
              emptyCacheStorage();
            }}
          >
            Update version
          </a>
        </p>
      )}
    </div>
  );
};

ReactDOM.render(
  <ClearCacheProvider duration={5000}>
    <App />
  </ClearCacheProvider>,
  document.getElementById('root')
);

Using hooks:

import * as React from 'react';

import { useClearCache } from 'react-clear-cache-v2';

const App: React.FC<{}> = () => {
  const { isLatestVersion, emptyCacheStorage } = useClearCache();
  return (
    <div>
      {!isLatestVersion && (
        <p>
          <a
            href="#"
            onClick={(e) => {
              e.preventDefault();
              emptyCacheStorage();
            }}
          >
            Update version
          </a>
        </p>
      )}
    </div>
  );
};

export default App;

Props

duration: number

You can set the duration (ms) when to fetch for new updates.

auto: boolean

Set to true to auto-reload the page whenever an update is available.

Render props

loading: boolean

A boolean that indicates whether the request is in flight

isLatestVersion: boolean

A boolean that indicates if the user has the latest version.

emptyCacheStorage: () => void

This function empty the CacheStorage and reloads the page.

Contributors

  1. noahjohn9259

License

MIT © noahjohn9259

Development

  1. In package.json, set main to src/index.js.

  2. Run npm start in root directory. It will build and watch if there are changes made.

  3. cd example and run npm start. It will run the demo application.

Note

If you are done making changes, reset main to dist/index.js in package.json.

FAQs

Last updated on 24 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc