Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
nextjs-dynamic-routes
Advanced tools
Super simple way to create dynamic routes with url parameters with Next.js
A dynamic routing solution for the awesome Next.js framework.
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)
})
// ./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...
npm install --save nextjs-dynamic-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!).
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
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)
})
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.
// 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>
)
// 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>
}
}
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 :
<Link prefetch route="film" id="2"><a>The Empire Strikes Back</a></Link>
import Router from '../routes'
<button onClick={() => Router.pushRoute('film', { id: 2 })}>
Go to film 2
</button>
import Router from '../routes'
<button onClick={() => Router.replaceRoute('film', { id: 2 })}>
Go to film 2
</button>
import Router from '../routes'
<button onClick={() => Router.prefetchRoute('film', { id: 2 })}>
Prefetch film 2
</button>
import Router from '../routes'
console.log(Router.getRoutePath('characterAndFilm', { characterId: 2, filmId: 5 }))
// => '/character-and-film/2/5'
FAQs
Super simple way to create dynamic routes with url parameters with Next.js
The npm package nextjs-dynamic-routes receives a total of 75 weekly downloads. As such, nextjs-dynamic-routes popularity was classified as not popular.
We found that nextjs-dynamic-routes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.