Catalog npm
Back to packageRELEASE DETAILS

1.7.0

npm

07aj-next-routes-extended

Easy to use universal dynamic routes for Next.js

nextnext.jsreactrouterouterroutesrouting
Published9 Mar 2022
Direct dependencies29
Transitive dependenciesNot calculated

README

Current package documentation

Dynamic Routes for Next.js

npm version Coverage Status Build Status

This is forked from version of the original repo with some enhancements

  • Updates to the latest packages ( next.js and path-to-regexp )
  • Better typescript support
  • More examples and live codesandbox
  • Actively maintained

PRs are welcome!

Easy to use universal dynamic routes for Next.js

  • Express-style route and parameters matching
  • Request handler middleware for express & co
  • Link and Router that generate URLs by route definition

Examples

CodeSandbox Example

How to use

Install:

npm install next-routes-extended --save

Create routes.js inside your project:

const routes = require('next-routes-extended')

                                                    // Name   Page      Pattern
module.exports = routes()                           // ----   ----      -----
.add('about')                                       // about  about     /about
.add('blog', '/blog/:slug')                         // blog   blog      /blog/:slug
.add('user', '/user/:id', 'profile')                // user   profile   /user/:id
.add('/:noname/:lang(en|es)/:wow+', 'complex')      // (none) complex   /:noname/:lang(en|es)/:wow+
.add({name: 'beta', pattern: '/v3', page: 'v3'})    // beta   v3        /v3

This file is used both on the server and the client.

API:

  • routes.add([name], pattern = /name, page = name)
  • routes.add(object)

Arguments:

  • name - Route name
  • pattern - Route pattern (like express, see path-to-regexp)
  • page - Page inside ./pages to be rendered

The page component receives the matched URL parameters merged into query

export default class Blog extends React.Component {
  static async getInitialProps ({query}) {
    // query.slug
  }
  render () {
    // this.props.url.query.slug
  }
}

On the server

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

// With express
const express = require('express')
app.prepare().then(() => {
  express().use(handler).listen(3000)
})

// Without express
const {createServer} = require('http')
app.prepare().then(() => {
  createServer(handler).listen(3000)
})

Optionally you can pass a custom handler, for example:

const handler = routes.getRequestHandler(app, ({req, res, route, query}) => {
  app.render(req, res, route.page, query)
})

Make sure to use server.js in your package.json scripts:

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

On the client

Import Link and Router from your routes.js file to generate URLs based on route definition:

// pages/index.js
import {Link} from '../routes'

export default () => (
  <div>
    <div>Welcome to Next.js!</div>
    <Link route='blog' params={{slug: 'hello-world'}}>
      <a>Hello world</a>
    </Link>
    or
    <Link route='/blog/hello-world'>
      <a>Hello world</a>
    </Link>
  </div>
)

API:

  • <Link route='name'>...</Link>
  • <Link route='name' params={params}> ... </Link>
  • <Link route='/path/to/match'> ... </Link>

Props:

  • route - Route name or URL to match (alias: to)
  • params - Optional parameters for named routes

It generates the URLs for href and as and renders next/link. Other props like prefetch will work as well.

Router example

// pages/blog.js
import React from 'react'
import {Router} from '../routes'

export default class Blog extends React.Component {
  handleClick () {
    // With route name and params
    Router.pushRoute('blog', {slug: 'hello-world'})
    // With route URL
    Router.pushRoute('/blog/hello-world')
  }
  render () {
    return (
      <div>
        <div>{this.props.url.query.slug}</div>
        <button onClick={this.handleClick}>Home</button>
      </div>
    )
  }
}

API:

  • Router.pushRoute(route)
  • Router.pushRoute(route, params)
  • Router.pushRoute(route, params, options)

Arguments:

  • route - Route name or URL to match
  • params - Optional parameters for named routes
  • options - Passed to Next.js

The same works with .replaceRoute() and .prefetchRoute()

It generates the URLs and calls next/router


Optionally you can provide custom Link and Router objects, for example:

const routes = module.exports = require('next-routes-extended')({
  Link: require('./my/link')
  Router: require('./my/router')
})

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 31

Native requirements
29 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
path-to-regexp
Environment
{
  "local_name": "path-to-regexp",
  "alias": false,
  "peer_metadata": null
}
^6.2.0runtime
url-parse
Environment
{
  "local_name": "url-parse",
  "alias": false,
  "peer_metadata": null
}
^1.5.4runtime
@babel/cli
Environment
{
  "local_name": "@babel/cli",
  "alias": false,
  "peer_metadata": null
}
^7.16.8development
@babel/core
Environment
{
  "local_name": "@babel/core",
  "alias": false,
  "peer_metadata": null
}
^7.16.12development
@babel/preset-env
Environment
{
  "local_name": "@babel/preset-env",
  "alias": false,
  "peer_metadata": null
}
^7.16.11development
@types/babel-core
Environment
{
  "local_name": "@types/babel-core",
  "alias": false,
  "peer_metadata": null
}
^6.25.7development
@types/babel__core
Environment
{
  "local_name": "@types/babel__core",
  "alias": false,
  "peer_metadata": null
}
^7.1.18development
@types/babel__preset-env
Environment
{
  "local_name": "@types/babel__preset-env",
  "alias": false,
  "peer_metadata": null
}
^7.9.2development
@types/concurrently
Environment
{
  "local_name": "@types/concurrently",
  "alias": false,
  "peer_metadata": null
}
^6.4.0development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^17.0.14development
@types/prop-types
Environment
{
  "local_name": "@types/prop-types",
  "alias": false,
  "peer_metadata": null
}
^15.7.4development
@types/react
Environment
{
  "local_name": "@types/react",
  "alias": false,
  "peer_metadata": null
}
^17.0.38development
@types/react-dom
Environment
{
  "local_name": "@types/react-dom",
  "alias": false,
  "peer_metadata": null
}
^17.0.11development
@types/react-test-renderer
Environment
{
  "local_name": "@types/react-test-renderer",
  "alias": false,
  "peer_metadata": null
}
^17.0.1development
@types/url-parse
Environment
{
  "local_name": "@types/url-parse",
  "alias": false,
  "peer_metadata": null
}
^1.4.7development
babel-core
Environment
{
  "local_name": "babel-core",
  "alias": false,
  "peer_metadata": null
}
^7.0.0-bridge.0development
babel-eslint
Environment
{
  "local_name": "babel-eslint",
  "alias": false,
  "peer_metadata": null
}
^10.1.0development
babel-jest
Environment
{
  "local_name": "babel-jest",
  "alias": false,
  "peer_metadata": null
}
^27.4.6development
concurrently
Environment
{
  "local_name": "concurrently",
  "alias": false,
  "peer_metadata": null
}
^7.0.0development
coveralls
Environment
{
  "local_name": "coveralls",
  "alias": false,
  "peer_metadata": null
}
^3.1.1development
del-cli
Environment
{
  "local_name": "del-cli",
  "alias": false,
  "peer_metadata": null
}
^4.0.1development
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^27.4.7development
next
Environment
{
  "local_name": "next",
  "alias": false,
  "peer_metadata": null
}
^12.0.10development
prop-types
Environment
{
  "local_name": "prop-types",
  "alias": false,
  "peer_metadata": null
}
^15.8.1development
react
Environment
{
  "local_name": "react",
  "alias": false,
  "peer_metadata": null
}
^17.0.2development
react-dom
Environment
{
  "local_name": "react-dom",
  "alias": false,
  "peer_metadata": null
}
^17.0.2development
react-test-renderer
Environment
{
  "local_name": "react-test-renderer",
  "alias": false,
  "peer_metadata": null
}
^17.0.2development
standard
Environment
{
  "local_name": "standard",
  "alias": false,
  "peer_metadata": null
}
^16.0.4development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^4.5.5development
next
Environment
{
  "local_name": "next",
  "alias": false,
  "peer_metadata": null
}
>=11peer
react
Environment
{
  "local_name": "react",
  "alias": false,
  "peer_metadata": null
}
>=17peer

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-UGLlL3i7pPBTXTeg1IPDgKn8GTwFQdpolUfMymK4wb6MNXcOhxvFWFs+xk4KMoRM1RcRmx5buynRYwA19BRnDg==",
  "shasum": "23329d478fad3a3007fdd019574d4e4e1433f584",
  "tarball": "https://registry.npmjs.org/07aj-next-routes-extended/-/07aj-next-routes-extended-1.7.0.tgz",
  "fileCount": 8,
  "unpackedSize": 21806,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiKFeTACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrvNg//dlkuj/uiQwJbe9dqX+gEM8ppJutjDGYMimVrPKWFcf2PVLDC\r\n4qTutb+quUxLuJmw5u23c+SB5vkLaXWTkaCMtAS8HHIInBo8Fg2GmJu1P/2d\r\n9jTMwdr7TNjhm2zFfFAJrW2xcYCv7pIVpyNsbGxbGX5DqzSzKfM45Mjr/cm1\r\n6H7/NJCdDxCj69+cMtkmbrg3FrBokxu8m+85t1nYYcpYSGy5FxGleooJ/nUC\r\nyO5pEkJD3rYJSLCyqMbUFZ7MXVzfqne/IPdc4cZOaOImJ4Et4mVGd+KDaprX\r\nfOODdbavKfe2SLcvu/paXgwSBItMHDWtMhXQN8uCFAyvgOl1ARsXpSycHPKV\r\n3/PLR9+MTcZ3DQYncAUXXDHKvIXSWo/rxMGwECoqK39RE+8PQyYxlpIb8DbZ\r\nXVcVR5gT0vMpZaydeAGkQlQTKm77it42OKNavsLREeYTHRElVWV0sbF1F/FO\r\nATTLtLqKTQHx2RHdbE218A7MTvMmItq+ERuzHb8/xyJzMb971ffl4vRjXVZg\r\npXKFJMsrdVzhCHtkR3jDQ4D17t3gYHZ641w6Pq7r859RVpCBKJGOxZ4M2taZ\r\nZ47enUnG3KyWX/yOLzkcbArLoBws0abpHhcB8rtFFXX1nanQYFMet3+/qv0l\r\nhWVeGq51zYu1XJlPJdErmgOoQd2PYPobkM8=\r\n=cq7Y\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIQDk0VbqW9qNn1616BSmMNjKn09kZEbRR8DbRhOAifu/owIgHl5J0sRvMWJQyrUvPiJwg6f8VlUYrCBp2Vqp7E+zzLs="
    }
  ]
}

Repository

PENDING
07abhishekrock/next-routes-extended

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 17:28 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:5e23869a1c0ec0da5938dfdb87130eeb227c7b67eb17a72fc8e35e2c63523099

Other collected versions

0.0.19 MarView all collected versions →