Skip to content

Instantly share code, notes, and snippets.


Jeffrey Posnick jeffposnick

View GitHub Profile
jeffposnick / package.json
Created Mar 24, 2021
workbox-webpack-plugin example
View package.json
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack"
"keywords": [],
"author": "",
jeffposnick / fallback-on-error-plugin.js
Last active Jan 4, 2021
Example Workbox plugin that fallsback to a cached HTML page when there's a non-200 OK navigation response.
View fallback-on-error-plugin.js
export class FallbackOnErrorPlugin {
constructor(fallbackURL) {
this.fallbackURL = fallbackURL;
// This is called whenever there's a network response,
// but we want special behavior for non-2xx statuses.
fetchDidSucceed({response}) {
if (response.ok) {
// If it's a 2xx, it can be used as-is.
View cache-network-race.ts
import {RouteHandlerCallbackOptions} from 'workbox-core/types';
import {CacheFirst, NetworkFirst} from 'workbox-strategies';
import {registerRoute} from 'workbox-routing';
// Borrowed from
function promiseAny(promises: Array<Promise<Response>>): Promise<Response> {
return new Promise((resolve, reject) => {
promises = => Promise.resolve(p));
promises.forEach(p => p.then(resolve));
promises.reduce((a, b) => a.catch(() => b))
jeffposnick / totalCacheSize.js
Created Oct 11, 2019
Snippet to get the total size of everything in the Cache Storage API for the current origin
View totalCacheSize.js
async function totalCacheSize() {
let size = 0;
const cacheNames = await caches.keys();
for (const cacheName of cacheNames) {
const cache = await;
const cachedRequests = await cache.keys();
for (const cachedRequest of cachedRequests) {
const cachedResponse = await cache.match(cachedRequest);
const responseBlob = await cachedResponse.blob();
size += responseBlob.size;
jeffposnick / new-service-worker.ts
Created Oct 6, 2019
Workbox CDN + JS service worker migrated to a custom bundle + TS equivalent
View new-service-worker.ts
import {CacheFirst, NetworkOnly} from 'workbox-strategies';
import {cacheNames} from 'workbox-core';
import {cleanupOutdatedCaches, getCacheKeyForURL, precacheAndRoute} from 'workbox-precaching';
import {ExpirationPlugin} from 'workbox-expiration';
import {initialize as initializeOfflineAnalytics} from 'workbox-google-analytics';
import {registerRoute, setCatchHandler} from 'workbox-routing';
import {RouteHandlerCallbackOptions} from 'workbox-core/types';
import {skipWaiting} from 'workbox-core';
import nunjucks from 'nunjucks/browser/nunjucks';
jeffposnick / custom-wb-code.js
Created Sep 27, 2019
Example of using importScriptsViaChunks
View custom-wb-code.js
import {registerRoute} from 'workbox-routing/registerRoute';
registerRoute('/path', yourHandlerCode);
jeffposnick / service-worker.js
Created Sep 20, 2019
Example of InjectManifest in Workbox v5
View service-worker.js
// Add any other logic here as needed.
import { CacheableResponsePlugin } from 'workbox-cacheable-response/CacheableResponsePlugin';
import { CacheFirst } from 'workbox-strategies/CacheFirst';
import { createHandlerForURL } from 'workbox-precaching/createHandlerForURL';
import { ExpirationPlugin } from 'workbox-expiration/ExpirationPlugin';
import { NavigationRoute } from 'workbox-routing/NavigationRoute';
import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute';
import { registerRoute } from 'workbox-routing/registerRoute';
View sw.js
addEventListener('install', (event) => ; {
event.waitUntil(async function() {
const cache = await'static-v1');
await cache.addAll(['offline.html', 'styles.css']);
// See
addEventListener('activate', event => {
event.waitUntil(async function() {
jeffposnick /
Created Dec 19, 2018
Shell script to automate nightly updates for the Camino browser (circa 2003?)
echo "Beginning Camino Nightly Download: `date`" >> /Users/jeff/Documents/Camino/log.txt
echo "Changing to download folder" >> /Users/jeff/Documents/Camino/log.txt
cd ~/Documents/Camino
echo "pwd is: `pwd`" >> /Users/jeff/Documents/Camino/log.txt
echo "Downloading Camino: `date`" >> /Users/jeff/Documents/Camino/log.txt
jeffposnick / find-opaque-responses.js
Created Dec 2, 2018
Code to find all opaque responses currently cached, and return their URLs.
View find-opaque-responses.js
async function findOpaqueResponses() {
const cacheNames = await caches.keys();
const urlsResultingInAnOpaqueResponse = [];
for (const cacheName of cacheNames) {
const cache = await;
const requests = await cache.keys();
for (const request of requests) {
const response = await cache.match(request);
if (response.status === 0) {