From 128a24ccde5ed7f1ed7a30c03fd79047328c9ac0 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 7 Oct 2025 18:43:28 -0700 Subject: [PATCH] fix(admin): remove infinite loop in navigation guards MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed infinite loop caused by calling goto() inside beforeNavigate, which would trigger the same navigation guard again. The correct approach is to NOT cancel navigation, but simply await the autosave flush. SvelteKit's beforeNavigate accepts async callbacks, so navigation will naturally wait for the flush to complete before proceeding. Changes: - Removed navigation.cancel() calls - Removed goto() calls that created the loop - Simply await autoSave.flush() and let navigation proceed 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/lib/components/admin/EssayForm.svelte | 6 +----- src/lib/components/admin/PhotoPostForm.svelte | 6 +----- src/lib/components/admin/ProjectForm.svelte | 7 +------ src/lib/components/admin/SimplePostForm.svelte | 6 +----- src/routes/admin/posts/[id]/edit/+page.svelte | 7 +------ 5 files changed, 5 insertions(+), 27 deletions(-) diff --git a/src/lib/components/admin/EssayForm.svelte b/src/lib/components/admin/EssayForm.svelte index b009774..a0159e9 100644 --- a/src/lib/components/admin/EssayForm.svelte +++ b/src/lib/components/admin/EssayForm.svelte @@ -170,13 +170,9 @@ $effect(() => { if (autoSave.status === 'saved') { return } - navigation.cancel() + // Flush any pending changes before allowing navigation to proceed try { await autoSave.flush() - // Navigate to the intended destination after flush completes - if (navigation.to?.url) { - goto(navigation.to.url.pathname + navigation.to.url.search) - } } catch (error) { console.error('Autosave flush failed:', error) } diff --git a/src/lib/components/admin/PhotoPostForm.svelte b/src/lib/components/admin/PhotoPostForm.svelte index a1a2094..9584039 100644 --- a/src/lib/components/admin/PhotoPostForm.svelte +++ b/src/lib/components/admin/PhotoPostForm.svelte @@ -174,13 +174,9 @@ $effect(() => { if (autoSave.status === 'saved') { return } - navigation.cancel() + // Flush any pending changes before allowing navigation to proceed try { await autoSave.flush() - // Navigate to the intended destination after flush completes - if (navigation.to?.url) { - goto(navigation.to.url.pathname + navigation.to.url.search) - } } catch (error) { console.error('Autosave flush failed:', error) } diff --git a/src/lib/components/admin/ProjectForm.svelte b/src/lib/components/admin/ProjectForm.svelte index 5454b36..e68a425 100644 --- a/src/lib/components/admin/ProjectForm.svelte +++ b/src/lib/components/admin/ProjectForm.svelte @@ -185,14 +185,9 @@ return } - // Otherwise, flush any pending changes before navigating - navigation.cancel() + // Otherwise, flush any pending changes before allowing navigation to proceed try { await autoSave.flush() - // Navigate to the intended destination after flush completes - if (navigation.to?.url) { - goto(navigation.to.url.pathname + navigation.to.url.search) - } } catch (error) { console.error('Autosave flush failed:', error) toast.error('Failed to save changes') diff --git a/src/lib/components/admin/SimplePostForm.svelte b/src/lib/components/admin/SimplePostForm.svelte index 17481fb..6a5c8f0 100644 --- a/src/lib/components/admin/SimplePostForm.svelte +++ b/src/lib/components/admin/SimplePostForm.svelte @@ -175,13 +175,9 @@ beforeNavigate(async (navigation) => { if (autoSave.status === 'saved') { return } - navigation.cancel() + // Flush any pending changes before allowing navigation to proceed try { await autoSave.flush() - // Navigate to the intended destination after flush completes - if (navigation.to?.url) { - goto(navigation.to.url.pathname + navigation.to.url.search) - } } catch (error) { console.error('Autosave flush failed:', error) } diff --git a/src/routes/admin/posts/[id]/edit/+page.svelte b/src/routes/admin/posts/[id]/edit/+page.svelte index c55f934..ff816a7 100644 --- a/src/routes/admin/posts/[id]/edit/+page.svelte +++ b/src/routes/admin/posts/[id]/edit/+page.svelte @@ -386,14 +386,9 @@ onMount(async () => { return } - // Otherwise, flush any pending changes before navigating - navigation.cancel() + // Otherwise, flush any pending changes before allowing navigation to proceed try { await autoSave.flush() - // Navigate to the intended destination after flush completes - if (navigation.to?.url) { - goto(navigation.to.url.pathname + navigation.to.url.search) - } } catch (error) { console.error('Autosave flush failed:', error) }