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

offline-audit.mjs

90 lines · 5.6 KB · JavaScript
// Offline audit for the hosted deck, shared by scripts/verify.mjs (local server) and
// scripts/check-live.mjs (the live host). Call it on a page that has visited `base` once online.
// It checks that the service worker reports every precached file stored, then cuts the network and
// reloads: the page (with and without the trailing slash, and a deep link), every precached file
// (fetched through the worker), every <img> (decoded, nonzero size), the three fonts, the phone
// demo's own images inside its iframes, and the speaker view. Any same-origin request that fails
// while offline is a problem. Returns { problems, summary }.
export async function auditOffline(context, page, base) {
	const problems = []
	const origin = new URL(base).origin
	const noSlash = base.replace(/\/$/, '')
	await page.goto(base)
	await page.waitForFunction(() => window.__pwa?.state === 'ready' || window.__pwa?.state === 'incomplete' || window.__pwa?.state === 'error', null, { timeout: 30000 })
	const reg = await page.evaluate(() => ({ ...window.__pwa }))
	if (reg.state !== 'ready') problems.push(`service worker not offline-ready: ${JSON.stringify(reg)}`)
	await page.reload()
	await page.waitForFunction(() => window.__deck?.isReady())
	if (!(await page.evaluate(() => !!navigator.serviceWorker.controller))) problems.push('service worker is not controlling the page after a reload')

	await context.setOffline(true)
	const failed = []
	// A request cancelled because the audit navigated on (ERR_ABORTED) is not a failure; anything else is.
	// /cdn-cgi/ is the host's own analytics beacon (Cloudflare Web Analytics), injected outside the deck.
	const onFail = (r) => { if (r.url().startsWith(origin) && !r.url().startsWith(`${origin}/cdn-cgi/`) && !/ERR_ABORTED/.test(r.failure()?.errorText ?? '')) failed.push(`${r.url().slice(origin.length)} (${r.failure()?.errorText})`) }
	page.on('requestfailed', onFail)
	const loads = []
	for (const url of [base, noSlash, `${base}#/why-now`]) {
		const ok = await page.goto(url).then(() => page.waitForFunction(() => window.__deck?.isReady(), null, { timeout: 10000 })).then(() => true, () => false)
		loads.push(`${url.slice(origin.length) || '/'} ${ok ? 'ok' : 'FAILED'}`)
		if (!ok) problems.push(`offline load of ${url} failed`)
	}
	const check = await page.evaluate(async () => {
		const out = { precache: 0, precacheBad: [], images: 0, imagesBad: [], fonts: {} }
		for (const path of window.__PRECACHE ?? []) {
			const url = new URL(path, new URL('./', location.href)).href
			const res = await fetch(url).catch(() => null)
			if (res?.ok) out.precache++
			else out.precacheBad.push(path || '(page)')
		}
		for (const img of document.images) {
			await img.decode().catch(() => {})
			if (img.complete && img.naturalWidth > 0) out.images++
			else out.imagesBad.push(img.getAttribute('src')?.slice(0, 80))
		}
		for (const [name, font] of [['Bricolage Grotesque', '700 60px "Bricolage Grotesque"'], ['Wix Madefor Text', '600 44px "Wix Madefor Text"'], ['Inter', '450 40px "Inter"']]) {
			await document.fonts.load(font).catch(() => {})
			out.fonts[name] = document.fonts.check(font)
		}
		return out
	})
	if (check.precacheBad.length) problems.push(`offline: precached files not served from cache: ${check.precacheBad.join(', ')}`)
	if (check.imagesBad.length) problems.push(`offline: broken images: ${check.imagesBad.join(', ')}`)
	for (const [name, ok] of Object.entries(check.fonts)) if (!ok) problems.push(`offline: font ${name} not loaded`)
	// The phone demo is its own document with its own hashed images and fonts.
	const demo = await page.evaluate(async () => {
		const s = document.getElementById('victorias-app')
		const i = window.__deck.getIndices(s)
		window.__deck.slide(i.h, 0, -1)
		const t0 = performance.now()
		while (!window.__demoSettled() && performance.now() - t0 < 20000) await new Promise((done) => setTimeout(done, 100))
		const frames = [...document.querySelectorAll('.demo iframe')]
		let images = 0
		const bad = []
		for (const f of frames) {
			const doc = f.contentDocument
			if (!doc) { bad.push('frame without document'); continue }
			for (const img of doc.images) {
				await img.decode().catch(() => {})
				if (img.complete && img.naturalWidth > 0) images++
				else bad.push(img.getAttribute('src')?.slice(0, 80))
			}
		}
		return { settled: window.__demoSettled(), frames: frames.length, images, bad }
	})
	if (!demo.settled || !demo.frames) problems.push(`offline: phone demo did not load (${JSON.stringify(demo)})`)
	if (demo.bad.length) problems.push(`offline: broken demo images: ${demo.bad.join(', ')}`)
	await page.goto(base)
	await page.waitForFunction(() => window.__deck?.isReady())
	const [popup] = await Promise.all([context.waitForEvent('page'), page.keyboard.press('s')])
	await popup.waitForLoadState()
	await popup.waitForTimeout(2500)
	const speaker = await popup.evaluate(() => document.querySelector('.speaker-controls-notes')?.textContent ?? '')
	if (!/Victoria/.test(speaker)) problems.push('offline: speaker view does not show the notes')
	await popup.close()
	page.off('requestfailed', onFail)
	if (failed.length) problems.push(`offline: same-origin requests failed: ${[...new Set(failed)].slice(0, 8).join(', ')}`)
	await context.setOffline(false)
	const summary = `worker ${reg.state} (${reg.files} files precached, ${reg.missing} missing); offline loads ${loads.join(', ')}; ${check.precache} precached files served from cache; ${check.images} page images and ${demo.images} demo images (${demo.frames} phones) decoded; fonts ${Object.entries(check.fonts).map(([k, v]) => `${k} ${v ? 'ok' : 'MISSING'}`).join(', ')}; speaker view ${/Victoria/.test(speaker) ? 'ok' : 'FAILED'}`
	return { problems, summary }
}