v3.2.3
A small client/server-side redirect utility for Next.js
Repository
Current version released
2 years ago
Versions
- v3.2.3Latest
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-6
- v3.0.0-5
- v3.0.0-4
- v3.0.0-3
- v3.0.0-2
- v3.0.0-1
- v3.0.0-0
- v2.8.0
- v2.7.2
- v2.7.1
- v2.7.0
- v1.1.1
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.1
- v2.5.0
- v1.0.7
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.15
- v2.1.14
- v2.1.13
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- esm
Next-Redirects
Next-Redirects is a small Redirect utility for client & serve side for Next.js, with a small package size
โ ๏ธ version 3.x and above is needs next.js v12 at least โ ๏ธ
Features
- โ๏ธ Client and Server side
- ๐ฅ Small and Fast
- ๐ Fully supports esm
- ๐ supported Preact so all next.js project using Preact Next-redirects-preact
How to install
#using yarn
yarn add Next-Redirects
#using npm
npm i Next-Redirects
How to use
For the client side, you can use the following code
You can use all these props with <Redirects>
component
Prop | usage | type |
---|---|---|
href | The link you want to redirect to | String |
condition | The boolean that decides to redirect or not | boolean |
fallBack | (optional) The Fallback link if the condition is false | String |
asPath | (optional) The path mask if you want to show a different url than the real one | string |
shallow | (optional) The shallow option in next router for more info | boolean |
//X.tsx
import { Redirects } from "next-redirects";
<Redirects href="/main" condition={auth.loggedin} />;
If you have a switch or more than the condition
//X.tsx
import { Redirects } from "next-redirects";
if (user.loggedin) {
<Redirects href="/Dashboard" />;
}
switch (condistionX) {
case admin:
<Redirects href="/Dashboard" />;
break;
case anonymous:
<Redirects href="/Login" />;
break;
default:
<Redirects href="/main" />;
}
For the server side, you can use the following code
You can use all these props with serverRedirect()
funcation
Prop | usage | type |
---|---|---|
condition | The boolean that decides to redirect or not | boolean |
url | The link you want to redirect to | String |
//inside _middleware.ts
import { serverRedirect } from "next-redirects";
export async function middleware(req: NextRequest) {
const admin = req.cookies.role == "admin";
return serverRedirect(admin, "/Upload");
}
This is just an example you can use it for whatever use cases you can imagine ๐