npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vue-safeguard

v1.0.1

Published

Vue 3 component/plugin that warns before leaving a page with unsaved changes (beforeunload).

Readme

vue-safeguard

A tiny Vue 3 component/plugin that warns users before leaving a page with unsaved changes.
Uses the native beforeunload event. No DOM rendered; optional slot text becomes the message.

Note: Some browsers ignore custom text, but using event.preventDefault() and event.returnValue is the correct API.


Install

# choose one
npm i vue-safeguard
# or
pnpm add vue-safeguard
# or
yarn add vue-safeguard

Peer dependency: Vue ^3.3.0.


Quick Start

As a global plugin

// main.js
import { createApp } from 'vue';
import VueSafeguard from 'vue-safeguard';
import App from './App.vue';

const app = createApp(App);
app.use(VueSafeguard); // registers <safe-guard />
app.mount('#app');

Then in any component:

<template>
  <safe-guard :guard="isDirty">
    You have unsaved changes. Are you sure you want to leave?
  </safe-guard>

  <!-- your form/UI -->
</template>

<script>
export default {
  data() {
    return { isDirty: false };
  }
};
</script>

As a local component

import { SafeGuard } from 'vue-safeguard';

export default {
  components: { SafeGuard }
};

API

<safe-guard /> props

| Prop | Type | Default | Description | |-----------|-----------|-----------------------------------------------------------------------|-----------------------------------------------| | guard | Boolean | true | Toggle the guard on/off. | | message | String | 'You have unsaved changes. Are you sure you want to leave?' | Message used for beforeunload (see note). |

Slot: Optional. If provided, its text is used as the message (overrides message prop).

Example:

<safe-guard :guard="true">
  <!-- Nothing is rendered; the slot is only used as the source for the text -->
  You have unsaved changes!
</safe-guard>

How it works

  • Adds a capturing beforeunload listener on mounted.
  • If guard is true, calls event.preventDefault() and sets event.returnValue.
  • Removes the listener on unmounted.

Browser note: modern browsers may ignore custom strings and show a standard prompt instead. This is expected and per spec.


Patterns & Tips

  • Form dirty state: Set guard to true when any form field differs from its initial value.
  • Route changes: This component guards tab/window closes and reloads. For in-app navigation (Vue Router), use a route guard too if needed:
    // pseudo-example
    router.beforeEach((to, from, next) => {
      if (store.isDirty && !confirm('Unsaved changes. Leave?')) return next(false);
      next();
    });
  • SSR: No effect on the server; beforeunload runs in the browser only.

Import formats

  • ESM: import { SafeGuard } from 'vue-safeguard'
  • Plugin: import VueSafeguard from 'vue-safeguard' then app.use(VueSafeguard)

License

MIT © Rene Koch