forge
Explore
Log in

Don't you dare go Hollow

The newest paint is the newest intent

Browse files at this commit

ee25951765f42b12dc76e8704e182b18c68bf40dAhmet Kaan GümüşParent: de54fef
  • fix
    • the newest paint is the newest intent
Show full message
🐛 fix(nav): the newest paint is the newest intent

A page arriving and then reverting to the one before it, a couple of hundred
milliseconds later. Every fetch that ends in a swap applied its answer
whenever it happened to land, and the live refresh fetches whatever URL the
reader was on when the timer fired: click a link inside that flight and the
refresh's answer — the old page, and a `replaceState` back to its URL —
lands on top of the page you just went to.

Each of those fetches now carries the value of a navigation counter
(`window.__izlekNav`) taken when it started, and drops its answer if the
counter has moved since. A navigation, a form post and an overlay close each
step it, so a second click also wins over a first one still in flight. The
live refresh steps nothing — it is not an intent, only an update of what is
already on screen — and lands only if nothing else has happened.

I could not make the bounce happen on demand in the harness, so this is the
mechanism fixed and not the symptom watched: in the captured request
timeline the refresh's GET goes out 150-390ms before the click's, which is
the overlap, but the two never landed in the order that reverts the page.
Ordinary navigation is unharmed across ~50 scripted click-through runs.
crates/izlek-web/src/layout.rsmodified+26 -5
@@ -201,6 +201,17 @@ pub async fn topbar_nav(cx: &Cx, active: NavPage, role: izlek_core::Role, lang:
201201 /// arrive with the swap because the redirect URL's query params rendered
202202 /// them server-side; `history.replaceState` keeps the address bar honest.
203203 ///
204+/// Every fetch that ends in a swap carries the navigation counter it started
205+/// under (`window.__izlekNav`), and its answer is thrown away if that counter
206+/// has moved. Without it the newest paint is not the newest intent: the live
207+/// refresh fetches the URL it was on, and a click landing during that flight
208+/// pushed a new page that the refresh's answer then overwrote — the page
209+/// arriving and reverting to the previous one two hundred milliseconds later.
210+/// Navigations, form posts and the overlay closes each step the counter, so a
211+/// second click also wins over a first one still in flight, and a refresh —
212+/// which is not an intent, only an update of what is already on screen —
213+/// steps nothing and lands only if nothing else has happened.
214+///
204215 /// The address bar and the `<html>` attributes are rewritten *before* the
205216 /// new body's scripts run, never after: a swapped-in script that reads
206217 /// `location.href` (the log-fit reload in `logs.rs`) would otherwise read
@@ -369,6 +380,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result {
369380 } \
370381 for (i = theirs.length; i < mine.length; i++) { mine[i].remove(); } \
371382 } \
383+ window.__izlekNav = 0; \
384+ function navStep() { return ++window.__izlekNav; } \
385+ function stillCurrent(n) { return window.__izlekNav === n; } \
372386 function swap(html, url, fresh, push, morphing) { \
373387 var keep = window.__izlekKeep ? captureFields() : null; \
374388 window.__izlekKeep = false; \
@@ -403,20 +417,23 @@ pub async fn soft_nav_script(cx: &Cx) -> Result {
403417 }); \
404418 } \
405419 window.__izlekGo = function (url) { \
420+ var n = navStep(); \
406421 fetch(url).then( \
407- function (r) { return r.text().then(function (t) { swap(t, r.url); }); }, \
422+ function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url); } }); }, \
408423 function () { window.location.href = url; } \
409424 ); \
410425 }; \
411426 window.__izlekRefresh = function () { \
427+ var n = window.__izlekNav; \
412428 fetch(window.location.href).then( \
413- function (r) { return r.text().then(function (t) { swap(t, r.url, false, false, true); }); }, \
429+ function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url, false, false, true); } }); }, \
414430 function () {} \
415431 ); \
416432 }; \
417433 window.__izlekPost = function (action, fields) { \
434+ var n = navStep(); \
418435 fetch(action, { method: 'POST', body: new URLSearchParams(fields) }).then( \
419- function (r) { return r.text().then(function (t) { swap(t, r.url); }); }, \
436+ function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url); } }); }, \
420437 function () {} \
421438 ); \
422439 }; \
@@ -424,6 +441,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result {
424441 var scrim = document.querySelector('.viewer-scrim'); \
425442 if (!scrim) { return false; } \
426443 var back = scrim.querySelector('.viewer-close').getAttribute('href'); \
444+ navStep(); \
427445 scrim.remove(); \
428446 sweepPanels(); \
429447 history.replaceState(null, '', back); \
@@ -434,6 +452,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result {
434452 window.__izlekCloseModal = function () { \
435453 var scrims = document.querySelectorAll('.modal-scrim'); \
436454 if (!scrims.length) { return false; } \
455+ navStep(); \
437456 scrims.forEach(function (el) { el.remove(); }); \
438457 sweepPanels(); \
439458 var u = new URL(window.location.href); \
@@ -455,8 +474,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result {
455474 e.preventDefault(); \
456475 var data = e.submitter ? new FormData(form, e.submitter) : new FormData(form); \
457476 var multipart = (form.getAttribute('enctype') || '').indexOf('multipart') !== -1; \
477+ var n = navStep(); \
458478 fetch(form.getAttribute('action'), { method: 'POST', body: multipart ? data : new URLSearchParams(data) }).then( \
459- function (r) { return r.text().then(function (t) { swap(t, r.url); }); }, \
479+ function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url); } }); }, \
460480 function () { form.submit(); } \
461481 ); \
462482 }, true); \
@@ -498,8 +518,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result {
498518 var href = link.getAttribute('href'); \
499519 if (!href || href.charAt(0) !== '/' || href.indexOf('/files/') === 0) { return; } \
500520 e.preventDefault(); \
521+ var n = navStep(); \
501522 fetch(href).then( \
502- function (r) { return r.text().then(function (t) { swap(t, r.url, true, true); }); }, \
523+ function (r) { return r.text().then(function (t) { if (stillCurrent(n)) { swap(t, r.url, true, true); } }); }, \
503524 function () { window.location.href = href; } \
504525 ); \
505526 }, true); \