2 hours ago · Tech · hide · 0 comments

Saving future me from past me.If you’re like me and using Astro + Sveltia CMS for your website, you might want to add this script. Last week, I lost all my changes from a draft after spending over 20 minutes writing it, and it was incredibly frustrating. Sveltia does have a restore option that appears when you reopen the page, but I find it a little finicky. I’d much rather have a warning before I accidentally close or refresh the page. I honestly thought this was already built in. diff --git a/src/pages/admin/index.astro b/src/pages/admin/index.astro index 443089b8..e7f27483 100644 --- a/src/pages/admin/index.astro +++ b/src/pages/admin/index.astro @@ -19,5 +19,26 @@ init({ config: cmsConfig }); </script> + + <script> + let isDirty = false; + + function isInEditor() { + const hash = location.hash; + return hash.includes('/entries/') || hash.endsWith('/new'); + } + + for (const event of ['input', 'change']) { + document.addEventListener(event, () => { + if (isInEditor()) isDirty =…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.