From 0498decfb0805beee15cd1cffe2e26c097a77a54 Mon Sep 17 00:00:00 2001 From: Scott Pouliot Date: Tue, 16 Jun 2026 20:24:47 -0400 Subject: [PATCH] Fix quote/job create page jumping to bottom on fresh load The wizard scroll-restore saved scroll position on form submit but never cleared it if the server redirected to a success page. Next fresh visit to the same URL found the stale sessionStorage key and jumped down. Fix: track whether the page unload was caused by our own form submit. On pagehide for any other reason (nav link, success redirect), remove the key so it never fires on a clean page load. Co-Authored-By: Claude Sonnet 4.6 --- src/PowderCoating.Web/wwwroot/js/item-wizard.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/PowderCoating.Web/wwwroot/js/item-wizard.js b/src/PowderCoating.Web/wwwroot/js/item-wizard.js index cc83999..86fdf46 100644 --- a/src/PowderCoating.Web/wwwroot/js/item-wizard.js +++ b/src/PowderCoating.Web/wwwroot/js/item-wizard.js @@ -91,13 +91,23 @@ document.addEventListener('DOMContentLoaded', () => { ownerForm.addEventListener('submit', writeHiddenFields, { capture: true }); // Save scroll position before the form causes a full-page reload so we can - // restore it after the server redirects back to this page. Key is path-specific - // so navigating away and back doesn't restore a stale position. + // restore it after the server redirects back to this page on a validation error. + // Key is path-specific; cleared on pagehide unless we're leaving via a submit so + // a fresh navigation to this page never restores a stale position. const scrollKey = 'wizardScrollY:' + location.pathname; + let wizardSubmitting = false; ownerForm.addEventListener('submit', () => { + wizardSubmitting = true; sessionStorage.setItem(scrollKey, String(Math.round(window.scrollY))); }, { capture: true }); + // If the page unloads for any reason other than our own form submit (e.g. the + // user clicks a nav link or the server redirects to a success page), discard the + // saved position so it doesn't fire on the next fresh visit. + window.addEventListener('pagehide', () => { + if (!wizardSubmitting) sessionStorage.removeItem(scrollKey); + }); + // Restore on load — fire after layout is painted so scrollTo lands correctly. const savedY = sessionStorage.getItem(scrollKey); if (savedY !== null) {