Generates a routes.js file containing the available routes in /pages folder in Next.js project.
npm install --save-dev generate-next-routesor
yarn add -D generate-next-routesAdd generate-next-routes as a script to package.json (not necessary when using yarn)
Run command
npm run generate-next-routesor
yarn generate-next-routes<Link href="/about-us">
About us
</Link>import { routes } from '../routes'; // path can be aliased in your jsconfig for easier imports
<Link href={routes.aboutUs}>
About us
</Link>Given a directory structured like this:
pages
├── news
│ └── articles
│ └── [article]
│ ├── edit.js
│ └── index.js
├── user
│ └── [user].js
├── _app.js
├── about-us.js
├── blog.js
└── index.js
Generates a routes.js file that looks like this:
export const routes = {
_app: '/_app',
aboutUs: '/about-us',
blog: '/blog',
home: '/',
news: {
articles: {
article: {
edit: {
getRoute: (article) => `/news/articles/${article}/edit}`
},
home: {
getRoute: (article) => `/news/articles/${article}}`
}
},
home: '/news/articles'
}
},
user: { user: { getRoute: (user) => `/user/${user}}` } }
}