Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nextjs-dynamic-routes

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextjs-dynamic-routes

Thin wrapper around Next.js's router to enable you to create dynamic routes.

  • 1.0.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
99
decreased by-66.44%
Maintainers
1
Weekly downloads
 
Created
Source

Next.js Dynamic Routes (WIP)

A dynamic routing solution for the 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:

// yourServer.js
server.get('/user/:id', (req, res) => {
  return app.render(req, res, '/user', req.params)
})
// /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

First 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.

import createDynamicRoutes from 'nextjs-dynamic-routes'

export default createDynamicRoutes({
  '/user': '/user/:id',
  '/film': '/film/:id'
})

Add the middleware to your express server:

import express from 'express'
import next from 'next'
import dynamicRoutes from './routes'

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

app.prepare()
  .then(() => {
    server.use(dynamicRoutes.createMiddleware(app))
    server.listen(3000)
  })

Then Nextjs Dynamic Routes will generate for you every Link components you will need! you just have to import them like this:

// pages/index.js
import { UserLink, FilmLink } from '../routes'

export default () => (
  <div>
    <h1>Users</h1>
    <nav>
      <UserLink id="1"><a>Luke Skywalker</a></UserLink>
      <UserLink id="2"><a>C-3PO</a></UserLink>
    </nav>

    <h1>Films</h1>
    <nav>
      <FilmLink id="1"><a>A New Hope</a></FilmLink>
      <FilmLink id="2"><a>The Empire Strikes Back</a></FilmLink>
    </nav>
  </div>
)

Prefetching data

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

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

<FilmLink prefetch id="2"><a>The Empire Strikes Back</a></FilmLink>

It works for static routes too!

You can even import Link components that you didn't declare in your routes.js config file! It's using an es6 Proxy under the hood to auto-fill the href property based on the name of the Link component you imported.

for exemple if you do:

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

what you will actually get is:

props => <Link href="/about" {...props} />

Pretty cool huh?

FAQs

Package last updated on 18 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc