next-routing
A nuxt inspired routing plugin for nextjs
About
This is a routing plugin for next inspired by nuxt routing. The idea is that your routing becomes a lot simpler by prepending underscores (_
) on your routes to create path parameters automatically.
Installation
You can install this package by using npm like so:
npm i next-routing --save
Page
If a file or folder begins with an underscore (_
), it will define that route part as a path parameter.
For example, if you had a file /pages/users/_id/comments.js
, it would convert that route to /users/:id/comments
.
The same can be applied to files. If you had a file /pages/users/_id.js
, the route would be /users/:id
.
This could be accessed like below:
Component static async { // query.id } { // this.props.url.query.id }
Server
You will need to add a hook on your server to intercept requests:
// server.jsconst next = const app = // make sure you export this as well as call thismoduleexportsroutes = const handler = moduleexportsroutes // With expressconst express = app // Without expressconst createServer = app
Client
You will need to import the Link
from your server file created above.
const Link = routes
Configuration
While not required, it is recommended that you disable file-system routing for added security and intentionality of how your routes are accessed.
For a full list of the api, feel free to checkout next-routes documentation (we piggy-back of what they have already done).
License
MIT