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

monke-universal-route

v1.1.3

Published

Universal monke.js Route

Downloads

1

Readme

Universal Next.js Route

Next.js is a fantastic Server-Side-Rendering framework for React, however one of the main issues community has with it is File-System based routing.

Universal Next.js Route strives to fix that by using Route objects for static, dynamic and absoloute paths. This library comes with custom Link, Router and middleware for creating a highly modular routing mechanism.

Full list of features, examples and API docs can be found below.

npm npm Travis (.org) Codecov

Installation

$ npm install next-universal-route

or

$ yarn add next-universal-route

Demo & Examples

Edit next-universal-route-demo

For fully featured demo check CodeSandbox or to get a quick peek take a look at example below.

Server-Side

// routes.js
const { Route } = require('next-universal-route');

const IndexRoute = new Route('/', 'index');
const PostRoute = new Route('/posts/:id/:slug', 'post';)

module.exports = {
  IndexRoute,
  PostRoute
};

// server.js
const express = require('express');
const next = require('next');
const { getRequestHandler } = require('next-universal-route');

const routes = require('./routes');
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handler = getRequestHandler(app, routes);

app.prepare().then(() => {
  express()
    .use(handler)
    .listen(3000);
});

Client-Side

// pages/index.js
import { Link } from 'next-universal-route';

import { IndexRoute, PostRoute } from '../routes';

<Link href={IndexRoute.generateUrl()}>
  <a>Index</a>
</Link>

<Link href={PostRoute.generateUrl({ id: 1, slug: 'first-post' })}>
  <a>Post</a>
</Link>

When using a custom server with a server file, for example called server.js, make sure you update the scripts key in package.json to:

{
  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
  }
}

Features

  • [x] Declaration of DRY and concise routes
  • [x] Two-way usage, works on both client and server
  • [x] Absolute, static and dynamic paths (using path-to-regexp)
  • [x] Opt-in routing system (via middleware)
  • [x] Automatic generation of both href and as urls
  • [x] Next.js Router replacement
  • [x] Next.js Link replacement
  • [x] Pass extra params to every page (support for tabs)
  • [x] Custom params and query string formatting
  • [ ] Rewrites
  • [ ] Redirects

 

API Docs

Route

Route.constructor(path: string, page?: string, urlFormatter?: Function): Route

Instantiates a Route object to be used throughout the application.

  • To create the route with absolute path, it needs to start with http

    • page can/should be ommited as it won't be used
    • urlFormatter can be ommited as it won't work on absolute paths
  • To create SPA friendly routes you have to pass relative paths.

    • path either be static or dynamic (using path-to-regexp)
    • page is required and it should correspond to page in pages/
      • it's possible to pass extra params using query strings syntax which can be accessed inside your page, but won't be shown to your user
    • urlFormatter is optional and it takes a function which will run on every given parameter when Route.generateUrl is called

Route.generateUrl(params?: object, queryStringParams?: object): NextRoute

Generates a NextRoute object which is used for client-side routing. It will generate both href and as via toHref and toAs methods.

  • If using static routes, Route.generateUrl can be called without any arguments
  • If generating dynamic routes you'll have to pass params and optionally queryStringParams
    • params is the object which corresponds to path-to-regexp params
    • queryStringparams is the object with query string key/values pairs

 

If not using Universal Next.js Route's Link

NextRoute.toAs(): string

Generates as prop to pass to Next.js's Link Component.

NextRoute.toHref(): string

Generates href prop to pass to Next.js's Link Component.

If not using Universal Next.js Route's Middleware Handler

Route.path: string

Returns path-to-regexp string for given route.

Route.page: string

Returns name of the page for given route.

Route.query: string

Returns an object which contains both params and query strings.

 

Router

Router.push(href: NextRoute, options?: object)

Wraps Next.js's Router.push.

Router.prefetch(href: NextRoute)

Wraps Next.js's Router.prefetch.

Router.replace(href: NextRoute, options?: object)

Wraps Next.js's Router.replace.

Router.update(href: Route, params: object)

Wraps Next.js's Router.push and updates only passed params.