Coder Social home page Coder Social logo

react-clear-browser-cache's Introduction

react-clear-browser-cache

Library for clearing browser cache after react app updates

NPM JavaScript Style Guide

Install

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.

{
  "prebuild": "npm run generate-build-meta",
  "generate-build-meta": "node ./node_modules/react-clear-browser-cache/bin/cli.js"
}

Usage

Using Boundary:

ClearBrowserCacheBoundary has context provider for child elements (useClearBrowserCache), add him after your ErrorBoundary, because ClearBrowserCacheBoundary only handles certain errors like ChunkLoadError.

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(
  <ErrorBoundary>
    <ClearBrowserCacheBoundary auto fallback='Loading' duration={60000}>
      <App />
    </ClearBrowserCacheBoundary>
  </ErrorBoundary>,
  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.

Using hook:

import React from 'react';

import { useClearBrowserCache } from 'react-clear-browser-cache';

function App() {
  const contextValue = useClearBrowserCache();

  return null;
}

Using render props:

import React from 'react';

import { ClearBrowserCache } from 'react-clear-browser-cache';

function App() {
  return <ClearBrowserCache>{(contextValue) => null}</ClearBrowserCache>;
}

Context value

Name Type More info
loading boolean Indicates request for a new version.
isLatestVersion boolean Indicates if the latest version of the app.
latestVersion string Latest version of the app.
disabled boolean Indicates if the boundary component is disabled, stopped all requests, doesn't handle errors, just render children because of internal errors.
clearCacheAndReload function Clear CacheStorage and reload the page.

License

MIT © arenuzzz

react-clear-browser-cache's People

Contributors

devmaxxx avatar maxsynefisco avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.