Skip to content
← Public packages

@kentcdodds/invisible-software-talk

Invisible Software: Kent C. Dodds's AI Coding Summit NYC 2026 talk deck. Agents become software's next billion users.

FollowForkOpen app

Files

sw.js

87 lines · 3.3 KB · JavaScript
// Offline support for the hosted deck. The page references its fonts, images and the phone demo as
// content-hashed files under c/ (name.<hash>.ext). On install the worker precaches the page, the PWA
// files and every hashed asset (the list is generated at build time); it only activates once all of
// them are stored, and the page reports offline-ready only after checking every one is in the cache.
// The page loads network-first (2.5s timeout), so an online visit gets the latest publish and
// refreshes the cached copy; with no network (or a stalled one) the cached copy is served at once.
// Only a real deck response (the app's x-invisible-software-deck header, not redirected) is cached
// or shown; anything else, such as a captive portal page or a 403/5xx, falls back to the cached deck.
// Hashed assets never change, so they are served cache-first. Old caches are deleted on activate.
const VERSION = '__VERSION__'
const CACHE = `invisible-sw-deck-${VERSION}`
const BASE = new URL('./', self.location).href
const PAGE = BASE
const PRECACHE = __PRECACHE__.map((path) => new URL(path, BASE).href)
const PRECACHED = new Set(PRECACHE)

self.addEventListener('install', (event) => {
	event.waitUntil(
		caches
			.open(CACHE)
			.then((cache) => cache.addAll(PRECACHE.map((url) => new Request(url, { cache: 'reload' }))))
			.then(() => self.skipWaiting()),
	)
})

self.addEventListener('activate', (event) => {
	event.waitUntil(
		caches
			.keys()
			.then((keys) => Promise.all(keys.filter((key) => key.startsWith('invisible-sw-deck-') && key !== CACHE).map((key) => caches.delete(key))))
			.then(() => self.clients.claim()),
	)
})

const withTimeout = (promise, ms) =>
	new Promise((resolve, reject) => {
		const timer = setTimeout(() => reject(new Error('timeout')), ms)
		promise.then(
			(value) => {
				clearTimeout(timer)
				resolve(value)
			},
			(error) => {
				clearTimeout(timer)
				reject(error)
			},
		)
	})

self.addEventListener('fetch', (event) => {
	const { request } = event
	if (request.method !== 'GET') return
	const url = request.url.split('#')[0]
	// Precached files first, including the phone demo document: the demo iframes load it with a
	// navigation request, which must never be mistaken for (or cached as) the deck page.
	if (PRECACHED.has(url) && url !== PAGE) {
		// Cache-first: hashed files never change, and the PWA files are versioned with the cache.
		event.respondWith(
			caches.match(url, { cacheName: CACHE }).then(
				(hit) =>
					hit ??
					fetch(request).then(async (response) => {
						if (response.ok) await (await caches.open(CACHE)).put(url, response.clone())
						return response
					}),
			),
		)
		return
	}
	if (request.mode === 'navigate' || url === PAGE) {
		// Every path serves the same page, so the page is cached under one key.
		event.respondWith(
			withTimeout(fetch(request), 2500)
				.then(async (response) => {
					const isDeck = response.ok && !response.redirected && response.headers.get('x-invisible-software-deck') === '1'
					if (isDeck) {
						const cache = await caches.open(CACHE)
						await cache.put(PAGE, response.clone())
						return response
					}
					return (await caches.match(PAGE, { cacheName: CACHE })) ?? response
				})
				.catch(async () => (await caches.match(PAGE, { cacheName: CACHE })) ?? Response.error()),
		)
		return
	}
})