npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loopmode/cra-workbox-refresh

v0.0.5

Published

Helper for displaying a notification after service worker updates in a CRA2 app

Downloads

253

Readme

@loopmode/cra-workbox-refresh

Helper for create-react-app v2 apps that use the workbox service worker.
Displays a UI that informs the user about updates and recommends a page refresh.

Based on https://developers.google.com/web/tools/workbox/guides/advanced-recipes.

Note: Requires Node >8

Installation

Install this package as a dev dependency in your project:

yarn add --dev @loopmode/cra-workbox-refresh

Usage

Two things need to be done in your project:

  1. Inject code into generated service-worker.js after build
  2. Provide a callback when initializing workbox in your app

1. Inject code after build

In your package.json, change your build script to execute inject-workbox-refresh right after the build is created.

"scripts": {
    "build": "react-scripts build && inject-workbox-refresh"
}

It will inject a simple piece of code for handling a skipWaiting message in the generated service worker file:

// this is appended to build/service-worker.js after each build
self.addEventListener('message', function handleSkipWaiting(event) {
  if (event.data === 'skipWaiting') { self.skipWaiting(); }
});

2. Provide workbox callback

In your index.js file, where you initialize workbox, initialize the refresh mechanism as well.
Use the onUpdate callback of the register function and pass along the received registration object.

import * as serviceWorker from './serviceWorker';
import initWorkboxRefresh from '@loopmode/cra-workbox-refresh';

// ...

serviceWorker.register({
    onUpdate: registration => initWorkboxRefresh(registration)
});

Options

textContent

Provide a custom text for the button:

serviceWorker.register({
    onUpdate: registration => initWorkboxRefresh(registration, {
        textContent: 'Updates loaded. Click here to refresh'
    })
});

className

Provide a custom CSS class for the button:

serviceWorker.register({
    onUpdate: registration => initWorkboxRefresh(registration, {
        className: 'workbox-refresh'
    })
});

render

Provide a custom render function that controls the rendered UI entirely. It will be invoked when a new service worker becomes available and it will receive the registration object.
You can provide whatever UI you need, and handle whatever events you like, you just need to call registration.waiting.postMessage("skipWaiting") to force the refresh. A function that does exactly that is provided to you as 'refresh' in the options object.

Pass your custom render function:

serviceWorker.register({
    onUpdate: registration => initWorkboxRefresh(registration, { render: renderRefreshUI })
});

If you don't need any special handling, just use the received refresh function:

function renderRefreshUI(registration, { refresh }) {
    const el = document.createElement('div');
    document.body.appendChild(el);

    ReactDOM.render(<MyRefreshNotification onClick={refresh} />, document.getElementById(el));
}

If you want to handle the event manually, e.g. disable the clicked button or show a loading animation:

function renderRefreshUI(registration) {
    const el = document.createElement('div');
    document.body.appendChild(el);

    const handleClick = (event) => {
        // ensure registration.waiting is available
        if (registration.waiting) {
            // post the "skipWaiting" message
            registration.waiting.postMessage("skipWaiting");
            // do whatever you need to do
            event.target.disabled = true;
        }
    }
    ReactDOM.render(<MyRefreshNotification onClick={handleClick} />, el));
}