Writing Skills
writing-skills
效率与协作技能,来自 obra/superpowers,查看原文获取完整用途和操作说明。
webar-mindar-ios【部署与运维】Build, debug, and ship browser-based AR image-tracking experiences (MindAR + A-Frame + Three.js inside a Next.js App Router H5), and especially diagnose the iOS-Safari/WebKit-only failures that desktop Chromium never reproduces — blank or black camera after "start AR", scenes collapsing to width:0, next/image not rendering, gyro/DeviceOrientation not…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
This skill captures a battle-tested workflow for web AR image tracking (point the phone camera at a printed/displayed target, anchor an animated overlay on it) built as a Next.js H5 page, plus the parts that are genuinely hard: iOS WebKit-only bugs and how to verify them without a physical iPhone.
The single most important idea: iOS Safari AND iOS Chrome both run WebKit
(Apple mandates it). Desktop Chromium ≠ iOS. A page that is perfect in Chromium
can be blank on every iPhone browser. So the verification loop is built around
Playwright's WebKit engine (= Safari's engine) on the desktop, with a
fake camera injected via getUserMedia override, because that's the only way
to see the real error fast instead of round-tripping screenshots with the user.
AR runtime (vendored as static files under public/vendor/ar/, not npm — keeps
them out of the bundle and lets you cache-bust with a ?v= query):
1.5.0 + MindAR mindar-image-aframe 1.2.5 (image tracking) + Three.js (bundled in A-Frame)..mind files (from the printed artwork) served from public/.App: Next.js 16 (App Router) + React 19, AR components are "use client".
loadMindArScripts() that appends <script> and resolves on onload.ArImageTrackingScene component wrapping the
<a-scene mindar-image> + <a-camera> + a target <a-entity mindar-image-target>.full | lite | 2d), NOT real 3D models — cheapest, most WebView-compatible.Verification: Playwright WebKit (desktop Safari engine) + fake camera, plus jsdom unit tests (vitest) that mock MindAR/getUserMedia. The two are complementary — see "Why the test pyramid has a hole" below.
Deploy (for on-phone testing / staging): source tarball → GCE VM over IAP SSH
→ npm ci && next build && next start (Node 20 via nvm) → cloudflare quick
tunnel for an instant public HTTPS URL. (Real production for a China 文旅/微信
audience is a separate target — Aliyun + ICP 备案 + .cn; GCP is blocked/slow in
mainland China and only suitable for staging.)
When an AR page is "blank/broken on iPhone but fine in Chrome", do NOT keep asking
the user to retry. Reproduce it yourself in WebKit. Full scripts and the exact
flow are in references/verification-playwright-webkit.md and
scripts/webkit-repro.mjs. The essentials:
npx playwright install webkit
(it must match your installed playwright version), then
webkit.launch() + browser.newContext({ ...devices['iPhone 13'] }).--use-fake-device... flag
(that's Chromium-only) and grantPermissions(['camera']) throws ("Unknown
permission"). Instead, in an addInitScript, override
navigator.mediaDevices.getUserMedia to return a canvas.captureStream(). Now
MindAR mounts and proceeds as if a camera exists.page.on('console' | 'pageerror' | 'requestfailed').getBoundingClientRect() of a-scene, <video>, <canvas>. A WebKit-only
width: 0 is the classic blank-camera cause. If width is 0, walk the DOM
ancestors logging each offsetWidth to find the element that collapses.Versions/UA matter: the user's iPhone may run an older WebKit than Playwright's latest. If you cannot reproduce with the latest, ask for their iOS version and match it, or use the on-screen error overlay (below) to read the real device error.
These are documented with root cause + fix in
references/ios-webkit-gotchas.md. The headline ones:
width:0 (blank camera after "start AR"). Cause: a
CSS-grid container with place-items-center (i.e. justify-items: center) sizes
its in-flow grid item to content width; when the only children are
absolutely-positioned (a-scene, overlays) there is no content width → WebKit
collapses it to 0 (Chromium happened to keep a width, so it only repros on iOS).
Fix: make the AR scene container fill its positioned parent with
absolute inset-0 instead of in-flow relative h-full — the same pattern a
working next/image fill already uses.next/image blank on some iOS versions. The optimizer + AVIF/format
negotiation can yield an image the device can't decode (works via curl with a
PNG Accept). For pre-sized art, set images: { unoptimized: true } to serve
the raw file and sidestep the optimizer entirely.DeviceOrientationEvent.requestPermission() from inside a user gesture; gate the
listener on the grant and always keep a touch-drag fallback.<video> explicitly
visible via CSS, and avoid React effect-cleanup tearing down MindAR — store
callbacks in a ref so the effect doesn't re-run and stop() the tracker.aspect-[4/3] box feels tiny; when the
camera is active, promote the viewport to fixed inset-0 (camera fills screen),
float the controls in a bottom sheet (fixed bottom-0, safe-area padded), hide
page chrome, and add a safe-area-aware exit button.entry → loading → scanning → tracking). Camera permission appears only after
the tap — required for iOS and good for trust.ArImageTrackingScene (single target, targetIndex, callbacks
onSceneReady/onSceneError/onTargetFound/onTargetLost) keeps every AR page
consistent and keeps the black-screen mitigation in one place.detectRenderTier() → full/lite/2d from
prefers-reduced-motion, hardwareConcurrency, deviceMemory, WeChat UA) so
low-end phones and WeChat WebView degrade instead of breaking.window.error/unhandledrejection and renders a dismissible panel only when an
error occurs. This is your "remote eyes" on a real phone when you can't attach a
Mac/Safari Web Inspector.Full pipeline + the exact gotchas in references/deploy-gce-cloudflare.md and
the template in scripts/deploy-gce-cloudflare.sh. The traps that cost time:
gcloud compute ssh/scp in a retry loop, and
run the actual deploy as a setsid detached script on the VM that logs to a
file, then poll the log. A dropped SSH must not kill the build.nvm on the VM (don't touch system Node).gh auth on the VM → ship a source tarball via scp
(exclude node_modules/.next/.git and stray junk like a Windows D:/... cache
dir) and npm ci on the VM, rather than git pull.next start renames its process to next-server — pkill -f "next start"
misses it, so the old build keeps port 3100 and your new build silently fails to
bind. Kill by port (ss -tlnp → pid → kill -9).next, not
cloudflared (the quick-tunnel URL changes every time cloudflared restarts).?v=N query (Safari caches aggressively; even
"private tab" can mislead if you don't change the URL).You can verify everything except a real camera recognizing a real printed target and device-specific performance/heat. That last mile (recognition rate across a device matrix, WeChat WebView, real lighting) is the human's on-site job — say so plainly rather than implying you tested it.