From 458d848b21bafea5c8bec242ef1b494240bf4fe1 Mon Sep 17 00:00:00 2001 From: Harenome Ranaivoarivony-Razanajato Date: Thu, 19 Nov 2026 11:48:17 +0100 Subject: [PATCH] feat(web): show a page skeleton while the next page loads Slow pages left the old page on screen with no sign a click was taken, so it was clicked again. A same-tab link or form that navigates the document now replaces the main content with a pulsing skeleton after 180 ms; the header and rail stay. Modified, middle, new-tab, download, hash-only and other-origin links, and forms a script handles, do not show it; a page restored from the back-forward cache, a cancelled leave and Escape take it away. The .skel blocks are reusable. --- app/static/page-loading.js | 58 +++++++++++++++++++ app/static/style.css | 12 ++++ app/templates/base.html | 1 + app/ui_tests/test_page_loading.py | 92 +++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 app/static/page-loading.js create mode 100644 app/ui_tests/test_page_loading.py diff --git a/app/static/page-loading.js b/app/static/page-loading.js new file mode 100644 index 00000000..24351546 --- /dev/null +++ b/app/static/page-loading.js @@ -0,0 +1,58 @@ +// A skeleton over the page while the next one loads: shown only when the document itself navigates, +// a moment after the click or submit, so a fast page never flashes it. The next page replaces it. +(() => { + const main = document.getElementById('main-content'); + if (!main) return; + const status = document.createElement('span'); + status.className = 'sr'; + status.setAttribute('role', 'status'); + document.body.append(status); + let timer = 0, skeleton = null; + const html = '
' + + '
' + + '
'.repeat(5); + function show() { + skeleton = document.createElement('div'); + skeleton.className = 'page-skeleton'; + skeleton.setAttribute('aria-hidden', 'true'); + skeleton.innerHTML = html; + main.prepend(skeleton); + main.setAttribute('aria-busy', 'true'); + status.textContent = 'Loading'; + } + function hide() { + clearTimeout(timer); + skeleton?.remove(); + skeleton = null; + main.removeAttribute('aria-busy'); + status.textContent = ''; + } + // Whether going to `url` in this tab replaces this document: same origin, and not a jump within it. + function leaves(url, target) { + if (target && target !== '_self') return false; + let to; + try { to = new URL(url, location.href); } catch { return false; } + if (to.origin !== location.origin) return false; + return !(to.hash && to.pathname === location.pathname && to.search === location.search); + } + // Checked when the delay is up, so a handler anywhere that prevented the default has had its turn. + function later(event) { + hide(); + timer = setTimeout(() => { if (!event.defaultPrevented) show(); }, 180); + } + addEventListener('click', event => { + if (event.button || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; + const a = event.target.closest?.('a[href]'); + if (a && !a.hasAttribute('download') && leaves(a.href, a.target)) later(event); + }); + addEventListener('submit', event => { + // Attributes, not properties: a field named "action" or "target" shadows the form's own. + const form = event.target, by = event.submitter, attr = name => by?.getAttribute('form' + name) || form.getAttribute(name); + if ((attr('method') || '').toLowerCase() === 'dialog') return; + if (leaves(attr('action') || location.href, attr('target'))) later(event); + }); + // A page that asks before leaving: whether the user stays or goes, this navigation shows no skeleton. + addEventListener('beforeunload', event => setTimeout(() => { if (event.defaultPrevented || event.returnValue) hide(); })); + addEventListener('keydown', event => { if (event.key === 'Escape') hide(); }); // Escape stops the load + addEventListener('pageshow', event => { if (event.persisted) hide(); }); // back to this page, from the cache +})(); diff --git a/app/static/style.css b/app/static/style.css index 8509c3ab..9eb9201f 100644 --- a/app/static/style.css +++ b/app/static/style.css @@ -1874,3 +1874,15 @@ select.ui-select-native { display: none !important; } /* the judge's button under its badge, what its last call took beside it */ .judge-actions { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; margin: .6rem 0 1rem; } .judge-actions .header-form { margin: 0; } + +/* a placeholder while content loads (.skel, reusable); the whole page's, while the next page loads (page-loading.js) */ +.skel { background: var(--surface-2); border-radius: 6px; animation: skel-pulse 1.6s ease-in-out infinite; } +@keyframes skel-pulse { 50% { opacity: .45; } } +@media (prefers-reduced-motion: reduce) { .skel { animation: none; } } +main[aria-busy="true"] > :not(.page-skeleton) { display: none; } +.page-skeleton { display: grid; gap: .6rem; } +.skel-title { height: calc(var(--h1-size) * 1.22); width: min(20rem, 70%); } +.skel-line { height: .95rem; width: min(32rem, 90%); } +.skel-row { display: flex; flex-wrap: wrap; gap: .5rem; margin: .5rem 0 var(--head-gap); } +.skel-btn { height: var(--tool-h); width: 6.5rem; } +.skel-block { height: 3.75rem; } diff --git a/app/templates/base.html b/app/templates/base.html index 40ecbf6d..0e2964f2 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -247,6 +247,7 @@ +