@fuelrats/next-named-routes v3.1.1
@fuelrats/next-named-routes
Named route definitions for Next.js 9+ dynamic routes.
- Easy to use API to simplify routing within your app.
- Generate URLS automatically from parameters.
- Prevent errors in your routing with strict validation.
Table of Contents
- Installation
- Defining routes in your application
- Using
<Link /> - Using
Router - Providing a Custom
LinkandRouter - Migrating from
next-routes
Installation
- Install via:
yarn add @fuelrats/next-named-routesornpm i @fuelrats/next-named-routes - Structure your
pagesdirectory for Next.js dynamic routes. - Create a
routes.jsfile in your project similar to this:
import routes from '@fuelrats/next-named-routes'
// Destructure what you need
const { Link, Router, useRouter, withRouter } = routes()
.add('about', '/about-us') // define your routes
.add('profile', '/profile/[tab]')
.add('cms', '/cms/[...cmsPath]')
// export what you need
export { Link, Router, useRouter, withRouter }- Use
<Link />andRouterin your application!
Defining routes in your application
In large applications routes can get long and complex. You can use the .add() function to alias your routes into simple names.
Route definitions are completely optional. You can use a normal href value to refer to your page without issue!
to use .add(), just tag the function onto the end of your routes() call:
const { /* ... */ } = routes()
.add(name, href).add() accepts two arguments:
name- The name of your route. This will be used to refer back to the given path. Can be a string or symbol.href- The path inside yourpagesdirectory. Identical tohrefused innext/linkandnext/router. Also accepts a function.
If successful, it will return the current instance of routes() so you can chain route definitions together.
See Using <Link /> and Using Router below for more details on using defined routes.
Function routes
.add() also accepts a callback for ultimate control over how your route definition behaves. This is good for when you want to process objects into path slugs, or if you want to control what page the route leads to based upon the value of a parameter.
The callback is passed a single parameter:
params- object given to<Link />orRoutercall
and is expected to return an object with the following properties:
href- The path inside yourpagesdirectory.as- The path that will be rendered in the browser URL bar.query- object of parameters to be transformed into a query string (optional)
const { /* ... */ } = routes()
.add('forum post', ({ publishDate, slug, ...query }) => {
const year = publishDate.getUTCFullYear()
const month = publishDate.getUTCMonth()
const day = publishDate.getUTCDate()
return {
href: '/forum/[year]/[month]/[day]/[slug]',
as: `/forum/${year}/${month}/${day}/${slug}`,
query,
}
})Another example:
.add('forum list', ({ category, page, ...query }) => {
let href = '/forum'
let as = '/forum'
if (category) {
href += '/cat/[category]'
href += `/cat/${category}`
if (page) {
href += '/[page]'
as += `/${page}`
}
}
return { href, as, query }
})Using <Link />
The provided <Link /> component is a wrapped next/link which lets you reference defined routes by their names, and generate the final URL via parameters.
route- Defined route name or a path in yourpagesdirectory.params- Parameters used to generate the URL and query string.- All other props of
next/link, howeverhrefandasare overwritten byrouteif it's defined.
route as a defined route name
import { Link } from '../routes'
const Nav = () => (
<div>
{/*
* Equivalent to:
* <Link
* href="/forums" />
*/}
<Link route="forum list">
<a>forums</a>
</Link>
{/*
* Equivalent to:
* <Link
* href="/forums/[year]/[month]/[day]/[slug]"
* as="/forums/2015/06/01/out-of-fuel-explorer-rescue-service-the-fuel-rats" />
*/}
<Link route="forum post" params={{ year: '2015', month: '06', day: '01', slug: 'out-of-fuel-explorer-rescue-service-the-fuel-rats' }}>
<a>Forum post</a>
</Link>
{/*
* Equivalent to:
* <Link
* href="/cms/[...cmsPath]"
* as="/cms/legal/terms-of-service" />
*/}
<Link route="cms" params{{ cmsPath: [ 'legal', 'terms-of-service' ] }}>
<a>Terms of Service</a>
</link>
</div>
)
)route as a file path
You can also refer to routes not defined in routes.js by using page's path in your pages directory.
{/*
* Equivalent to:
* <Link
* href="/profile/[tab]"
* as="/profile/overview" />
*/}
<Link route="/profile/[tab]" params={{ tab: 'overview' }}>
<a>Your Profile</a>
</Link>Handling parameters
The route compiler is smart about what parameters are used by your dynamic routes. for example:
Unused parameters will be passed as a querystring to your page.
{/*
* Equivalent to:
* <Link
* href="/profile/[tab]?welcome=true"
* as="/profile/overview?welcome=true" />
*/}
<Link route="/profile/[tab]" params={{ tab: 'overview', welcome: true }}>
<a>Your Profile</a>
</Link>Missing parameters will cause an error.
{/* ERROR! "tab" is required */}
<Link route="/profile/[tab]" params={{ welcome: true }}>
<a>Your Profile</a>
</Link>Using Router
The Router object ia a modified next/router with three additional functions.
.pushRoute()- wrapped.push().replaceRoute()- wrapped.replace().prefetchRoute()- wrapped.prefetch()
All three accept the same parameters and behave like their next/router counterparts
import Router from '../routes'
Router.pushRoute(route, params, options)route- Defined route name or a path in yourpagesdirectory.params- Parameters used to generate the URL and query string.options-next/routeroptions object.
route and params arguments accept the same values as the corresponding <Link /> props above.
Providing a Custom Link and Router
You can provide your own Link and Router instances if you wish to extend functionality futher, however there is one major caveat. the Router object MUST be an object which provides both the main Router API object and the useRouter() hook.
Pass Link and Router to routes like the following:
const RouterObj = {
default: CustomRouter
useRouter: customRouterHook
}
const {Link, Router} = routes(CustomLink, RouterObj)While inconvienent, we require both of these things to be able to provide the wrapped useRouter() hook and withRouter() HoC.
The custom Router is NOT required to pass a custom Link component. One can exist without the other.
Migrating from next-routes
Coming from next-routes? Welcome! While we do not provide the exact same API, it should feel familiar to use.
This library is NOT a simple drop-in replacement for next-routes. Some work is required to transition your route definitions to dynamic route string format, and most advanced regex matches are not possible since next.js does not use path-to-regexp.
- Follow the setup as above, making adjustments to your
routes.jsfile as needed. - remove
next-routesfrom your server router. In most cases this just involves removing thenext-routeshandler wrapper, and the library import itself.- If you only implemented a custom server for dynamic routing, chances are you could remove it altogether!
- remove
next-routesviayarn remove next-routesornpm r -S next-routes
Differences from next-routes
- Arguments for
routes.add()differs significantly fromnext-routes. For more information see the Configuringroutes()section above.nameis now required.pattern, which we callhref, is aNext.jsdynamic route path instead of apath-to-regexppattern.page, or the path to the page file, is no longer needed as the file system will always reflect thehrefvalue.
Brought to you by The Fuel Rats! ⛽🐀
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago