@fuelrats/next-named-routes

中文文档
3.1.1发布于 2020/07/15BSD-3-ClauseCommonJS近 30 天下载 4165

Named routes API for Next.js dynamic routing

此软件包已被标记为弃用

Next.JS 10 now has automatic href resolution. There are currently no plans on updating this package since the experience of defining routes has been greatly improved by the Next.js team.

原始使用文档
文档目录12 节
  1. Table of Contents
  2. Installation
  3. Defining routes in your application
  4. Function routes
  5. Using <Link />
  6. route as a defined route name
  7. route as a file path
  8. Handling parameters
  9. Using Router
  10. Providing a Custom Link and Router
  11. Migrating from next-routes
  12. Differences from next-routes

@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

  1. Install via: yarn add @fuelrats/next-named-routes or npm i @fuelrats/next-named-routes
  2. Structure your pages directory for Next.js dynamic routes.
  3. Create a routes.js file 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 }
  1. Use <Link /> and Router in 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 your pages directory. Identical to href used in next/link and next/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 /> or Router call

and is expected to return an object with the following properties:

  • href - The path inside your pages directory.
  • 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 }
})

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 your pages directory.
  • params - Parameters used to generate the URL and query string.
  • All other props of next/link, however href and as are overwritten by route if 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 your pages directory.
  • params - Parameters used to generate the URL and query string.
  • options - next/router options object.

route and params arguments accept the same values as the corresponding <Link /> props above.

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.

  1. Follow the setup as above, making adjustments to your routes.js file as needed.
  2. remove next-routes from your server router. In most cases this just involves removing the next-routes handler wrapper, and the library import itself.
    • If you only implemented a custom server for dynamic routing, chances are you could remove it altogether!
  3. remove next-routes via yarn remove next-routes or npm r -S next-routes

Differences from next-routes

  • Arguments for routes.add() differs significantly from next-routes. For more information see the Configuring routes() section above.
    • name is now required.
    • pattern, which we call href, is a Next.js dynamic route path instead of a path-to-regexp pattern.
    • page, or the path to the page file, is no longer needed as the file system will always reflect the href value.

Brought to you by The Fuel Rats! ⛽🐀

版本与发布记录

@fuelrats/next-named-routes 共发布 21 个版本,首次发布于 2019/11/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.1latest2020/07/156 年前
3.1.02020/05/236 年前
3.1.0-canary.1canary2020/05/236 年前
3.1.0-canary.02020/05/236 年前
3.0.02020/03/096 年前
3.0.0-beta.3beta2020/03/086 年前

关于 @fuelrats/next-named-routes 的常见问题

@fuelrats/next-named-routes 是什么?

Named routes API for Next.js dynamic routing注意:该包已被作者标记为弃用——Next.JS 10 now has automatic href resolution. There are currently no plans on updating this package since the experience of defining routes has been greatly improved by the Next.js team.

如何安装 @fuelrats/next-named-routes?

在项目目录运行 npm install @fuelrats/next-named-routes,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:next(^9.2)、react(^16.6.0)、react-dom(^16.6.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fuelrats/next-named-routes 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fuelrats__next-named-routes 是否存在。

@fuelrats/next-named-routes 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fuelrats/next-named-routes 可以商用吗?

@fuelrats/next-named-routes 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@fuelrats/next-named-routes 的下载量有多少?

根据 npm 官方下载统计,@fuelrats/next-named-routes 在 2026/10/16 – 2026/11/14 这 30 天内被下载 4,165 次,日均约 139 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fuelrats/next-named-routes 还在维护吗?

最新版本 3.1.1 发布于 2020/07/15(6 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/16。作者已将其标记为弃用,新项目建议寻找替代方案。