Repository navigation
Perf: the shell asks the service worker for a screen before you click it - #678
Conversation
The shared asset cache took the network out of a window's assets. It could never touch the document: admin HTML carries nonces and per-request screen state, so it is uncacheable by construction - and on a live install it is the majority of a window open, measured at ~2.1s of a ~3.8s tab click against ~1.7s for everything the browser then does with it. That cost does not have to be paid after the click. The shell already knows every URL a window can reach and already reads hover intent; the worker already sees every iframe navigation's fetch event and deliberately declines to answer it. Nothing connected the two. Now a hover on a submenu tab posts `os-speculate-doc` to the worker, which fetches that screen once and holds it, and the iframe's own navigation is answered from those bytes. This is deliberately NOT keeping tabs alive: nothing rendered is retained - no DOM, no live iframe, no memory beyond a response body dropped after 30s. The page is still built fresh, just early. The store holds the fetch's PROMISE rather than the settled response, which is the difference between helping sometimes and helping always: a user who hovers and clicks half a second later would otherwise arrive mid-flight, find nothing, and start a SECOND request for the same screen. Measured live: 600ms of hover turned a 3,605ms click into 1,014ms. Answering an iframe navigation is safe here precisely because the response is never re-fetched. The hazard the navigate branch guards against (issue #171) is the worker re-fetching an iframe request, which makes Chrome send Sec-Fetch-Dest: empty and collapses the whole desktop into a window. A speculative document is fetched once, ahead of time, from a URL carrying openstation_chromeless=1 - which the server checks before Sec-Fetch - so the held bytes are already correctly chromeless, and the fallback re-fetch is safe for the same reason. Speculation never acts: any URL carrying action/_wpnonce/delete_all is refused, so a hover can never activate a plugin or empty a trash. Single-use, exact-URL, 30s TTL, capped at 6 documents, and gated on the same hover-prewarm opt-in the dock uses. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
@claude can you review? |
|
Claude finished @AllTerrainDeveloper's task in 3m 34s —— View job Review
The core idea is sound and the write-up in the PR body is careful — I verified the central safety claim myself: Two issues worth fixing before merge, plus a couple of smaller notes. 1. The per-page
|
…till building the shell
A shell boot is two independent server renders run strictly one after
the other. Measured on a live install:
0ms ─── shell document requested
3172ms ─── shell HTML arrives (3.2s of server time)
3876ms ─── only NOW is a window document requested
6491ms ─── everything ready
The window's document does not depend on a single byte of the shell's,
yet it cannot even be asked for until the shell's HTML has arrived and
its JavaScript has run and built an iframe. The shell cannot fix this
from inside itself: by the time its code exists, the 3.2 seconds are
already spent.
The service worker can. It is woken by the shell's own navigation -
before the server has finished building it - so it can start the window
fetches right then, in parallel. It only needs to know what to fetch,
which is what the session saver now tells it: every save posts the
restore list, the worker persists it, and the next boot replays it the
moment the shell navigation arrives.
Reuses the speculative store wholesale, including the in-flight promise
semantics, so a window whose iframe appears mid-fetch waits on the
request already running instead of starting a second one. Same safety
rules: same-origin, /wp-admin/ only, chromeless flag required, nothing
carrying action/_wpnonce/delete_all, capped and short-lived. Gated on
the same hover-prewarm opt-in, and fire-and-forget - a boot never waits
on speculation.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
`replayedThisBoot` was a boolean, but a service worker outlives any single page load - it stays resident across navigations and can be reused for hours. So the replay fired on the first shell load that worker ever saw and never again, leaving every later boot unaccelerated. The first live measurement said so plainly: one window's TTFB halved (1,353ms -> 687ms) while the other sat unchanged at 1,246ms. Replaced with a short time-based throttle, which is all that was ever needed - beginSpeculation() already de-duplicates by URL. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Extended: the worker now starts your windows' documents while the server is still building the shellSame collaboration, applied to boot instead of hover — and this is the bigger half. A shell boot is two independent server renders run strictly one after the other. Measured on openstation.blog before this change: The window's document does not depend on a single byte of the shell's. It simply cannot be asked for until the shell's HTML has arrived and its JavaScript has run and built an iframe. And the shell can never fix this from inside itself: by the time its code exists, the 3.2 seconds are already spent. The service worker can. It is woken by the shell's own navigation — before the server has finished building it — so it can start the window fetches right then, in parallel. It only needs to know what to fetch, which is what the session saver now tells it: every save posts the restore list, the worker persists it to a cache, and the next boot replays it the moment the shell navigation arrives. Measured, same site, same session:
~1.8 s off every boot, and the first window usable 2.5 s sooner. The window documents now arrive effectively instantly, because they were fetched alongside the shell rather than after it. The bug the measurement caughtThe first live run was suspicious: one window's TTFB halved (1,353 → 687 ms) while the other sat unchanged at 1,246 ms. The cause was a wrong assumption of mine — Reuses the speculative store wholesale, including the in-flight promise semantics, so a window whose iframe appears mid-fetch waits on the request already running rather than starting a second. Same safety rules throughout: same-origin, |
1. The shell's `asked` set silently killed the feature after first use. It was never cleared in production, but the worker's held document is single-use and expires after 30s - so once a tab had been speculated once, whether the click consumed it or it simply timed out, every later hover over that tab fell through to a plain navigation with no speculation at all. Users bounce between the same few screens constantly, so the repeat visits were exactly what the feature should accelerate and exactly what it abandoned. Replaced with a 1s throttle: enough to damp a pointer crossing a tab's icon and label, far shorter than the worker's hold, so a genuine return visit always gets through. The worker remains the real de-duplicator. 2. docs/pwa.md now documents the whole surface - both message types, the config flag, the new interception path, the safety rules and the measured numbers - alongside the existing shared-asset-cache section, per the doc map in AGENTS.md. 3. Real test coverage for the load-bearing parts, which had none. `isSpeculatableDocument` moved to sw-policy.ts and the store to a new speculative-store.ts, both pure and now directly tested: the action/nonce denylist that keeps a hover from ever performing a state change, the promise-vs-settled-response semantics that burned the first implementation, single-use taking, TTL expiry, oldest-first eviction, and the shell-side origin check and throttle. 15 new cases. Also documents two deliberate choices the review flagged: pointerover over pointerenter (the latter does not bubble, so it cannot be delegated, and tabs come and go too often to bind per tab), and the speculative fetch not forwarding Referer / Accept-Language (admin screens do not branch on either; fabricating headers we never received would be guessing). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Review fixes pushed (
|
Verified live: the repeat-visit fix works, and the "missing tabs" was my test harness, not a bugOn the tab strip. I chased this as a possible pre-existing bug and it is not one — the product is correct and I was measuring wrong. Restored windows carry their submenu tabs properly ( The empty strips I kept hitting came from my own harness calling On the actual fix (issue 1), measured on openstation.blog using a dock-shaped open:
Before the fix, the 2nd and 3rd visits to Writing would have been silent no-ops falling through to a plain ~2 s navigation, because the URL was already in the permanent Controls in the same session confirm the baseline hasn't moved: un-hovered clicks still land at ~2.0 s. |
|
@claude would you mind one second review? |
|
Claude finished @AllTerrainDeveloper's task in 4m 10s —— View job Second review
The fixes from the first pass hold up. Two things worth a look on the new boot-replay half ( 1.
|
… path too 1. rememberRestoreTargets() fired on every session save regardless of the opt-in, unlike the two other speculation call sites which both read windowPrewarmEnabled first. A user who never touched the setting still paid a postMessage and a Cache Storage write on every debounced save - small, but it contradicted "off by default; zero behaviour change until a user opts in", which the PR body and docs/pwa.md both promise. Now gated the same way, in a shared noteRestoreTargets() helper, and gated again on the worker side so a stray message cannot make an opted-out browser start writing caches. 2. flushImmediately() - the pagehide beacon that fires when the tab actually closes - never updated the list, so the worker replayed whichever debounced save happened to land last. Close a window and exit before the 500ms debounce and the next boot speculates a document for a window that is gone, and misses one opened just before exit. Session restore itself was always accurate (that rides the beacon), so this was a lost optimisation rather than a correctness bug - but it is exactly the "close it and come straight back" case the feature exists for. Both paths now share the helper. docs/pwa.md tightened to match: the list is posted from both save paths, and the opt-in is checked on both sides. Covered by a new test that drives the real session saver with the toggle off and asserts no `os-remember-session` message is sent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Both fixed (
|
The shared asset cache from #676 took the network out of a window's assets. It could never touch the document — admin HTML carries nonces and per-request screen state, so it is uncacheable by construction. And on a live install the document is the majority of a window open: measured at ~2.1 s of a ~3.8 s tab click, against ~1.7 s for everything the browser then does with it.
That cost does not have to be paid after the click.
The gap this closes
Two halves existed and were never connected:
fetchevent for every iframe navigation — and deliberately declines to answer it.Now a hover on a submenu tab posts
os-speculate-docto the worker, which fetches that screen once and holds it; the iframe's own navigation is then answered from those bytes.This is explicitly not "keep the tab alive." Nothing rendered is retained: no DOM, no live iframe, no memory beyond a response body dropped after 30 s. The page is still built fresh — it is simply built while the user is still deciding.
Measured on openstation.blog (production WordPress.com hosting, real network)
Roughly 2–3.5× faster, and strikingly consistent once the document is in hand. The residual ~1 s is the browser parsing and executing that page's JavaScript — the half no prefetch can remove, and exactly what the earlier breakdown predicted:
This PR removes the first row from the user's wait. The second row is untouched and honestly out of reach without retaining the rendered page, which we deliberately are not doing.
The bug the first measurement caught
The first implementation held only the settled response. Two runs disagreed wildly — 5 ms in one, 1,001 ms in another — and the reason mattered: a click landing while the fetch was still in flight found nothing waiting and started a second request for the same screen. Full price for the user, double load for the server.
The store now holds the promise, so a mid-flight click awaits the request already running and inherits whatever head start the hover bought. That is why 600 ms of hover was enough to turn a 3,605 ms click into 1,014 ms.
Why answering an iframe navigation is safe here
The fetch handler otherwise refuses to answer iframe navigations, and that guard stays. The hazard it exists for (#171) is the worker re-fetching an iframe request: Chrome then sends
Sec-Fetch-Dest: empty, the server's chromeless detection falls through, and the entire desktop renders inside a window.A speculative document is never re-fetched. It is fetched once, ahead of time, from a URL the shell built with
openstation_chromeless=1present — and the server checks that query flag before it consults Sec-Fetch. So the held bytes are already correctly chromeless, and the fallback path (speculative fetch failed → fetch normally) is safe for exactly the same reason.Safety
action,action2,_wpnonce,nonceordelete_allis refused, so a hover can never activate a plugin, empty a trash, or apply an update./wp-admin/only, and the chromeless flag must be present.windowPrewarmEnabled), delivered to the worker through the sameself.__OS_SW_CONFIGpreamble the asset cache uses. Off by default; zero behaviour change until a user opts in.pointerType === 'mouse') — touch has no hover, so there is no intent to read.Testing
npm run typecheck,lint,lint:php,buildgreen. Full suites: 4,972 JS, 2,495 PHP. New coverage for the config flag's independent parsing insw-policy.test.ts. Verified live on openstation.blog throughout: correct screens rendered (Writing Settings,Media Settings,Discussion Settings,Permalink Settingsall confirmed by heading), forms intact, zero console errors.🤖 Generated with Claude Code