GithubHelp home page GithubHelp logo

rubenbupe / react-update-notification Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iamkd/react-update-notification

0.0 0.0 0.0 228 KB

Home Page: https://www.npmjs.com/package/react-update-notification

JavaScript 39.72% TypeScript 37.13% Dockerfile 23.15%

react-update-notification's Introduction

react-update-notification · npm version

A small CLI tool & React hook to check when your app has a new version and show a notification. Works great when you cannot or do not want to set up a service worker.

Installation

yarn add react-update-notification

or

npm i -S react-update-notification

Usage

This tool works in two steps:

  1. Injecting current version number into window object in index.html and creating a version.json file to check for a new version.
  2. Providing a React hook to build a custom update notification component.

1. Adding CLI command after build

In your package.json, add call to generate-version after the build is created.

{
  "scripts": {
    "build": "react-scripts build && generate-version -s package"
  }
}

CLI Options

Option Description Default
-s, --strategy Set strategy. The update notification will trigger when the version string in the JSON file is different from the version string in the HTML file.

latest-commit uses current commit hash.
package uses package.json version field.
latest-commit
-b, --buildPath Set custom build path. This should be the root of the public directory that is served. build
-i, --indexFile Path to index.html relative to build path. index.html
-v, --versionFile Version file target path relative to build path. version.json
-p, --versionFilePathPrefix A prefix to add before the versionFile option above (useful for more control over the deployed version file target path e.g. when using subdirectories to host your React app).

2. Using a React hook

The useUpdateCheck hook returns:

  • status string that can be 'checking', 'current' or 'available'.
  • reloadPage helper function that reloads the page.
  • checkUpdate function to manually trigger the update check.

It supports three check types:

  • interval checks on component mount and then every set period defined by interval property (10 seconds by default).
  • mount checks only on component mount.
  • manual does not check at all. It is used to check with checkUpdate.
import React from 'react';
import { useUpdateCheck } from 'react-update-notification';

const NotificationContainer = () => {
  const { status, reloadPage } = useUpdateCheck({
    type: 'interval',
    interval: 10000,
  });

  if (status === 'checking' || status === 'current') {
    return null;
  }

  return (
    <div>
      <button type="button" onClick={reloadPage}>
        Refresh to update the app
      </button>
    </div>
  );
};

react-update-notification's People

Contributors

iamkd avatar dependabot[bot] 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.