From 672eb47143e930a003bd4b005340a3bfe8f0fb1a Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 7 Oct 2025 13:21:57 -0700 Subject: [PATCH] fix(admin): only block navigation/close when changes are unsaved MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update beforeNavigate guard to check autosave status before blocking - Allow instant navigation when status is 'saved' (no interruption) - Add beforeunload warning for browser close/reload - Only show warnings when status is NOT 'saved' (saving/error/idle) - Improves UX by not interrupting users when everything is already saved 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/lib/components/admin/ProjectForm.svelte | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lib/components/admin/ProjectForm.svelte b/src/lib/components/admin/ProjectForm.svelte index a58d77f..7af681b 100644 --- a/src/lib/components/admin/ProjectForm.svelte +++ b/src/lib/components/admin/ProjectForm.svelte @@ -177,9 +177,15 @@ } }) - // Navigation guard: flush autosave before navigating away + // Navigation guard: flush autosave before navigating away (only if there are unsaved changes) beforeNavigate(async (navigation) => { if (mode === 'edit' && hasLoaded && autoSave) { + // If status is 'saved', there are no unsaved changes - allow navigation + if (autoSave.status === 'saved') { + return + } + + // Otherwise, flush any pending changes before navigating navigation.cancel() try { await autoSave.flush() @@ -191,6 +197,22 @@ } }) + // Warn before closing browser tab/window if there are unsaved changes + $effect(() => { + if (mode !== 'edit' || !autoSave) return + + function handleBeforeUnload(event: BeforeUnloadEvent) { + // Only warn if there are unsaved changes + if (autoSave!.status !== 'saved') { + event.preventDefault() + event.returnValue = '' // Required for Chrome + } + } + + window.addEventListener('beforeunload', handleBeforeUnload) + return () => window.removeEventListener('beforeunload', handleBeforeUnload) + }) + // Keyboard shortcut: Cmd/Ctrl+S to save immediately $effect(() => { if (mode !== 'edit' || !autoSave) return