@nimpl/router
Edge router for next.js apps.
Allows you to configure rewrites
, redirects
, i18n
and basePath
at the middleware
level instead of next.config.js
.
Motivation
All these settings are much more efficient in @nimpl/router and in edge runtime for several reasons:
- The Edge is best suited for redirects.
Instead of the
Client -> Edge -> Server -> Redirect -> Client -> Edge -> Server -> Response
journey, it becomesClient -> Edge -> Redirect -> Client -> Edge -> Server -> Response
. That is, instead of two long journeys to the server, the client goes to the CDN and then goes to the server only for the necessary route. - Next.js
i18n
is full of bugs, and in the latest versions, they completely abandoned it, recommending using middleware. - In
middleware
, you can decide for yourself which settings to enable for which paths, which rules to set, which redirect lists for/app
paths, and which for/marketing
. Take a look at the middleware-chain to get the most flexible and suitable configuration for you.
Installation
Using npm:
npm i @nimpl/router
Example
import { createMiddleware } from '@nimpl/router';
export const middleware = createMiddleware({
redirects: [
{
source: '/old',
destination: '/',
permanent: false,
},
],
rewrites: [
{
source: '/home',
destination: '/',
locale: false,
},
],
basePath: '/doc',
i18n: {
defaultLocale: 'en',
locales: ['en', 'de'],
},
});
Options
The options either completely repeat the next.js API, or they are as close as possible to them.
rewrites
- absolutely identical next.js rewrites. Next.config.js/rewrites documentation
redirects
- absolutely identical next.js redirects. Next.config.js/redirects documentation
basePath
- path prefix for the application (f.e. /docs
, /blog
, /v2
)
i18n
- configuration for localized paths.
app/[lang]/page.tsx
).Also, instead of a boolean localeDetection
, the router takes a localeDetector
method where you can set up how exactly to get the locale (f.e. from a CloudFlare header)
Otherwise, it repeats the logic of next.js. Next.config.js/i18n documentation
Development
Read about working on the package and making changes on the contribution page
Additional
Please consider giving a star if you like it, it shows that the package is useful and helps me continue work on this and other packages.
Create issues with wishes, ideas, difficulties, etc. All of them will definitely be considered and thought over.