-
Notifications
You must be signed in to change notification settings - Fork 69
chore: add guarded on-device levers to A/B the iOS back-swipe blank #1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
vivek7405
wants to merge
3
commits into
main
Choose a base branch
from
fix/ios-back-swipe-snapshot-ab
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
packages/core/test/routing/browser/nav-swipe-ab-levers.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| /** | ||
| * Real-browser test for #1428: the guarded paint-timing levers that let a real | ||
| * iPhone decide what actually fixes the back-swipe blank. | ||
| * | ||
| * #1410 moved the `pushState` ahead of the DOM mutation and the blank survived | ||
| * on-device, which leaves one assumption behind that fix untested: that WebKit | ||
| * binds the back-forward gesture snapshot synchronously, at the `pushState` | ||
| * call. If it instead captures the compositing surface when the | ||
| * `didSameDocumentNavigation` IPC lands in the UI process, that happens after | ||
| * the whole push-swap-scroll task has run, and reordering inside that task | ||
| * changes nothing a device can see. | ||
| * | ||
| * The gesture preview is iOS-only and cannot be asserted anywhere but a real | ||
| * iPhone. What CAN be asserted in any browser is the thing the levers change: | ||
| * whether a FRAME BOUNDARY falls between the push and the mutation. That is the | ||
| * whole mechanism under test, so it is what these pin, in both directions. | ||
| * | ||
| * The default direction matters as much as the lever direction. These sit on | ||
| * the live navigation path, so an app that opts into nothing must run exactly | ||
| * the timing it ran before, and only a test that fails when the lever leaks can | ||
| * say so. | ||
| */ | ||
| import { enableClientRouter, navigate } from '../../../src/router-client.js'; | ||
|
|
||
| import { assert } from '../../../../../test/browser-assert.js'; | ||
| import { installNavGuard } from '../../../../../test/browser-nav-guard.js'; | ||
|
|
||
| const OUTGOING_SENTINEL = 'outgoing-page-1428'; | ||
| const INCOMING_SENTINEL = 'incoming-page-1428'; | ||
| const SCROLL_TO = 800; | ||
|
|
||
| suite('Client router: back-swipe A/B levers (#1428)', () => { | ||
| let origFetch, origPushState, navGuard, origHref, observer, seq, pushes; | ||
|
|
||
| /** @param {Record<string, boolean> | null} levers */ | ||
| function setup(levers) { | ||
| origHref = location.href; | ||
| enableClientRouter(); | ||
| navGuard = installNavGuard(); | ||
|
|
||
| if (levers) window.__webjsDiag = levers; | ||
| else delete window.__webjsDiag; | ||
|
|
||
| // A genuinely TALL outgoing document against a SHORT destination, the shape | ||
| // the defect was measured on: the engine really clamps the offset, so the | ||
| // #1410 guarantee is being re-checked against real layout rather than a | ||
| // stub. | ||
| document.body.innerHTML = | ||
| `<!--wj:children:/:/-->${OUTGOING_SENTINEL}<div style="height:3000px"></div><!--/wj:children:/-->`; | ||
| window.scrollTo({ left: 0, top: SCROLL_TO, behavior: 'instant' }); | ||
|
|
||
| // One ordered trace of the three events the levers reorder. `frame` is | ||
| // requested from INSIDE the push wrapper, so it is queued ahead of any | ||
| // frame the router itself requests afterwards and therefore fires first | ||
| // within that frame. That ordering is what makes `frame` before `mutate` | ||
| // mean "the router waited for a frame" rather than "some frame elapsed". | ||
| seq = []; | ||
| pushes = []; | ||
| origPushState = history.pushState; | ||
| history.pushState = function (...args) { | ||
| seq.push('push'); | ||
| pushes.push({ text: document.body.textContent || '', scrollY: window.scrollY }); | ||
| requestAnimationFrame(() => seq.push('frame')); | ||
| return origPushState.apply(this, args); | ||
| }; | ||
|
|
||
| // A MutationObserver callback is a microtask, so a synchronous swap records | ||
| // `mutate` before the next frame can run, which is exactly the distinction | ||
| // being drawn. | ||
| observer = new MutationObserver(() => { | ||
| if (!seq.includes('mutate')) seq.push('mutate'); | ||
| }); | ||
| observer.observe(document.body, { childList: true, subtree: true }); | ||
|
|
||
| origFetch = window.fetch; | ||
| window.fetch = () => Promise.resolve(new Response( | ||
| '<!doctype html><html><head></head><body>' | ||
| + `<!--wj:children:/:/-->${INCOMING_SENTINEL}<!--/wj:children:/--></body></html>`, | ||
| { headers: { 'content-type': 'text/html', 'x-webjs-build': '' } }, | ||
| )); | ||
| } | ||
|
|
||
| function teardown() { | ||
| if (observer) observer.disconnect(); | ||
| window.fetch = origFetch; | ||
| history.pushState = origPushState; | ||
| delete window.__webjsDiag; | ||
| if (navGuard) navGuard.remove(); | ||
| document.body.innerHTML = ''; | ||
| window.scrollTo({ left: 0, top: 0, behavior: 'instant' }); | ||
| history.replaceState(null, '', origHref); | ||
| } | ||
|
|
||
| /** Let any pending frame callback land, so the trace is complete. */ | ||
| function settleFrames() { | ||
| return new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => r(undefined)))); | ||
| } | ||
|
|
||
| test('default: no lever set, so the swap still happens in the push\'s own task', async () => { | ||
| setup(null); | ||
| try { | ||
| await navigate(location.origin + '/swipe-ab-default'); | ||
| await settleFrames(); | ||
|
|
||
| assert.equal(navGuard.hardNavigations.length, 0, | ||
| `the swap must not degrade here (fallbacks: ${JSON.stringify(navGuard.fallbacks)})`); | ||
| assert.deepEqual(seq, ['push', 'mutate', 'frame'], | ||
| `with no lever the mutation lands before any frame boundary (got ${JSON.stringify(seq)})`); | ||
| } finally { teardown(); } | ||
| }); | ||
|
|
||
| test('?raf: a frame boundary falls between the push and the mutation', async () => { | ||
| setup({ raf: true }); | ||
| try { | ||
| await navigate(location.origin + '/swipe-ab-raf'); | ||
| await settleFrames(); | ||
|
|
||
| assert.equal(navGuard.hardNavigations.length, 0, | ||
| `the swap must not degrade here (fallbacks: ${JSON.stringify(navGuard.fallbacks)})`); | ||
| assert.deepEqual(seq, ['push', 'frame', 'mutate'], | ||
| `the lever must paint a frame between the push and the swap (got ${JSON.stringify(seq)})`); | ||
| } finally { teardown(); } | ||
| }); | ||
|
|
||
| test('?raf2: same ordering, and the entry is still recorded exactly once', async () => { | ||
| setup({ raf2: true }); | ||
| try { | ||
| await navigate(location.origin + '/swipe-ab-raf2'); | ||
| await settleFrames(); | ||
|
|
||
| assert.deepEqual(seq, ['push', 'frame', 'mutate'], | ||
| `the double-frame lever must also swap after a frame (got ${JSON.stringify(seq)})`); | ||
| // The lever calls the one-shot thunk itself and `applySwap` calls it | ||
| // again at its commit point, so the guard is the only thing between this | ||
| // and a duplicated history entry. | ||
| assert.equal(pushes.length, 1, 'exactly one pushState per navigation under the lever'); | ||
| } finally { teardown(); } | ||
| }); | ||
|
|
||
| test('?scrolllast: the scroll-to-top lands after a frame, not in the swap\'s task', async () => { | ||
| setup({ scrolllast: true }); | ||
| const scrolls = []; | ||
| const origScrollTo = window.scrollTo; | ||
| try { | ||
| // Recorded rather than suppressed: the lever is about WHEN the write | ||
| // happens, so the write still has to happen. | ||
| window.scrollTo = function (...args) { scrolls.push(seq.slice()); return origScrollTo.apply(this, args); }; | ||
| await navigate(location.origin + '/swipe-ab-scrolllast'); | ||
| const beforeFrame = scrolls.length; | ||
| await settleFrames(); | ||
|
|
||
| assert.equal(beforeFrame, 0, | ||
| `the deferred scroll must not have run yet when the navigation resolved (ran ${beforeFrame} times)`); | ||
| assert.equal(scrolls.length, 1, 'the scroll still runs, one frame later'); | ||
| assert.ok(scrolls[0].includes('frame'), | ||
| `the scroll landed after a frame boundary (trace at the write: ${JSON.stringify(scrolls[0])})`); | ||
| } finally { window.scrollTo = origScrollTo; teardown(); } | ||
| }); | ||
|
|
||
| test('?scrolllast: a superseded navigation does not scroll the page that replaced it', async () => { | ||
| setup({ scrolllast: true }); | ||
| const scrolls = []; | ||
| const origScrollTo = window.scrollTo; | ||
| try { | ||
| window.scrollTo = function (...args) { scrolls.push('scroll'); return origScrollTo.apply(this, args); }; | ||
| await navigate(location.origin + '/swipe-ab-superseded'); | ||
|
|
||
| // Start a second navigation inside the deferred scroll's frame gap. It | ||
| // bumps the nav token and then does nothing else, which isolates the | ||
| // guard from anything the newer navigation would itself have done to | ||
| // scroll. | ||
| // | ||
| // Aborted rather than left pending. A fetch that never settles leaves a | ||
| // navigation in flight for the rest of the page's life, holding the | ||
| // router's token and its own frame state, and a test that never cleans | ||
| // that up is a leak looking for somewhere to surface. An AbortError is | ||
| // the shape the router already treats as a superseded navigation, so it | ||
| // settles down the path it would take in production. | ||
| let abortPending; | ||
| window.fetch = () => new Promise((_, reject) => { abortPending = reject; }); | ||
| const superseder = navigate(location.origin + '/swipe-ab-superseder'); | ||
| await settleFrames(); | ||
| if (abortPending) abortPending(new DOMException('aborted', 'AbortError')); | ||
| await superseder.catch(() => {}); | ||
|
|
||
| // Without the token guard the first navigation's scroll fires here, into | ||
| // a document a newer navigation already owns. | ||
| assert.equal(scrolls.length, 0, | ||
| 'the superseded navigation abandoned its deferred scroll'); | ||
| } finally { window.scrollTo = origScrollTo; teardown(); } | ||
| }); | ||
|
|
||
| test('?raf: the #1410 guarantee survives, the outgoing page is still live at the push', async () => { | ||
| setup({ raf: true }); | ||
| try { | ||
| await navigate(location.origin + '/swipe-ab-raf-state'); | ||
| await settleFrames(); | ||
|
|
||
| assert.equal(pushes.length, 1, 'the navigation recorded exactly one history entry'); | ||
| const at = pushes[0]; | ||
| // The lever moves the push EARLIER (ahead of the yield), so what #1410 | ||
| // pinned has to still hold: a lever that fixed the frame timing by | ||
| // sacrificing the recorded state would be a regression wearing a fix. | ||
| assert.ok(at.text.includes(OUTGOING_SENTINEL), | ||
| 'the outgoing page is still in the DOM at the pushState call'); | ||
| assert.ok(!at.text.includes(INCOMING_SENTINEL), | ||
| 'the incoming page has NOT been swapped in yet at the pushState call'); | ||
| assert.ok(at.scrollY >= SCROLL_TO - 5, | ||
| `the outgoing scroll offset is still live at the pushState call (expected about ${SCROLL_TO}, got ${at.scrollY})`); | ||
| } finally { teardown(); } | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.