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 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-named-routes

v0.1.0

Published

Put enum names on next routes, to make programatic handling of routes easier.

Downloads

2,608

Readme

next-named-routes

A supporting library to NextJS 10+ that allows you to programatically handle routes in your application.

Does currently not support i18n routing – https://nextjs.org/docs/advanced-features/i18n-routing Does currently not support catch all routes – https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes

(PRs welcome)

Use case

This library should be used, if you find the need to for example iterate all routes in your application at run time, or detect which route is currently active.

As the default NextJS has no "names" for the routes you add, it is very tricky and hacky to make this work without some additional support this library provides. Use case here can be for example that you have dynamic links in your application to other pages, and would like to know which page in the route this corresponds to (to render it in a popover, as an example).

API

The library exports a single class NextNamedRoutes with the following methods:

In TypeScript, NextNamedRoutes takes a type argument that should be an enum of all routes in your application. This enum is referred to as RouteName below.

The following helper type is also exported by the library as return value from several functions:

type Route<RouteName> = {
  name: RouteName // Name of the routes, as provided to `add`
  identifiers: string[] // Any dynamic parts of the route. So the route /[threadId]/messages[messageId] will return ['threadId', 'messageId']
  path: string // The second input to `add`
  regex: RegExp // A regex, that can be used to match against `window.pathname` or a `href` starting with `/` to see if it leads to this route.
}
  • add(name: RouteName, path: string) – Register a name to the route referred to by path. path should be specified with the file system path to the route in NextJS, with a leading slash but without the file extension. For example, /[threadId]/messages/[messageId] is a valid path argument as long as the file /pages/[threadId]/messages/[messageId].ts exists. The method returns the this object so you can chain multiple calls.
  • activeRoute(): RouteName – Returns the RouteName of the route that is currently active. Very useful for highlighting the current route in the menu for example.
  • findRouteByName(name: RouteName): Route<RouteName> | undefined – Returns the metadata for a route given its name.
  • findRouteByPath(path: string): Route<RouteName> | undefined – Takes a path (of the form window.pathname) and returns the metadata for the matching route.
  • pathnameForParams(name: RouteName, params: { [key: string]: string }) – Returns a pathname suitable for passing as href to an a element for a route (or setting window.href = ...). The params argument is any dynamic parts of the path that should be injected.
  • routes: RouteName[] – List of all routes added to the router.

Possible extensions

  • Full i18n support as supported by Next: https://nextjs.org/docs/advanced-features/i18n-routing
  • Support catch all routes: https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes
  • On server side – detect against the file system that path actually maps to a valid NextJS path, else throw an error to prevent typing errors.