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

sw-broadcast-cache-update

v0.0.23

Published

A helper library that uses the Broadcast Channel API to announce when two Response objects differ.

Downloads

49

Readme

sw-broadcast-cache-update

A helper library that uses the Broadcast Channel API to announce when two Response objects differ.

Installation

npm install --save-dev sw-broadcast-cache-update

Demo

Browse sample source code in the demo directory, or try it out directly.

API

goog.broadcastCacheUpdate

packages/sw-broadcast-cache-update/src/index.js:24-29

Behavior

packages/sw-broadcast-cache-update/src/lib/behavior.js:66-155

Examples

// Used as an automatically invoked as "behavior" by a RequestWrapper:

const requestWrapper = new goog.runtimeCaching.RequestWrapper({
  cacheName: 'runtime-cache',
  behaviors: [
    new goog.broadcastCacheUpdate.Behavior({channelName: 'cache-updates'})
  ]
});

// Set up a route to match any requests made against the example.com domain.
// The requests will be handled with a stale-while-revalidate policy, and the
// cache update notification behavior, as configured in requestWrapper, will
// be automatically applied.
const route = new goog.routing.RegExpRoute({
  match: ({url}) => url.domain === 'example.com',
  handler: new goog.runtimeCaching.StaleWhileRevalidate({requestWrapper})
});
// Explicitly invoked usage independent of the goog.routing framework, via
// the notifyIfUpdated() method:

const cacheUpdateBehavior = new goog.broadcastCacheUpdates.Behavior({
  channelName: 'cache-updates'
});

const url = 'https://example.com';
const cacheName = 'runtime-cache';

const cache = await caches.open(cacheName);
const oldResponse = await cache.match(url);
const newResponse = await fetch(url);
await cache.put(url, newResponse);

// Only check for an update if there was a previously cached Response.
if (oldResponse) {
  cacheUpdateBehavior.notifyIfUpdated({
    first: oldResponse,
    second: newResponse,
    cacheName
  });
}

constructor

packages/sw-broadcast-cache-update/src/lib/behavior.js:86-92

Creates a new Behavior instance, which is used to compare two Responses and use the Broadcast Channel API to notify interested parties when those Responses differ.

For efficiency's sake, the underlying response bodies are not compared; only specific response headers are checked.

Parameters

  • $0 Object
    • $0.channelName string The name that will be used when creating the BroadcastChannel.
    • $0.headersToCheck [Array<string>] A list of headers that will be used to determine whether the Responses differ. If not provided, the values ['content-length', 'etag', 'last-modified'] are used.
    • $0.source [string] An attribution value that will be used in the broadcast message to indicate where the update originated. If not provided, a default value will be used.

notifyIfUpdated

packages/sw-broadcast-cache-update/src/lib/behavior.js:146-154

An explicit method to call from your own code to trigger the comparison of two Response and fire off a notification via the Broadcast Channel API if they differ.

Parameters

broadcastUpdate

packages/sw-broadcast-cache-update/src/lib/broadcast-update.js:51-65

Uses the Broadcast Channel API to notify interested subscribers about a change to a cached resource.

You would not normally call this method directly; it's called automatically by an instance of the Behavior class. It's exposed here for the benefit of developers who would rather not use the full Behavior implementation.

The message that's posted takes the following format, inspired by the Flux standard action format. (Usage of Flux itself is not at all required.)

{
  type: 'CACHE_UPDATED',
  meta: 'sw-broadcast-cache-update',
  payload: {
    cacheName: 'the-cache-name',
    updatedUrl: 'https://example.com/'
  }
}

Parameters

  • $0 Object
    • $0.channel BroadcastChannel The BroadcastChannel to use.
    • $0.cacheName string The name of the cache in which the updated Response was stored.
    • $0.url string The URL associated with the updated Response.
    • $0.source string A string identifying this library as the source of the update message.

cacheUpdatedMessageType

packages/sw-broadcast-cache-update/src/lib/constants.js:21-21

responsesAreSame

packages/sw-broadcast-cache-update/src/lib/responses-are-same.js:29-38

Given two Responses, compares several header values to see if they are the same or not.

Parameters

  • $0 Object
    • $0.first Response One of the Responses.
    • $0.second Response Another of the Responses.
    • $0.headersToCheck Array<string> A list of headers that will be used to determine whether the Responses differ.

Returns boolean Whether or not the Response objects are assumed to be the same.