Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert for unsaved changes before navigation in Svelte

I have a Svelte application with form content. After a change in the form, I can trigger "unsaved change" alert when the page is refreshed. But after when i make a change in the form, if I try to navigate to another page with svelte navigation, I cannot handle unsaved change alert. How can I do this using Svelte Navigator?

like image 554
firstlord Avatar asked Sep 17 '25 10:09

firstlord


1 Answers

While his question was specifically asking about Svelte navigator, here's a solution for anyone using SvelteKit:

beforeNavigate(({ cancel }) => {
  if (dirty) {
    if (!confirm('Are you sure you want to leave this page? You have unsaved changes that will be lost.')) {
      cancel();
    }
  }
});

See the beforeNavigate docs for more information.

like image 156
tgordon18 Avatar answered Sep 19 '25 23:09

tgordon18