Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/mobile-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Mobile check (developer tool)

`scripts/mobile-check.mjs` loads the site under **true device emulation** and fails when a page has a mobile
defect. It is a developer tool: it is not part of the build and nothing in CI runs it.

## Why not `chrome --headless --window-size=390,844`

Headless Chrome will not go narrower than about 500px. With `--window-size=390,844` it lays the page out at
about 500px and crops the screenshot, which fakes a cut-off. The script instead drives Chrome through
[puppeteer-core](https://pptr.dev) with `Emulation.setDeviceMetricsOverride` (real layout width), a mobile user
agent, touch, and a device pixel ratio of 2 or 3. It uses your installed Google Chrome; nothing is downloaded.

## Run it

```bash
npm install # puppeteer-core is a devDependency; axe-core already was
npm run build
find dist functions lib src -name '._*' -delete # macOS sidecar files break wrangler on external drives
npx wrangler pages dev dist --port 8799 --kv CLAIMS & # run it directly; the preview launcher hangs
npm run mobile-check # all pages x all devices, light/dark/warm axe at 375
```

Useful options:

```bash
npm run mobile-check -- --pages /,/the-81 --devices iphone-se-1,iphone-se,phone-large
npm run mobile-check -- --axe-all # axe at every device, not just 375
npm run mobile-check -- --no-axe -v # fast; -v also prints warnings
npm run mobile-check -- --shots /tmp/shots # JPEG screenshots, sliced every 1400px
npm run mobile-check -- --json out.json # raw measurements
npm run mobile-check -- --no-fail # report only (exit code 0)
CHROME_PATH=/path/to/chrome npm run mobile-check
```

Devices (`--devices`): `iphone-se-1` 320x568, `android-s` 360x800, `iphone-se` 375x667, `iphone-14` 390x844,
`phone-large` 430x932, `landscape` 844x390, `tablet` 768x1024. Default is all seven.

## What fails the run (exit code 1)

| Check | Rule |
|---|---|
| Horizontal overflow | `scrollWidth` of the page greater than the viewport, measured with the `overflow-x:hidden` safety net on `html, body` **lifted**, plus a list of the elements that pass the edge outside an intentional scroller |
| Tap targets | any link, button, field, summary or tab smaller than 44x44 CSS px. Inline links inside running text are listed as warnings (WCAG 2.2 exempts them) |
| Text fields | font size under 16px (iOS Safari zooms the page on focus) |
| Dock bar | its controls under 44px, or the bar covering the last lines of the page |
| axe | any violation in light, dark or warm (at 375; `--axe-all` for every device) |
| Console | any console error or page error (a CSP refusal shows up here) |

Warnings (printed with `-v`, never fail): content clipped by `overflow:hidden`, horizontal scrollers that hide
content, text under 12px, inline links under 44px, a viewport meta without `viewport-fit=cover`.

Reader pages are loaded in a fresh browser context (signed out, so the gate is what you see). `/read/NN/full`
signs a test address (`mobile-check@example.test`) into the local KV, so run it against `wrangler pages dev`,
never against production.

## What it cannot tell you

Headless Chrome is not iOS Safari. The script cannot see the dynamic toolbar (`100vh` vs `100dvh`),
`env(safe-area-inset-*)` (always 0 here), iOS-specific `position:sticky`, mask or backdrop behaviour, pinch-zoom,
or what a thumb actually reaches. See the checklist at the end of `drafts/mobile/AUDIT.md`.
207 changes: 207 additions & 0 deletions drafts/mobile/AUDIT.md

Large diffs are not rendered by default.

Binary file added drafts/mobile/shots/audit/agents-table-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/contribute-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/dossier-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/home-hero-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/nav-landscape-844.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/nav-tablet-768.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/read-gate-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/read01-cards-320.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/the81-elements-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/the81-graph-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drafts/mobile/shots/audit/the81-sticky-375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions drafts/mobile/tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Screenshot helpers for the mobile audit

Throw-away helpers used to make the before/after images in the PRs. They need `pillow` (Python) and the
emulation setup from `docs/mobile-check.md`.

- `shot.mjs <base> <url> <device> <out.jpg> [js] [wait-ms] [--full] [--theme=dark]` one emulated screenshot
- `probe.mjs <url> <w> <h> '<js>'` evaluate an expression in an emulated viewport (`BASE=` picks the server)
- `sheet.py` / `grid.py` tile screenshots side by side or in a captioned grid (small JPEGs)
- `mkgrid.sh` before/after grid at 320, 375 and 430
35 changes: 35 additions & 0 deletions drafts/mobile/tools/graph-tap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// usage: node graph-tap.mjs <base> <device> <out.jpg> — open GRAPH, tap nodes by touch, report strip state, screenshot
import { createRequire } from 'node:module';
import { DEVICES } from '../../../scripts/mobile-check.mjs';
const require = createRequire(import.meta.url.replace('drafts/mobile/tools/graph-tap.mjs', 'scripts/x.mjs'));
const puppeteer = require('puppeteer-core');
const [base, dev, out] = process.argv.slice(2);
const d = DEVICES[dev];
const b = await puppeteer.launch({ executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless: 'new' });
const p = await b.newPage();
await p.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1');
await p.setViewport({ width: d.w, height: d.h, deviceScaleFactor: 1.5, isMobile: true, hasTouch: true });
await p.goto(base + '/the-81', { waitUntil: 'networkidle2' });
await p.evaluate(() => document.getElementById('btn-graph').click());
await new Promise(r => setTimeout(r, 3500));
await p.evaluate(() => document.getElementById('graph-canvas').scrollIntoView({ block: 'start' }));
await new Promise(r => setTimeout(r, 300));
const box = await p.evaluate(() => { const r = document.getElementById('graph-canvas').getBoundingClientRect(); return { x: r.left, y: r.top, w: r.width, h: r.height, vh: innerHeight }; });
console.log('canvas', JSON.stringify(box));
let tapped = 0, picked = [];
for (let gy = 0.1; gy < 0.9 && picked.length < 2; gy += 0.07) for (let gx = 0.1; gx < 0.9 && picked.length < 2; gx += 0.07) {
const y = box.y + box.h * gy; if (y > box.vh - 10 || y < 10) continue;
await p.touchscreen.tap(box.x + box.w * gx, y); tapped++;
const st = await p.evaluate(() => { const s = document.getElementById('graph-strip'); return s.hidden ? null : document.getElementById('graph-strip-name').textContent + '|' + document.getElementById('graph-sel-count').textContent; });
if (st && !picked.includes(st)) picked.push(st);
}
console.log('taps', tapped, 'picked', JSON.stringify(picked));
await p.screenshot({ path: out, type: 'jpeg', quality: 60 });
// page must still scroll when a swipe starts on the canvas
const y0 = await p.evaluate(() => scrollY);
await p.touchscreen.touchStart(box.x + box.w / 2, Math.min(box.y + 300, box.vh - 100));
await p.touchscreen.touchMove(box.x + box.w / 2, Math.min(box.y + 300, box.vh - 100) - 150);
await p.touchscreen.touchEnd();
await new Promise(r => setTimeout(r, 400));
console.log('scroll moved by swipe on canvas:', (await p.evaluate(() => scrollY)) - y0);
await b.close();
31 changes: 31 additions & 0 deletions drafts/mobile/tools/grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
# Lay screenshots out in a grid with captions, as one small JPEG (for PR descriptions).
# usage: grid.py out.jpg cellwidth cols "caption1|file1" "caption2|file2" ...
import sys
from PIL import Image, ImageDraw, ImageFont
out, cw, cols, *items = sys.argv[1:]
cw, cols = int(cw), int(cols)
cells = []
for it in items:
cap, path = it.split('|', 1)
im = Image.open(path).convert('RGB')
im = im.resize((cw, round(im.height * cw / im.width)), Image.LANCZOS)
cells.append((cap, im))
try:
font = ImageFont.truetype('/System/Library/Fonts/Menlo.ttc', 13)
except Exception:
font = ImageFont.load_default()
rows = [cells[i:i + cols] for i in range(0, len(cells), cols)]
gap, cap_h = 8, 22
rh = [max(im.height for _, im in r) + cap_h for r in rows]
sheet = Image.new('RGB', (cols * cw + gap * (cols - 1), sum(rh) + gap * (len(rows) - 1)), (255, 255, 255))
d = ImageDraw.Draw(sheet)
y = 0
for r, h in zip(rows, rh):
for c, (cap, im) in enumerate(r):
x = c * (cw + gap)
d.text((x + 4, y + 3), cap, fill=(20, 20, 20), font=font)
sheet.paste(im, (x, y + cap_h))
y += h + gap
sheet.save(out, 'JPEG', quality=62, optimize=True)
print(out, sheet.size)
13 changes: 13 additions & 0 deletions drafts/mobile/tools/mkgrid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/zsh
# usage: mkgrid.sh <dir> <out.jpg> name1:"caption" name2:"caption" ... (each name has -before/-after-<device>.jpg; use name@before / name@after for single-sided rows)
dir=$1; out=$2; shift 2
items=()
for spec in "$@"; do
key=${spec%%:*}; cap=${spec#*:}
tag=${key##*@}; name=${key%%@*}
for dev in iphone-se-1 iphone-se phone-large; do
case $dev in iphone-se-1) w=320;; iphone-se) w=375;; *) w=430;; esac
items+=("$cap @${w}|$dir/$name-$tag-$dev.jpg")
done
done
python3 ${0:A:h}/grid.py $out 300 3 "${items[@]}"
14 changes: 14 additions & 0 deletions drafts/mobile/tools/probe.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// usage: BASE=http://localhost:8799 node probe.mjs <url> <width> <height> '<js expression string>'
// Evaluates an expression in an emulated phone viewport (DPR 2) and prints the JSON result.
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url.replace('drafts/mobile/tools/probe.mjs', 'scripts/x.mjs'));
const puppeteer = require('puppeteer-core');
const [url, w, h, expr] = process.argv.slice(2);
const b = await puppeteer.launch({ executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless: 'new' });
const p = await b.newPage();
await p.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1');
await p.setViewport({ width: +w, height: +h, deviceScaleFactor: 2, isMobile: true, hasTouch: true });
await p.goto((process.env.BASE || 'http://localhost:8799') + url, { waitUntil: 'networkidle2' });
await new Promise(r => setTimeout(r, 600));
console.log(JSON.stringify(await p.evaluate(expr), null, 1));
await b.close();
18 changes: 18 additions & 0 deletions drafts/mobile/tools/sheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Tile several screenshots side by side as one small JPEG for viewing.
# usage: sheet.py out.jpg cellwidth in1.jpg in2.jpg ...
import sys
from PIL import Image
out, cw, *ins = sys.argv[1:]
cw = int(cw)
ims = []
for p in ins:
im = Image.open(p).convert('RGB')
h = round(im.height * cw / im.width)
ims.append(im.resize((cw, h), Image.LANCZOS))
H = max(i.height for i in ims)
sheet = Image.new('RGB', (cw * len(ims) + 8 * (len(ims) - 1), H), (128, 128, 128))
for n, im in enumerate(ims):
sheet.paste(im, (n * (cw + 8), 0))
sheet.save(out, 'JPEG', quality=60)
print(out, sheet.size)
22 changes: 22 additions & 0 deletions drafts/mobile/tools/shot.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// usage: node shot.mjs <base> <url> <device> <out.jpg> [js-to-run-before] [wait-ms] [--full] [--theme=dark]
import { createRequire } from 'node:module';
import { DEVICES } from '../../../scripts/mobile-check.mjs';
const require = createRequire(import.meta.url.replace('drafts/mobile/tools/shot.mjs', 'scripts/x.mjs'));
const puppeteer = require('puppeteer-core');
const args = process.argv.slice(2);
const flags = args.filter((a) => a.startsWith('--'));
const [base, url, dev, out, js, wait] = args.filter((a) => !a.startsWith('--'));
const UA = { iphone: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1', android: 'Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36', ipad: 'Mozilla/5.0 (iPad; CPU OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1' };
const d = DEVICES[dev];
const b = await puppeteer.launch({ executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless: 'new', args: ['--hide-scrollbars'] });
const p = await b.newPage();
await p.setUserAgent(UA[d.ua]);
await p.setViewport({ width: d.w, height: d.h, deviceScaleFactor: flags.includes('--hi') ? d.dpr : 1.5, isMobile: true, hasTouch: true, isLandscape: !!d.landscape });
const th = flags.find((f) => f.startsWith('--theme='));
if (th) await p.evaluateOnNewDocument((t) => { try { localStorage.setItem('kw-theme', t); } catch {} }, th.split('=')[1]);
await p.goto(base + url, { waitUntil: 'networkidle2', timeout: 180000 });
await new Promise((r) => setTimeout(r, 500));
if (js) { const r = await p.evaluate(js); if (r !== undefined) console.log(JSON.stringify(r)); }
await new Promise((r) => setTimeout(r, +(wait || 700)));
await p.screenshot({ path: out, type: 'jpeg', quality: 60, fullPage: flags.includes('--full') });
await b.close();
Loading
Loading