# nextjs-dynamic-routes

> Super simple way to create dynamic routes with url parameters with Next.js

Latest version **2.5.1** (published 2019-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install nextjs-dynamic-routes
pnpm add nextjs-dynamic-routes
yarn add nextjs-dynamic-routes
bun add nextjs-dynamic-routes
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.5.1 |
| Published | 2019-01-28 |
| First published | 2017-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 444.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 140 |
| Author | Gabriel Vergnaud |
| Maintainers | gabrielvergnaud |

## Links

- npm: https://www.npmjs.com/package/nextjs-dynamic-routes
- Repository: https://github.com/gvergnaud/nextjs-dynamic-routes
- Homepage: https://github.com/gvergnaud/nextjs-dynamic-routes#readme
- Issues: https://github.com/gvergnaud/nextjs-dynamic-routes/issues
- npm.io page: https://npm.io/package/nextjs-dynamic-routes

## Dependencies (1)

- [path-match](https://npm.io/package/path-match.md) ^1.2.4

## Recent versions

- 2.5.1 (latest) — 2019-01-28
- 2.5.0 — 2019-01-23
- 2.4.0 — 2019-01-17
- 2.3.0 — 2019-01-08
- 2.2.1 — 2018-10-20
- 2.2.0 — 2018-10-18
- 2.1.9 — 2018-10-17
- 2.1.8 — 2018-04-23
- 2.1.7 — 2018-03-10
- 2.1.6 — 2017-09-16
- 2.1.5 — 2017-06-11
- 2.1.4 — 2017-06-11
- 2.1.3 — 2017-06-05
- 2.1.2 — 2017-06-02
- 2.1.1 — 2017-06-02
- … 13 more at https://npm.io/package/nextjs-dynamic-routes/versions

## README

# Next.js Dynamic Routes

A dynamic routing solution for the awesome [Next.js](https://github.com/zeit/next.js)
framework.

## Why ?

Next.js introduced in it's V2 a programmatic routing API allowing you to serve your
Next app from, for example, an express server:

```js
// yourServer.js
server.get('/user/:id', (req, res) => {
  return app.render(req, res, '/user', req.params)
})
```
```jsx
// ./pages/index.js
<Link href={`/user?id={id}`} as={`/user/${id}`}><a>Visit me!</a></Link>
```

But as the number of pages grows, it's getting a little hard to manage...

## Introducing Dynamic Routes

```
npm install --save nextjs-dynamic-routes
```

### Setup your routes
Create a `routes.js` file and list all your **Dynamic** routes.
You don't have to list your regular routes, as Next.js will handle them as usual (but you can!).

```js
const Router = require('nextjs-dynamic-routes')

const router = new Router()

router.add({ name: 'character', pattern: '/characters/:id' })

router.add({ name: 'film', pattern: '/films/:id' })

// if the name of your route is different from your component file name:
router.add({
  name: 'characterAndFilm',
  pattern: '/character-and-film/:characterId/:filmId',
  page: '/character-and-film'
})

module.exports = router

```

### Setup your request handler
```js
const express = require('express')
const next = require('next')
const Router = require('./routes')

const app = next({ dev: process.env.NODE_ENV !== 'production' })
const server = express()
const handle = Router.getRequestHandler(app)

app.prepare()
  .then(() => {
    server.get('*', (req, res) => handle(req, res))
    server.listen(3000)
  })
```

### Use your routes
Then Nextjs Dynamic Routes exports a `Link` component. It's just like `next/link`,
but it adds a `route` prop that let you refer to a route by its name.

```jsx
// pages/index.js
import React from 'react'
import { Link } from '../routes'

export default () => (
  <ul>
    <li><Link route="character" id="1"><a>Luke Skywalker</a></Link></li>
    <li><Link route="character" id="2"><a>C-3PO</a></Link></li>
    <li><Link route="film" id="1"><a>A New Hope</a></Link></li>
    <li><Link route="film" id="2"><a>The Empire Strikes Back</a></Link></li>
    <li>
      <Link route="characterAndFilm" characterId="1" filmId="2">
        <a>The Empire Strikes Back and Luke Skywalker</a>
      </Link>
    </li>
  </ul>
)
```

```jsx
// pages/character.js
import React from 'react'

export default class Character extends React.Component {
  static async getInitialProps({ query }) {
    return fetch(`//swapi.co/api/films/${query.id}`).then(x => x.json())
  }

  render() {
    return <p>{this.props.name}</p>
  }
}
```

## Prefetching data
Next.js has this great feature allowing you to prefetch data for your next routes
in the background.

You can benefit from that by simply putting a `prefetch` property on any Link :

```jsx
<Link prefetch route="film" id="2"><a>The Empire Strikes Back</a></Link>
```

## Imperative API

### Router.pushRoute(name, params [, options])
```jsx
import Router from '../routes'

<button onClick={() => Router.pushRoute('film', { id: 2 })}>
  Go to film 2
</button>
```

### Router.replaceRoute(name, params [, options])
```jsx
import Router from '../routes'

<button onClick={() => Router.replaceRoute('film', { id: 2 })}>
  Go to film 2
</button>
```

### Router.prefetchRoute(name, params)
```jsx
import Router from '../routes'

<button onClick={() => Router.prefetchRoute('film', { id: 2 })}>
  Prefetch film 2
</button>
```

### Router.getRoutePath(name, params)
```js
import Router from '../routes'

console.log(Router.getRoutePath('characterAndFilm', { characterId: 2, filmId: 5 }))
// => '/character-and-film/2/5'
```

## Query params
The Link component has a `queryParams` prop which you can fill with an object of regular query parameters.

```jsx
<Link prefetch route="film" id="2" queryParams={{ utm_campaign: 'website' }}>
  <a>The Empire Strikes Back</a>
</Link>
```
This will result in the following url: `/films/2?utm_campaign=website`.

You can use `queryParams` with the imperative API as well

```js
// It doesn't work only for pushRoute, but for all the other methods as well.
Router.pushRoute('film', {
  id: 2,
  queryParams: {
    utm_campaign: 'website'
  }
})
```

---
_Source: https://npm.io/package/nextjs-dynamic-routes · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
