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

@real-router/lifecycle-plugin

v0.4.2

Published

Route-level lifecycle hooks: onEnter, onStay, onLeave

Readme

@real-router/lifecycle-plugin

npm npm downloads bundle size License: MIT

Route-level lifecycle hooks for Real-Router. Add onNavigate, onEnter, onStay, onLeave callbacks directly to route definitions.

// Without plugin — scattered subscribe() calls with route checks:
router.subscribe(({ route, previousRoute }) => {
  if (route.name === "catalog") loadServices(route.params);
  if (previousRoute?.name === "editor") saveEditorState();
});

// With plugin — declarative, per-route:
{ name: "catalog", path: "/catalog?q&sort", onNavigate: () => (s) => loadServices(s.params) }
{ name: "editor", path: "/editor", onLeave: () => () => saveEditorState() }

Installation

npm install @real-router/lifecycle-plugin

Peer dependency: @real-router/core

Quick Start

import { createRouter } from "@real-router/core";
import { lifecyclePluginFactory } from "@real-router/lifecycle-plugin";

const routes = [
  {
    name: "services.catalog",
    path: "/catalog?q&sort&dir",
    // Fires on entry AND on param-change — recommended default
    onNavigate: () => (toState) => {
      loadServices(toState.params);
    },
  },
  {
    name: "chat",
    path: "/chat/:roomId",
    // Orthogonal: onEnter covers entry-only setup, onNavigate covers
    // every navigation (including entry). Both fire on entry.
    onEnter: () => (toState) => {
      chatSocket.connect(toState.params.roomId);
    },
    onNavigate: () => (toState) => {
      loadMessages(toState.params.roomId);
    },
    onLeave: () => () => {
      chatSocket.disconnect();
    },
  },
];

const router = createRouter(routes);
router.usePlugin(lifecyclePluginFactory());

await router.start("/");

Start with onNavigate. It covers the most common case — running the same logic whenever the route is the navigation target (data loading, analytics, UI reset). Add onEnter or onStay for extra case-specific logic.

Hook Reference

| Hook | Fires when | Typical use case | | ------------ | --------------------------------------- | ------------------------------------------- | | onNavigate | Any successful navigation to the route | Data loading, analytics, UI reset (default) | | onEnter | Route is entered | Entry-only setup (open socket, scroll top) | | onStay | Same route, params changed | Stay-only logic (incremental updates) | | onLeave | Route is left | Cleanup timers, save state |

Orthogonal dispatch: onEnter / onStay / onNavigate fire independently based on their own conditions. On entry, onEnter and onNavigate fire. On param-change, onStay and onNavigate fire. Each hook is composable — declaring one never silences another.

Each hook field is a factory function (router, getDependency) => (toState, fromState?) => void. The factory runs once per route; the returned callback is cached and invoked on each matching transition. When you don't need DI, omit the factory params:

// Without DI — ignore factory params:
onEnter: () => (toState) => { console.log("entered", toState.name); }

// With DI — access router and dependencies:
onEnter: (router, getDependency) => (toState) => {
  const analytics = getDependency("analytics");
  analytics.track("page_viewed", { route: toState.name });
}

Execution order

onLeave fires first (at leave-approve phase), then onEnter or onStay (at transition success).

Use Cases

Data loading (onNavigate — recommended default)

{
  name: "services.catalog",
  path: "/catalog?q&sort&dir",
  onNavigate: () => (toState) => {
    // Fires on entry from another route AND on filter/sort param changes
    loadServices(toState.params);
  },
}

Analytics tracking

{
  name: "product",
  path: "/products/:id",
  onEnter: () => (toState) => {
    analytics.track("product_viewed", { productId: toState.params.id });
  },
}

Cleanup on leave

{
  name: "editor",
  path: "/editor/:docId",
  onLeave: () => () => {
    autosaveTimer.clear();
    webSocket.disconnect();
  },
}

React to param changes

{
  name: "search",
  path: "/search?q",
  onStay: () => (toState) => {
    searchStore.setQuery(toState.params.q);
  },
}

Documentation

Related Packages

| Package | Description | | ---------------------------------------------------------------------------------------- | -------------------------------------- | | @real-router/core | Core router (required peer dependency) | | @real-router/browser-plugin | Browser History API integration | | @real-router/logger-plugin | Development logging |

License

MIT © Oleg Ivanov