[{"data":1,"prerenderedAt":1676},["ShallowReactive",2],{"blog-unsaved-changes-nuxt":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"type":11,"body":12,"_type":1669,"_id":1670,"_source":1671,"_file":1672,"_stem":1673,"_extension":1674,"sitemap":1675},"/blog/unsaved-changes-nuxt","blog",false,"","Warn Users Before They Lose Unsaved Changes in Nuxt","One composable that covers in-app links, the back button, reloads, and closing the tab, without nagging users who already saved.","2026-12-03","code",{"type":13,"children":14,"toc":1667},"root",[15,26,35,44,90,98,107,824,887,895,903,1227,1243,1251,1272,1395,1403,1411,1556,1564,1624,1632,1648,1661],{"type":16,"tag":17,"props":18,"children":19},"element","content-h1",{},[20],{"type":16,"tag":21,"props":22,"children":23},"p",{},[24],{"type":25,"value":8},"text",{"type":16,"tag":27,"props":28,"children":29},"sub-title",{},[30],{"type":16,"tag":21,"props":31,"children":32},{},[33],{"type":25,"value":34},"One composable that covers in-app links, the back button, reloads, and closing the tab, without nagging users who already saved",{"type":16,"tag":36,"props":37,"children":38},"paragraph",{},[39],{"type":16,"tag":21,"props":40,"children":41},{},[42],{"type":25,"value":43},"Someone spends ten minutes on a settings form, clicks a link in the sidebar, and everything they entered is gone. A \"you have unsaved changes\" warning prevents that, but most examples online only handle half the problem. A user can leave a page in two very different ways:",{"type":16,"tag":45,"props":46,"children":47},"indent",{},[48],{"type":16,"tag":49,"props":50,"children":51},"ul",{},[52,72],{"type":16,"tag":53,"props":54,"children":55},"li",{},[56,62,64,70],{"type":16,"tag":57,"props":58,"children":59},"strong",{},[60],{"type":25,"value":61},"Inside your app",{"type":25,"value":63},": clicking a link, calling ",{"type":16,"tag":11,"props":65,"children":67},{"className":66},[],[68],{"type":25,"value":69},"navigateTo()",{"type":25,"value":71},", or pressing the browser's back button. Vue Router handles these, so a route guard can stop them.",{"type":16,"tag":53,"props":73,"children":74},{},[75,80,82,88],{"type":16,"tag":57,"props":76,"children":77},{},[78],{"type":25,"value":79},"Leaving the page entirely",{"type":25,"value":81},": closing the tab, reloading, or typing a new URL. The router never sees these. Only the browser's ",{"type":16,"tag":11,"props":83,"children":85},{"className":84},[],[86],{"type":25,"value":87},"beforeunload",{"type":25,"value":89}," event can.",{"type":16,"tag":36,"props":91,"children":92},{},[93],{"type":16,"tag":21,"props":94,"children":95},{},[96],{"type":25,"value":97},"A reliable guard needs both, and it needs to stay quiet once the user has saved.",{"type":16,"tag":99,"props":100,"children":101},"content-h2",{},[102],{"type":16,"tag":21,"props":103,"children":104},{},[105],{"type":25,"value":106},"The composable",{"type":16,"tag":108,"props":109,"children":110},"code-block",{},[111],{"type":16,"tag":112,"props":113,"children":117},"pre",{"className":114,"code":115,"language":116,"meta":7,"style":7},"language-ts shiki shiki-themes github-dark","// composables/useUnsavedChanges.ts\nimport { onBeforeUnmount, onMounted, toValue, watch, type MaybeRefOrGetter } from 'vue'\nimport { onBeforeRouteLeave } from 'vue-router'\n\nexport function useUnsavedChanges(\n  isDirty: MaybeRefOrGetter\u003Cboolean>,\n  confirmLeave: () => boolean | Promise\u003Cboolean> = () =>\n    window.confirm('You have unsaved changes. Leave this page anyway?')\n) {\n  // 1. In-app navigation: links, navigateTo(), back/forward buttons\n  onBeforeRouteLeave(async () => {\n    if (!toValue(isDirty)) return true\n    return await confirmLeave()\n  })\n\n  // 2. Closing the tab, reloading, or leaving the site\n  const onBeforeUnload = (event: BeforeUnloadEvent) => {\n    event.preventDefault()\n    event.returnValue = '' // older Chrome and Edge need this too\n  }\n\n  // Only listen while there are unsaved changes\n  onMounted(() => {\n    watch(() => toValue(isDirty), (dirty) => {\n      if (dirty) window.addEventListener('beforeunload', onBeforeUnload)\n      else window.removeEventListener('beforeunload', onBeforeUnload)\n    }, { immediate: true })\n  })\n\n  onBeforeUnmount(() => window.removeEventListener('beforeunload', onBeforeUnload))\n}\n","ts",[118],{"type":16,"tag":11,"props":119,"children":120},{"__ignoreMap":7},[121,133,170,192,202,227,263,328,357,366,375,406,445,469,478,486,495,545,563,586,595,603,612,634,678,711,742,761,769,777,815],{"type":16,"tag":122,"props":123,"children":126},"span",{"class":124,"line":125},"line",1,[127],{"type":16,"tag":122,"props":128,"children":130},{"style":129},"--shiki-default:#6A737D",[131],{"type":25,"value":132},"// composables/useUnsavedChanges.ts\n",{"type":16,"tag":122,"props":134,"children":136},{"class":124,"line":135},2,[137,143,149,154,159,164],{"type":16,"tag":122,"props":138,"children":140},{"style":139},"--shiki-default:#F97583",[141],{"type":25,"value":142},"import",{"type":16,"tag":122,"props":144,"children":146},{"style":145},"--shiki-default:#E1E4E8",[147],{"type":25,"value":148}," { onBeforeUnmount, onMounted, toValue, watch, ",{"type":16,"tag":122,"props":150,"children":151},{"style":139},[152],{"type":25,"value":153},"type",{"type":16,"tag":122,"props":155,"children":156},{"style":145},[157],{"type":25,"value":158}," MaybeRefOrGetter } ",{"type":16,"tag":122,"props":160,"children":161},{"style":139},[162],{"type":25,"value":163},"from",{"type":16,"tag":122,"props":165,"children":167},{"style":166},"--shiki-default:#9ECBFF",[168],{"type":25,"value":169}," 'vue'\n",{"type":16,"tag":122,"props":171,"children":173},{"class":124,"line":172},3,[174,178,183,187],{"type":16,"tag":122,"props":175,"children":176},{"style":139},[177],{"type":25,"value":142},{"type":16,"tag":122,"props":179,"children":180},{"style":145},[181],{"type":25,"value":182}," { onBeforeRouteLeave } ",{"type":16,"tag":122,"props":184,"children":185},{"style":139},[186],{"type":25,"value":163},{"type":16,"tag":122,"props":188,"children":189},{"style":166},[190],{"type":25,"value":191}," 'vue-router'\n",{"type":16,"tag":122,"props":193,"children":195},{"class":124,"line":194},4,[196],{"type":16,"tag":122,"props":197,"children":199},{"emptyLinePlaceholder":198},true,[200],{"type":25,"value":201},"\n",{"type":16,"tag":122,"props":203,"children":205},{"class":124,"line":204},5,[206,211,216,222],{"type":16,"tag":122,"props":207,"children":208},{"style":139},[209],{"type":25,"value":210},"export",{"type":16,"tag":122,"props":212,"children":213},{"style":139},[214],{"type":25,"value":215}," function",{"type":16,"tag":122,"props":217,"children":219},{"style":218},"--shiki-default:#B392F0",[220],{"type":25,"value":221}," useUnsavedChanges",{"type":16,"tag":122,"props":223,"children":224},{"style":145},[225],{"type":25,"value":226},"(\n",{"type":16,"tag":122,"props":228,"children":230},{"class":124,"line":229},6,[231,237,242,247,252,258],{"type":16,"tag":122,"props":232,"children":234},{"style":233},"--shiki-default:#FFAB70",[235],{"type":25,"value":236},"  isDirty",{"type":16,"tag":122,"props":238,"children":239},{"style":139},[240],{"type":25,"value":241},":",{"type":16,"tag":122,"props":243,"children":244},{"style":218},[245],{"type":25,"value":246}," MaybeRefOrGetter",{"type":16,"tag":122,"props":248,"children":249},{"style":145},[250],{"type":25,"value":251},"\u003C",{"type":16,"tag":122,"props":253,"children":255},{"style":254},"--shiki-default:#79B8FF",[256],{"type":25,"value":257},"boolean",{"type":16,"tag":122,"props":259,"children":260},{"style":145},[261],{"type":25,"value":262},">,\n",{"type":16,"tag":122,"props":264,"children":266},{"class":124,"line":265},7,[267,272,276,281,286,291,296,301,305,309,314,319,323],{"type":16,"tag":122,"props":268,"children":269},{"style":218},[270],{"type":25,"value":271},"  confirmLeave",{"type":16,"tag":122,"props":273,"children":274},{"style":139},[275],{"type":25,"value":241},{"type":16,"tag":122,"props":277,"children":278},{"style":145},[279],{"type":25,"value":280}," () ",{"type":16,"tag":122,"props":282,"children":283},{"style":139},[284],{"type":25,"value":285},"=>",{"type":16,"tag":122,"props":287,"children":288},{"style":254},[289],{"type":25,"value":290}," boolean",{"type":16,"tag":122,"props":292,"children":293},{"style":139},[294],{"type":25,"value":295}," |",{"type":16,"tag":122,"props":297,"children":298},{"style":218},[299],{"type":25,"value":300}," Promise",{"type":16,"tag":122,"props":302,"children":303},{"style":145},[304],{"type":25,"value":251},{"type":16,"tag":122,"props":306,"children":307},{"style":254},[308],{"type":25,"value":257},{"type":16,"tag":122,"props":310,"children":311},{"style":145},[312],{"type":25,"value":313},"> ",{"type":16,"tag":122,"props":315,"children":316},{"style":139},[317],{"type":25,"value":318},"=",{"type":16,"tag":122,"props":320,"children":321},{"style":145},[322],{"type":25,"value":280},{"type":16,"tag":122,"props":324,"children":325},{"style":139},[326],{"type":25,"value":327},"=>\n",{"type":16,"tag":122,"props":329,"children":331},{"class":124,"line":330},8,[332,337,342,347,352],{"type":16,"tag":122,"props":333,"children":334},{"style":145},[335],{"type":25,"value":336},"    window.",{"type":16,"tag":122,"props":338,"children":339},{"style":218},[340],{"type":25,"value":341},"confirm",{"type":16,"tag":122,"props":343,"children":344},{"style":145},[345],{"type":25,"value":346},"(",{"type":16,"tag":122,"props":348,"children":349},{"style":166},[350],{"type":25,"value":351},"'You have unsaved changes. Leave this page anyway?'",{"type":16,"tag":122,"props":353,"children":354},{"style":145},[355],{"type":25,"value":356},")\n",{"type":16,"tag":122,"props":358,"children":360},{"class":124,"line":359},9,[361],{"type":16,"tag":122,"props":362,"children":363},{"style":145},[364],{"type":25,"value":365},") {\n",{"type":16,"tag":122,"props":367,"children":369},{"class":124,"line":368},10,[370],{"type":16,"tag":122,"props":371,"children":372},{"style":129},[373],{"type":25,"value":374},"  // 1. In-app navigation: links, navigateTo(), back/forward buttons\n",{"type":16,"tag":122,"props":376,"children":378},{"class":124,"line":377},11,[379,384,388,393,397,401],{"type":16,"tag":122,"props":380,"children":381},{"style":218},[382],{"type":25,"value":383},"  onBeforeRouteLeave",{"type":16,"tag":122,"props":385,"children":386},{"style":145},[387],{"type":25,"value":346},{"type":16,"tag":122,"props":389,"children":390},{"style":139},[391],{"type":25,"value":392},"async",{"type":16,"tag":122,"props":394,"children":395},{"style":145},[396],{"type":25,"value":280},{"type":16,"tag":122,"props":398,"children":399},{"style":139},[400],{"type":25,"value":285},{"type":16,"tag":122,"props":402,"children":403},{"style":145},[404],{"type":25,"value":405}," {\n",{"type":16,"tag":122,"props":407,"children":409},{"class":124,"line":408},12,[410,415,420,425,430,435,440],{"type":16,"tag":122,"props":411,"children":412},{"style":139},[413],{"type":25,"value":414},"    if",{"type":16,"tag":122,"props":416,"children":417},{"style":145},[418],{"type":25,"value":419}," (",{"type":16,"tag":122,"props":421,"children":422},{"style":139},[423],{"type":25,"value":424},"!",{"type":16,"tag":122,"props":426,"children":427},{"style":218},[428],{"type":25,"value":429},"toValue",{"type":16,"tag":122,"props":431,"children":432},{"style":145},[433],{"type":25,"value":434},"(isDirty)) ",{"type":16,"tag":122,"props":436,"children":437},{"style":139},[438],{"type":25,"value":439},"return",{"type":16,"tag":122,"props":441,"children":442},{"style":254},[443],{"type":25,"value":444}," true\n",{"type":16,"tag":122,"props":446,"children":448},{"class":124,"line":447},13,[449,454,459,464],{"type":16,"tag":122,"props":450,"children":451},{"style":139},[452],{"type":25,"value":453},"    return",{"type":16,"tag":122,"props":455,"children":456},{"style":139},[457],{"type":25,"value":458}," await",{"type":16,"tag":122,"props":460,"children":461},{"style":218},[462],{"type":25,"value":463}," confirmLeave",{"type":16,"tag":122,"props":465,"children":466},{"style":145},[467],{"type":25,"value":468},"()\n",{"type":16,"tag":122,"props":470,"children":472},{"class":124,"line":471},14,[473],{"type":16,"tag":122,"props":474,"children":475},{"style":145},[476],{"type":25,"value":477},"  })\n",{"type":16,"tag":122,"props":479,"children":481},{"class":124,"line":480},15,[482],{"type":16,"tag":122,"props":483,"children":484},{"emptyLinePlaceholder":198},[485],{"type":25,"value":201},{"type":16,"tag":122,"props":487,"children":489},{"class":124,"line":488},16,[490],{"type":16,"tag":122,"props":491,"children":492},{"style":129},[493],{"type":25,"value":494},"  // 2. Closing the tab, reloading, or leaving the site\n",{"type":16,"tag":122,"props":496,"children":498},{"class":124,"line":497},17,[499,504,509,514,518,523,527,532,537,541],{"type":16,"tag":122,"props":500,"children":501},{"style":139},[502],{"type":25,"value":503},"  const",{"type":16,"tag":122,"props":505,"children":506},{"style":218},[507],{"type":25,"value":508}," onBeforeUnload",{"type":16,"tag":122,"props":510,"children":511},{"style":139},[512],{"type":25,"value":513}," =",{"type":16,"tag":122,"props":515,"children":516},{"style":145},[517],{"type":25,"value":419},{"type":16,"tag":122,"props":519,"children":520},{"style":233},[521],{"type":25,"value":522},"event",{"type":16,"tag":122,"props":524,"children":525},{"style":139},[526],{"type":25,"value":241},{"type":16,"tag":122,"props":528,"children":529},{"style":218},[530],{"type":25,"value":531}," BeforeUnloadEvent",{"type":16,"tag":122,"props":533,"children":534},{"style":145},[535],{"type":25,"value":536},") ",{"type":16,"tag":122,"props":538,"children":539},{"style":139},[540],{"type":25,"value":285},{"type":16,"tag":122,"props":542,"children":543},{"style":145},[544],{"type":25,"value":405},{"type":16,"tag":122,"props":546,"children":548},{"class":124,"line":547},18,[549,554,559],{"type":16,"tag":122,"props":550,"children":551},{"style":145},[552],{"type":25,"value":553},"    event.",{"type":16,"tag":122,"props":555,"children":556},{"style":218},[557],{"type":25,"value":558},"preventDefault",{"type":16,"tag":122,"props":560,"children":561},{"style":145},[562],{"type":25,"value":468},{"type":16,"tag":122,"props":564,"children":566},{"class":124,"line":565},19,[567,572,576,581],{"type":16,"tag":122,"props":568,"children":569},{"style":145},[570],{"type":25,"value":571},"    event.returnValue ",{"type":16,"tag":122,"props":573,"children":574},{"style":139},[575],{"type":25,"value":318},{"type":16,"tag":122,"props":577,"children":578},{"style":166},[579],{"type":25,"value":580}," ''",{"type":16,"tag":122,"props":582,"children":583},{"style":129},[584],{"type":25,"value":585}," // older Chrome and Edge need this too\n",{"type":16,"tag":122,"props":587,"children":589},{"class":124,"line":588},20,[590],{"type":16,"tag":122,"props":591,"children":592},{"style":145},[593],{"type":25,"value":594},"  }\n",{"type":16,"tag":122,"props":596,"children":598},{"class":124,"line":597},21,[599],{"type":16,"tag":122,"props":600,"children":601},{"emptyLinePlaceholder":198},[602],{"type":25,"value":201},{"type":16,"tag":122,"props":604,"children":606},{"class":124,"line":605},22,[607],{"type":16,"tag":122,"props":608,"children":609},{"style":129},[610],{"type":25,"value":611},"  // Only listen while there are unsaved changes\n",{"type":16,"tag":122,"props":613,"children":615},{"class":124,"line":614},23,[616,621,626,630],{"type":16,"tag":122,"props":617,"children":618},{"style":218},[619],{"type":25,"value":620},"  onMounted",{"type":16,"tag":122,"props":622,"children":623},{"style":145},[624],{"type":25,"value":625},"(() ",{"type":16,"tag":122,"props":627,"children":628},{"style":139},[629],{"type":25,"value":285},{"type":16,"tag":122,"props":631,"children":632},{"style":145},[633],{"type":25,"value":405},{"type":16,"tag":122,"props":635,"children":637},{"class":124,"line":636},24,[638,643,647,651,656,661,666,670,674],{"type":16,"tag":122,"props":639,"children":640},{"style":218},[641],{"type":25,"value":642},"    watch",{"type":16,"tag":122,"props":644,"children":645},{"style":145},[646],{"type":25,"value":625},{"type":16,"tag":122,"props":648,"children":649},{"style":139},[650],{"type":25,"value":285},{"type":16,"tag":122,"props":652,"children":653},{"style":218},[654],{"type":25,"value":655}," toValue",{"type":16,"tag":122,"props":657,"children":658},{"style":145},[659],{"type":25,"value":660},"(isDirty), (",{"type":16,"tag":122,"props":662,"children":663},{"style":233},[664],{"type":25,"value":665},"dirty",{"type":16,"tag":122,"props":667,"children":668},{"style":145},[669],{"type":25,"value":536},{"type":16,"tag":122,"props":671,"children":672},{"style":139},[673],{"type":25,"value":285},{"type":16,"tag":122,"props":675,"children":676},{"style":145},[677],{"type":25,"value":405},{"type":16,"tag":122,"props":679,"children":681},{"class":124,"line":680},25,[682,687,692,697,701,706],{"type":16,"tag":122,"props":683,"children":684},{"style":139},[685],{"type":25,"value":686},"      if",{"type":16,"tag":122,"props":688,"children":689},{"style":145},[690],{"type":25,"value":691}," (dirty) window.",{"type":16,"tag":122,"props":693,"children":694},{"style":218},[695],{"type":25,"value":696},"addEventListener",{"type":16,"tag":122,"props":698,"children":699},{"style":145},[700],{"type":25,"value":346},{"type":16,"tag":122,"props":702,"children":703},{"style":166},[704],{"type":25,"value":705},"'beforeunload'",{"type":16,"tag":122,"props":707,"children":708},{"style":145},[709],{"type":25,"value":710},", onBeforeUnload)\n",{"type":16,"tag":122,"props":712,"children":714},{"class":124,"line":713},26,[715,720,725,730,734,738],{"type":16,"tag":122,"props":716,"children":717},{"style":139},[718],{"type":25,"value":719},"      else",{"type":16,"tag":122,"props":721,"children":722},{"style":145},[723],{"type":25,"value":724}," window.",{"type":16,"tag":122,"props":726,"children":727},{"style":218},[728],{"type":25,"value":729},"removeEventListener",{"type":16,"tag":122,"props":731,"children":732},{"style":145},[733],{"type":25,"value":346},{"type":16,"tag":122,"props":735,"children":736},{"style":166},[737],{"type":25,"value":705},{"type":16,"tag":122,"props":739,"children":740},{"style":145},[741],{"type":25,"value":710},{"type":16,"tag":122,"props":743,"children":745},{"class":124,"line":744},27,[746,751,756],{"type":16,"tag":122,"props":747,"children":748},{"style":145},[749],{"type":25,"value":750},"    }, { immediate: ",{"type":16,"tag":122,"props":752,"children":753},{"style":254},[754],{"type":25,"value":755},"true",{"type":16,"tag":122,"props":757,"children":758},{"style":145},[759],{"type":25,"value":760}," })\n",{"type":16,"tag":122,"props":762,"children":764},{"class":124,"line":763},28,[765],{"type":16,"tag":122,"props":766,"children":767},{"style":145},[768],{"type":25,"value":477},{"type":16,"tag":122,"props":770,"children":772},{"class":124,"line":771},29,[773],{"type":16,"tag":122,"props":774,"children":775},{"emptyLinePlaceholder":198},[776],{"type":25,"value":201},{"type":16,"tag":122,"props":778,"children":780},{"class":124,"line":779},30,[781,786,790,794,798,802,806,810],{"type":16,"tag":122,"props":782,"children":783},{"style":218},[784],{"type":25,"value":785},"  onBeforeUnmount",{"type":16,"tag":122,"props":787,"children":788},{"style":145},[789],{"type":25,"value":625},{"type":16,"tag":122,"props":791,"children":792},{"style":139},[793],{"type":25,"value":285},{"type":16,"tag":122,"props":795,"children":796},{"style":145},[797],{"type":25,"value":724},{"type":16,"tag":122,"props":799,"children":800},{"style":218},[801],{"type":25,"value":729},{"type":16,"tag":122,"props":803,"children":804},{"style":145},[805],{"type":25,"value":346},{"type":16,"tag":122,"props":807,"children":808},{"style":166},[809],{"type":25,"value":705},{"type":16,"tag":122,"props":811,"children":812},{"style":145},[813],{"type":25,"value":814},", onBeforeUnload))\n",{"type":16,"tag":122,"props":816,"children":818},{"class":124,"line":817},31,[819],{"type":16,"tag":122,"props":820,"children":821},{"style":145},[822],{"type":25,"value":823},"}\n",{"type":16,"tag":825,"props":826,"children":827},"code-description",{},[828],{"type":16,"tag":21,"props":829,"children":830},{},[831,833,839,841,847,849,854,856,862,864,869,871,877,879,885],{"type":25,"value":832},"Returning ",{"type":16,"tag":11,"props":834,"children":836},{"className":835},[],[837],{"type":25,"value":838},"false",{"type":25,"value":840}," from ",{"type":16,"tag":11,"props":842,"children":844},{"className":843},[],[845],{"type":25,"value":846},"onBeforeRouteLeave",{"type":25,"value":848}," cancels the navigation, and the guard can be ",{"type":16,"tag":11,"props":850,"children":852},{"className":851},[],[853],{"type":25,"value":392},{"type":25,"value":855},", so ",{"type":16,"tag":11,"props":857,"children":859},{"className":858},[],[860],{"type":25,"value":861},"confirmLeave",{"type":25,"value":863}," can return a promise. The ",{"type":16,"tag":11,"props":865,"children":867},{"className":866},[],[868],{"type":25,"value":87},{"type":25,"value":870}," listener is added only while the form is dirty: a permanent listener can stop the browser from using its back/forward cache, which makes the back button slower on every page that uses it. The watcher starts in ",{"type":16,"tag":11,"props":872,"children":874},{"className":873},[],[875],{"type":25,"value":876},"onMounted",{"type":25,"value":878}," because ",{"type":16,"tag":11,"props":880,"children":882},{"className":881},[],[883],{"type":25,"value":884},"window",{"type":25,"value":886}," doesn't exist during server rendering.",{"type":16,"tag":99,"props":888,"children":889},{},[890],{"type":16,"tag":21,"props":891,"children":892},{},[893],{"type":25,"value":894},"Knowing when the form is dirty",{"type":16,"tag":36,"props":896,"children":897},{},[898],{"type":16,"tag":21,"props":899,"children":900},{},[901],{"type":25,"value":902},"The simplest reliable check is to compare the form to a snapshot of what was last saved:",{"type":16,"tag":108,"props":904,"children":905},{},[906],{"type":16,"tag":112,"props":907,"children":909},{"className":114,"code":908,"language":116,"meta":7,"style":7},"const form = reactive({ name: '', email: '', notifications: true })\n\nconst saved = ref(JSON.stringify(form))\nconst isDirty = computed(() => JSON.stringify(form) !== saved.value)\n\nasync function save() {\n  await $fetch('/api/settings', { method: 'PUT', body: form })\n  saved.value = JSON.stringify(form)   // mark clean *before* navigating\n  await navigateTo('/dashboard')\n}\n\nuseUnsavedChanges(isDirty)\n",[910],{"type":16,"tag":11,"props":911,"children":912},{"__ignoreMap":7},[913,967,974,1019,1076,1083,1104,1141,1175,1200,1207,1214],{"type":16,"tag":122,"props":914,"children":915},{"class":124,"line":125},[916,921,926,930,935,940,945,950,954,959,963],{"type":16,"tag":122,"props":917,"children":918},{"style":139},[919],{"type":25,"value":920},"const",{"type":16,"tag":122,"props":922,"children":923},{"style":254},[924],{"type":25,"value":925}," form",{"type":16,"tag":122,"props":927,"children":928},{"style":139},[929],{"type":25,"value":513},{"type":16,"tag":122,"props":931,"children":932},{"style":218},[933],{"type":25,"value":934}," reactive",{"type":16,"tag":122,"props":936,"children":937},{"style":145},[938],{"type":25,"value":939},"({ name: ",{"type":16,"tag":122,"props":941,"children":942},{"style":166},[943],{"type":25,"value":944},"''",{"type":16,"tag":122,"props":946,"children":947},{"style":145},[948],{"type":25,"value":949},", email: ",{"type":16,"tag":122,"props":951,"children":952},{"style":166},[953],{"type":25,"value":944},{"type":16,"tag":122,"props":955,"children":956},{"style":145},[957],{"type":25,"value":958},", notifications: ",{"type":16,"tag":122,"props":960,"children":961},{"style":254},[962],{"type":25,"value":755},{"type":16,"tag":122,"props":964,"children":965},{"style":145},[966],{"type":25,"value":760},{"type":16,"tag":122,"props":968,"children":969},{"class":124,"line":135},[970],{"type":16,"tag":122,"props":971,"children":972},{"emptyLinePlaceholder":198},[973],{"type":25,"value":201},{"type":16,"tag":122,"props":975,"children":976},{"class":124,"line":172},[977,981,986,990,995,999,1004,1009,1014],{"type":16,"tag":122,"props":978,"children":979},{"style":139},[980],{"type":25,"value":920},{"type":16,"tag":122,"props":982,"children":983},{"style":254},[984],{"type":25,"value":985}," saved",{"type":16,"tag":122,"props":987,"children":988},{"style":139},[989],{"type":25,"value":513},{"type":16,"tag":122,"props":991,"children":992},{"style":218},[993],{"type":25,"value":994}," ref",{"type":16,"tag":122,"props":996,"children":997},{"style":145},[998],{"type":25,"value":346},{"type":16,"tag":122,"props":1000,"children":1001},{"style":254},[1002],{"type":25,"value":1003},"JSON",{"type":16,"tag":122,"props":1005,"children":1006},{"style":145},[1007],{"type":25,"value":1008},".",{"type":16,"tag":122,"props":1010,"children":1011},{"style":218},[1012],{"type":25,"value":1013},"stringify",{"type":16,"tag":122,"props":1015,"children":1016},{"style":145},[1017],{"type":25,"value":1018},"(form))\n",{"type":16,"tag":122,"props":1020,"children":1021},{"class":124,"line":194},[1022,1026,1031,1035,1040,1044,1048,1053,1057,1061,1066,1071],{"type":16,"tag":122,"props":1023,"children":1024},{"style":139},[1025],{"type":25,"value":920},{"type":16,"tag":122,"props":1027,"children":1028},{"style":254},[1029],{"type":25,"value":1030}," isDirty",{"type":16,"tag":122,"props":1032,"children":1033},{"style":139},[1034],{"type":25,"value":513},{"type":16,"tag":122,"props":1036,"children":1037},{"style":218},[1038],{"type":25,"value":1039}," computed",{"type":16,"tag":122,"props":1041,"children":1042},{"style":145},[1043],{"type":25,"value":625},{"type":16,"tag":122,"props":1045,"children":1046},{"style":139},[1047],{"type":25,"value":285},{"type":16,"tag":122,"props":1049,"children":1050},{"style":254},[1051],{"type":25,"value":1052}," JSON",{"type":16,"tag":122,"props":1054,"children":1055},{"style":145},[1056],{"type":25,"value":1008},{"type":16,"tag":122,"props":1058,"children":1059},{"style":218},[1060],{"type":25,"value":1013},{"type":16,"tag":122,"props":1062,"children":1063},{"style":145},[1064],{"type":25,"value":1065},"(form) ",{"type":16,"tag":122,"props":1067,"children":1068},{"style":139},[1069],{"type":25,"value":1070},"!==",{"type":16,"tag":122,"props":1072,"children":1073},{"style":145},[1074],{"type":25,"value":1075}," saved.value)\n",{"type":16,"tag":122,"props":1077,"children":1078},{"class":124,"line":204},[1079],{"type":16,"tag":122,"props":1080,"children":1081},{"emptyLinePlaceholder":198},[1082],{"type":25,"value":201},{"type":16,"tag":122,"props":1084,"children":1085},{"class":124,"line":229},[1086,1090,1094,1099],{"type":16,"tag":122,"props":1087,"children":1088},{"style":139},[1089],{"type":25,"value":392},{"type":16,"tag":122,"props":1091,"children":1092},{"style":139},[1093],{"type":25,"value":215},{"type":16,"tag":122,"props":1095,"children":1096},{"style":218},[1097],{"type":25,"value":1098}," save",{"type":16,"tag":122,"props":1100,"children":1101},{"style":145},[1102],{"type":25,"value":1103},"() {\n",{"type":16,"tag":122,"props":1105,"children":1106},{"class":124,"line":265},[1107,1112,1117,1121,1126,1131,1136],{"type":16,"tag":122,"props":1108,"children":1109},{"style":139},[1110],{"type":25,"value":1111},"  await",{"type":16,"tag":122,"props":1113,"children":1114},{"style":218},[1115],{"type":25,"value":1116}," $fetch",{"type":16,"tag":122,"props":1118,"children":1119},{"style":145},[1120],{"type":25,"value":346},{"type":16,"tag":122,"props":1122,"children":1123},{"style":166},[1124],{"type":25,"value":1125},"'/api/settings'",{"type":16,"tag":122,"props":1127,"children":1128},{"style":145},[1129],{"type":25,"value":1130},", { method: ",{"type":16,"tag":122,"props":1132,"children":1133},{"style":166},[1134],{"type":25,"value":1135},"'PUT'",{"type":16,"tag":122,"props":1137,"children":1138},{"style":145},[1139],{"type":25,"value":1140},", body: form })\n",{"type":16,"tag":122,"props":1142,"children":1143},{"class":124,"line":330},[1144,1149,1153,1157,1161,1165,1170],{"type":16,"tag":122,"props":1145,"children":1146},{"style":145},[1147],{"type":25,"value":1148},"  saved.value ",{"type":16,"tag":122,"props":1150,"children":1151},{"style":139},[1152],{"type":25,"value":318},{"type":16,"tag":122,"props":1154,"children":1155},{"style":254},[1156],{"type":25,"value":1052},{"type":16,"tag":122,"props":1158,"children":1159},{"style":145},[1160],{"type":25,"value":1008},{"type":16,"tag":122,"props":1162,"children":1163},{"style":218},[1164],{"type":25,"value":1013},{"type":16,"tag":122,"props":1166,"children":1167},{"style":145},[1168],{"type":25,"value":1169},"(form)   ",{"type":16,"tag":122,"props":1171,"children":1172},{"style":129},[1173],{"type":25,"value":1174},"// mark clean *before* navigating\n",{"type":16,"tag":122,"props":1176,"children":1177},{"class":124,"line":359},[1178,1182,1187,1191,1196],{"type":16,"tag":122,"props":1179,"children":1180},{"style":139},[1181],{"type":25,"value":1111},{"type":16,"tag":122,"props":1183,"children":1184},{"style":218},[1185],{"type":25,"value":1186}," navigateTo",{"type":16,"tag":122,"props":1188,"children":1189},{"style":145},[1190],{"type":25,"value":346},{"type":16,"tag":122,"props":1192,"children":1193},{"style":166},[1194],{"type":25,"value":1195},"'/dashboard'",{"type":16,"tag":122,"props":1197,"children":1198},{"style":145},[1199],{"type":25,"value":356},{"type":16,"tag":122,"props":1201,"children":1202},{"class":124,"line":368},[1203],{"type":16,"tag":122,"props":1204,"children":1205},{"style":145},[1206],{"type":25,"value":823},{"type":16,"tag":122,"props":1208,"children":1209},{"class":124,"line":377},[1210],{"type":16,"tag":122,"props":1211,"children":1212},{"emptyLinePlaceholder":198},[1213],{"type":25,"value":201},{"type":16,"tag":122,"props":1215,"children":1216},{"class":124,"line":408},[1217,1222],{"type":16,"tag":122,"props":1218,"children":1219},{"style":218},[1220],{"type":25,"value":1221},"useUnsavedChanges",{"type":16,"tag":122,"props":1223,"children":1224},{"style":145},[1225],{"type":25,"value":1226},"(isDirty)\n",{"type":16,"tag":825,"props":1228,"children":1229},{},[1230],{"type":16,"tag":21,"props":1231,"children":1232},{},[1233,1235,1241],{"type":25,"value":1234},"Comparing against a snapshot, instead of flipping a flag on every input, means that typing something and then deleting it counts as clean again. If the form loads its data asynchronously, update ",{"type":16,"tag":11,"props":1236,"children":1238},{"className":1237},[],[1239],{"type":25,"value":1240},"saved",{"type":25,"value":1242}," once the data arrives, or the form will look dirty before the user has touched it.",{"type":16,"tag":99,"props":1244,"children":1245},{},[1246],{"type":16,"tag":21,"props":1247,"children":1248},{},[1249],{"type":25,"value":1250},"Using your own dialog",{"type":16,"tag":36,"props":1252,"children":1253},{},[1254],{"type":16,"tag":21,"props":1255,"children":1256},{},[1257,1263,1265,1270],{"type":16,"tag":11,"props":1258,"children":1260},{"className":1259},[],[1261],{"type":25,"value":1262},"window.confirm",{"type":25,"value":1264}," works, but it's unstyled and blocks the page. Because the guard awaits ",{"type":16,"tag":11,"props":1266,"children":1268},{"className":1267},[],[1269],{"type":25,"value":861},{"type":25,"value":1271},", any modal that returns a promise can replace it:",{"type":16,"tag":108,"props":1273,"children":1274},{},[1275],{"type":16,"tag":112,"props":1276,"children":1278},{"className":114,"code":1277,"language":116,"meta":7,"style":7},"useUnsavedChanges(isDirty, () =>\n  openConfirmDialog({            // your modal, resolving to true or false\n    title: 'Discard changes?',\n    message: \"Your changes to this page haven't been saved.\",\n    confirmText: 'Discard',\n    cancelText: 'Keep editing'\n  })\n)\n",[1279],{"type":16,"tag":11,"props":1280,"children":1281},{"__ignoreMap":7},[1282,1298,1316,1334,1351,1368,1381,1388],{"type":16,"tag":122,"props":1283,"children":1284},{"class":124,"line":125},[1285,1289,1294],{"type":16,"tag":122,"props":1286,"children":1287},{"style":218},[1288],{"type":25,"value":1221},{"type":16,"tag":122,"props":1290,"children":1291},{"style":145},[1292],{"type":25,"value":1293},"(isDirty, () ",{"type":16,"tag":122,"props":1295,"children":1296},{"style":139},[1297],{"type":25,"value":327},{"type":16,"tag":122,"props":1299,"children":1300},{"class":124,"line":135},[1301,1306,1311],{"type":16,"tag":122,"props":1302,"children":1303},{"style":218},[1304],{"type":25,"value":1305},"  openConfirmDialog",{"type":16,"tag":122,"props":1307,"children":1308},{"style":145},[1309],{"type":25,"value":1310},"({            ",{"type":16,"tag":122,"props":1312,"children":1313},{"style":129},[1314],{"type":25,"value":1315},"// your modal, resolving to true or false\n",{"type":16,"tag":122,"props":1317,"children":1318},{"class":124,"line":172},[1319,1324,1329],{"type":16,"tag":122,"props":1320,"children":1321},{"style":145},[1322],{"type":25,"value":1323},"    title: ",{"type":16,"tag":122,"props":1325,"children":1326},{"style":166},[1327],{"type":25,"value":1328},"'Discard changes?'",{"type":16,"tag":122,"props":1330,"children":1331},{"style":145},[1332],{"type":25,"value":1333},",\n",{"type":16,"tag":122,"props":1335,"children":1336},{"class":124,"line":194},[1337,1342,1347],{"type":16,"tag":122,"props":1338,"children":1339},{"style":145},[1340],{"type":25,"value":1341},"    message: ",{"type":16,"tag":122,"props":1343,"children":1344},{"style":166},[1345],{"type":25,"value":1346},"\"Your changes to this page haven't been saved.\"",{"type":16,"tag":122,"props":1348,"children":1349},{"style":145},[1350],{"type":25,"value":1333},{"type":16,"tag":122,"props":1352,"children":1353},{"class":124,"line":204},[1354,1359,1364],{"type":16,"tag":122,"props":1355,"children":1356},{"style":145},[1357],{"type":25,"value":1358},"    confirmText: ",{"type":16,"tag":122,"props":1360,"children":1361},{"style":166},[1362],{"type":25,"value":1363},"'Discard'",{"type":16,"tag":122,"props":1365,"children":1366},{"style":145},[1367],{"type":25,"value":1333},{"type":16,"tag":122,"props":1369,"children":1370},{"class":124,"line":229},[1371,1376],{"type":16,"tag":122,"props":1372,"children":1373},{"style":145},[1374],{"type":25,"value":1375},"    cancelText: ",{"type":16,"tag":122,"props":1377,"children":1378},{"style":166},[1379],{"type":25,"value":1380},"'Keep editing'\n",{"type":16,"tag":122,"props":1382,"children":1383},{"class":124,"line":265},[1384],{"type":16,"tag":122,"props":1385,"children":1386},{"style":145},[1387],{"type":25,"value":477},{"type":16,"tag":122,"props":1389,"children":1390},{"class":124,"line":330},[1391],{"type":16,"tag":122,"props":1392,"children":1393},{"style":145},[1394],{"type":25,"value":356},{"type":16,"tag":825,"props":1396,"children":1397},{},[1398],{"type":16,"tag":21,"props":1399,"children":1400},{},[1401],{"type":25,"value":1402},"Name the buttons after what they do. \"Discard\" and \"Keep editing\" are clearer than \"OK\" and \"Cancel\", where it's never obvious which one loses the work.",{"type":16,"tag":99,"props":1404,"children":1405},{},[1406],{"type":16,"tag":21,"props":1407,"children":1408},{},[1409],{"type":25,"value":1410},"Gotchas",{"type":16,"tag":45,"props":1412,"children":1413},{},[1414],{"type":16,"tag":49,"props":1415,"children":1416},{},[1417,1434,1451,1483,1524],{"type":16,"tag":53,"props":1418,"children":1419},{},[1420,1432],{"type":16,"tag":57,"props":1421,"children":1422},{},[1423,1425,1430],{"type":25,"value":1424},"Browsers ignore your ",{"type":16,"tag":11,"props":1426,"children":1428},{"className":1427},[],[1429],{"type":25,"value":87},{"type":25,"value":1431}," message.",{"type":25,"value":1433}," Modern browsers always show their own generic \"Leave site?\" dialog when the tab closes or reloads. Only in-app navigation can use your wording or your modal.",{"type":16,"tag":53,"props":1435,"children":1436},{},[1437,1442,1444,1449],{"type":16,"tag":57,"props":1438,"children":1439},{},[1440],{"type":25,"value":1441},"No dialog without interaction.",{"type":25,"value":1443}," Browsers only show the ",{"type":16,"tag":11,"props":1445,"children":1447},{"className":1446},[],[1448],{"type":25,"value":87},{"type":25,"value":1450}," dialog if the user has interacted with the page. In practice that's fine: if they haven't typed anything, there's nothing to lose.",{"type":16,"tag":53,"props":1452,"children":1453},{},[1454,1459,1461,1467,1469,1474,1476,1481],{"type":16,"tag":57,"props":1455,"children":1456},{},[1457],{"type":25,"value":1458},"Reset the snapshot before you navigate.",{"type":25,"value":1460}," If ",{"type":16,"tag":11,"props":1462,"children":1464},{"className":1463},[],[1465],{"type":25,"value":1466},"save()",{"type":25,"value":1468}," calls ",{"type":16,"tag":11,"props":1470,"children":1472},{"className":1471},[],[1473],{"type":25,"value":69},{"type":25,"value":1475}," before updating ",{"type":16,"tag":11,"props":1477,"children":1479},{"className":1478},[],[1480],{"type":25,"value":1240},{"type":25,"value":1482},", your own guard asks the user whether they want to lose the changes they just saved.",{"type":16,"tag":53,"props":1484,"children":1485},{},[1486,1491,1493,1499,1501,1507,1509,1514,1516,1522],{"type":16,"tag":57,"props":1487,"children":1488},{},[1489],{"type":25,"value":1490},"Same-route changes aren't covered.",{"type":25,"value":1492}," Going from ",{"type":16,"tag":11,"props":1494,"children":1496},{"className":1495},[],[1497],{"type":25,"value":1498},"/settings?tab=profile",{"type":25,"value":1500}," to ",{"type":16,"tag":11,"props":1502,"children":1504},{"className":1503},[],[1505],{"type":25,"value":1506},"/settings?tab=billing",{"type":25,"value":1508}," reuses the same page component, so ",{"type":16,"tag":11,"props":1510,"children":1512},{"className":1511},[],[1513],{"type":25,"value":846},{"type":25,"value":1515}," doesn't run. Use ",{"type":16,"tag":11,"props":1517,"children":1519},{"className":1518},[],[1520],{"type":25,"value":1521},"onBeforeRouteUpdate",{"type":25,"value":1523}," with the same check if your tabs are query parameters.",{"type":16,"tag":53,"props":1525,"children":1526},{},[1527,1532,1534,1539,1541,1547,1549,1555],{"type":16,"tag":57,"props":1528,"children":1529},{},[1530],{"type":25,"value":1531},"Call it from a page, or a component inside one.",{"type":25,"value":1533}," ",{"type":16,"tag":11,"props":1535,"children":1537},{"className":1536},[],[1538],{"type":25,"value":846},{"type":25,"value":1540}," only works in components rendered by ",{"type":16,"tag":11,"props":1542,"children":1544},{"className":1543},[],[1545],{"type":25,"value":1546},"\u003CNuxtPage>",{"type":25,"value":1548},". It won't fire from a layout or ",{"type":16,"tag":11,"props":1550,"children":1552},{"className":1551},[],[1553],{"type":25,"value":1554},"app.vue",{"type":25,"value":1008},{"type":16,"tag":99,"props":1557,"children":1558},{},[1559],{"type":16,"tag":21,"props":1560,"children":1561},{},[1562],{"type":25,"value":1563},"Lessons learned",{"type":16,"tag":45,"props":1565,"children":1566},{},[1567],{"type":16,"tag":49,"props":1568,"children":1569},{},[1570,1587,1597,1614],{"type":16,"tag":53,"props":1571,"children":1572},{},[1573,1578,1580,1585],{"type":16,"tag":57,"props":1574,"children":1575},{},[1576],{"type":25,"value":1577},"Cover both exits.",{"type":25,"value":1579}," A route guard alone misses reloads and closed tabs; ",{"type":16,"tag":11,"props":1581,"children":1583},{"className":1582},[],[1584],{"type":25,"value":87},{"type":25,"value":1586}," alone misses every in-app link.",{"type":16,"tag":53,"props":1588,"children":1589},{},[1590,1595],{"type":16,"tag":57,"props":1591,"children":1592},{},[1593],{"type":25,"value":1594},"Track dirty state by comparison.",{"type":25,"value":1596}," A snapshot of the last save is simpler and more accurate than tracking every edit.",{"type":16,"tag":53,"props":1598,"children":1599},{},[1600,1605,1607,1612],{"type":16,"tag":57,"props":1601,"children":1602},{},[1603],{"type":25,"value":1604},"Only listen when it matters.",{"type":25,"value":1606}," Adding ",{"type":16,"tag":11,"props":1608,"children":1610},{"className":1609},[],[1611],{"type":25,"value":87},{"type":25,"value":1613}," only while the form is dirty keeps the rest of the app fast.",{"type":16,"tag":53,"props":1615,"children":1616},{},[1617,1622],{"type":16,"tag":57,"props":1618,"children":1619},{},[1620],{"type":25,"value":1621},"Never warn about work that's already saved.",{"type":25,"value":1623}," A guard that fires after a successful save teaches users to click through it without reading.",{"type":16,"tag":99,"props":1625,"children":1626},{},[1627],{"type":16,"tag":21,"props":1628,"children":1629},{},[1630],{"type":25,"value":1631},"Conclusion",{"type":16,"tag":36,"props":1633,"children":1634},{},[1635],{"type":16,"tag":21,"props":1636,"children":1637},{},[1638,1640,1646],{"type":25,"value":1639},"About 30 lines of composable protect every form in an app from the most frustrating kind of data loss. Drop ",{"type":16,"tag":11,"props":1641,"children":1643},{"className":1642},[],[1644],{"type":25,"value":1645},"useUnsavedChanges(isDirty)",{"type":25,"value":1647}," into any page with a form, and users get a warning exactly when they need one and never when they don't.",{"type":16,"tag":36,"props":1649,"children":1650},{},[1651],{"type":16,"tag":21,"props":1652,"children":1653},{},[1654,1656,1660],{"type":25,"value":1655},"Let me know if you found this useful. Feel free to reach out to me with any questions on ",{"type":16,"tag":1657,"props":1658,"children":1659},"linkedin-link",{},[],{"type":25,"value":1008},{"type":16,"tag":1662,"props":1663,"children":1664},"style",{},[1665],{"type":25,"value":1666},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":135,"depth":135,"links":1668},[],"markdown","content:blog:unsaved-changes-nuxt.md","content","blog/unsaved-changes-nuxt.md","blog/unsaved-changes-nuxt","md",{"loc":4},1796288161051]