Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
jeffposnick / index.html
Created October 17, 2018 20:48
Example of precaching video content, and using it with a Workbox-powered SW.
<!doctype html>
<html>
<head>
<title>Video Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>
Here's a small local video. (Copy from http://techslides.com/demos/sample-videos/small.mp4)
@jeffposnick
jeffposnick / register-sw.js
Last active September 13, 2021 11:53
A modern-ish SW registration script, detecting various state changes.
if ('serviceWorker' in navigator) {
window.addEventListener('load', async function() {
const registration = await navigator.serviceWorker.register('/service-worker.js');
if (registration.waiting && registration.active) {
// The page has been loaded when there's already a waiting and active SW.
// This would happen if skipWaiting isn't being called, and there are
// still old tabs open.
console.log('Please close all tabs to get updates.');
} else {
// updatefound is also fired for the very first install. ¯\_(ツ)_/¯
@jeffposnick
jeffposnick / index.html
Last active July 26, 2018 20:05
Testing manifest.json interception by a service worker.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Manifest Test</title>
<link rel="manifest" href="manifest.json">
<link rel="icon" href="icon.png">
</head>
<body>
I'm just a test file.
@jeffposnick
jeffposnick / app.js
Created October 5, 2017 20:06
Example of staleWhileRevalidate + broadcastCacheUpdate
const HN_URL = 'https://hackernewsapi.example.com/';
function updateUI(hnData) {
// Update the DOM.
}
async function init() {
const channel = new BroadcastChannel('hn-updates');
channel.addEventListener('message', async (event) => {
if (event.data.payload.updatedUrl === HN_URL) {

After thinking about this a bit more, and reading Paul Lewis's article about rIC, I'm of the opinion that it's not the right fit.

rIC seems geared towards delaying a discrete piece of work, which will happen on the main thread, until there are a few free moments.

In our case, the expensive bits all happen in a separate thread, at some indeterminate future point in time, after the serviceWorker.register() has already completed.

rIC doesn't offer any guarantees that starting up the SW thread and precaching resources will happen when the main thread is idle, and I don't want to give developers the false impression that it does.

@jeffposnick
jeffposnick / index.html
Created September 22, 2016 15:51
beforeinstallprompt demo
<html>
<head>
<title>Test</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<p>
If the <code>beforeinstallprompt</code> event fires, there will be a button displayed allowing
you to use <code>prompt()</code> on the deferred event.
</p>
@jeffposnick
jeffposnick / cache-size.js
Last active August 10, 2016 20:56
Total size, in bytes, of all entries in the Cache Storage API
// Note that any opaque (i.e. cross-domain, without CORS) responses in the cache will return a size of 0.
caches.keys().then(cacheNames => {
let total = 0;
return Promise.all(
cacheNames.map(cacheName => {
// Change this to match the cache name filter you want.
if (!cacheName.includes('sw-precache')) {
return;
}
@jeffposnick
jeffposnick / index.html
Last active June 13, 2016 20:44
SW test
<html>
<head>
<title>SW Test</title>
</head>
<body>
<img src="nope.jpg">
<script>
navigator.serviceWorker.register('sw.js');
</script>
</body>
@jeffposnick
jeffposnick / @ SimpleDB.md
Last active September 7, 2016 17:19
Simple DB

SimpleDB - Like Indexed DB, but Simple

A simple asynchronous data store.

STATUS: This is a thought experiment, not a serious proposal. Would basic async storage like this be useful? With this plus some locking primitive, could you build Indexed DB?

Like Indexed DB:

  • rich value types - store anything you can structured clone
  • rich key types - Number, String, Date, Array (of other key types)