-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from rawnly/f/6-before-all-middleware
beforeAll middleware
- Loading branch information
Showing
24 changed files
with
2,024 additions
and
657 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"getting-started": "Getting Started" | ||
"getting-started": "Getting Started", | ||
"middlewares": "Middlewares", | ||
"hostname-checks": "Hostname Checks", | ||
"advanced-options": "Advanced Options" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Callout } from 'nextra-theme-docs' | ||
|
||
# Advanced Options | ||
Next Wayfinder provides some advanced options to help you customize the behavior of your middlewares. | ||
|
||
```ts | ||
interface WayfinderOptions<T> { | ||
/** | ||
* | ||
* Enables debug logs | ||
*/ | ||
debug?: boolean; | ||
|
||
/** | ||
* | ||
* A function that returns the data to be injected into the request | ||
*/ | ||
context?: T | RequestInjector<T>; | ||
|
||
/** | ||
* Global middleware to be executed before all other middlewares | ||
* Useful if you want to set a cookie or apply some logic before each request | ||
*/ | ||
beforeAll?: BeforeAllMiddleware; | ||
|
||
/** | ||
* | ||
* A function to extract `hostname` and `pathname` from `NextRequest` | ||
*/ | ||
parser?: RequestParser; | ||
|
||
/** | ||
* The response to be used. | ||
* Useful when you want to chain other middlewares or return a custom response | ||
* Default to `NextResponse.next()` | ||
*/ | ||
response?: NextResponse; | ||
} | ||
``` | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"before-all": "beforeAll", | ||
"context": "context", | ||
"response": "response" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Callout } from 'nextra-theme-docs' | ||
|
||
# `beforeAll` | ||
|
||
This callback is executed before each middleware. It can be used to write cookies or execute some custom logic. | ||
It must return a valid `NextResponse` instance. | ||
|
||
<Callout type="info"> | ||
Note that you must use the `res` param inside your middlewares to access the "mapped" version from `beforeAll` | ||
</Callout> | ||
|
||
#### Good Usage | ||
```ts copy showLineNumbers filename="middleware.ts" {5,11} | ||
export default handlePaths([ | ||
{ | ||
path: "/a/b/:path*", | ||
handler: (req, res) => { | ||
console.log(res.cookies.get('my-cookie')?.value) // => '123' | ||
return res | ||
} | ||
} | ||
], { | ||
beforeAll: (req, res) => { | ||
res.cookies.set('my-cookie', 123.toString()) | ||
return res | ||
} | ||
}) | ||
``` | ||
|
||
#### Bad Usage | ||
```ts copy showLineNumbers filename="middleware.ts" {5-6,12} | ||
export default handlePaths([ | ||
{ | ||
path: "/a/b/:path*", | ||
handler: (req) => { | ||
const res = NextResponse.next() | ||
console.log(res.cookies.get('my-cookie')?.value) // => undefined | ||
return res | ||
} | ||
} | ||
], { | ||
beforeAll: (req, res) => { | ||
response.cookies.set('my-cookie', 123.toString()) | ||
return res | ||
} | ||
}) | ||
``` | ||
|
||
#### Example with redirect | ||
```ts copy showLineNumbers filename="middleware.ts" {7-8,10} | ||
export default handlePaths([ | ||
{ | ||
path: "/old-path", | ||
handler: (req, res) => { | ||
console.log(res.cookies.get('my-cookie')?.value) // => '123' | ||
|
||
const url = req.nextUrl.clone() | ||
url.pathname = "/new-path" | ||
|
||
return NextResponse.redirect(url, res) | ||
} | ||
} | ||
], { | ||
beforeAll: (req, res) => { | ||
response.cookies.set('my-cookie', 123.toString()) | ||
return res | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Context | ||
|
||
The `context` property can be an object or a function that can used to pass properties into our request. | ||
For example you can provide a session object or token with the user data. | ||
|
||
#### Example | ||
```ts copy showLineNumbers filename="middleware.ts" | ||
import { type Session, getSession } from '@acme/auth' | ||
|
||
interface Context { | ||
session: Session | ||
} | ||
|
||
export default handlePaths([ | ||
{ | ||
path: "/protected", | ||
pre: req => !!req.ctx?.session || { | ||
redirectTo: '/auth/sign-in' | ||
}, | ||
handler: (_, res) => res | ||
} | ||
], { | ||
context: async request => { | ||
const session = await getSession(request); | ||
|
||
return { | ||
session | ||
} | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# response | ||
This is were the first instance of `NextResponse` is intiated. | ||
Default value is `NextResponse.next()` but you can customize it and even provide a function to generate it. | ||
|
||
## Callbacks Order | ||
1. The response journey starts here. | ||
2. It's passed through the `beforeAll` callback | ||
3. If available goes through the `beforeAll` callback of the hostname middleware | ||
4. The actual handler if exists or the default one | ||
|
||
```ts | ||
// step 1 | ||
function responseFactory() : NextResponse { | ||
const res = NextResponse.next() | ||
|
||
// whatever | ||
res.cookies.set('appVersion', '0.0.1') | ||
|
||
|
||
return res | ||
} | ||
|
||
export default handlePaths([ | ||
{ | ||
hostname: /app\./, | ||
// step 3 | ||
beforeAll: (req, res) => { | ||
// do checks or whatever | ||
return res | ||
}, | ||
handler: [ | ||
{ | ||
path: '/:path*', | ||
// step 4 | ||
handler: (req, res) => { | ||
// do stuff | ||
return res | ||
} | ||
} | ||
] | ||
} | ||
], { | ||
response: responseFactory, | ||
// step 2 | ||
beforeAll: (req, res) => { | ||
res.cookies.set('my-cookie', 123.toString()) | ||
return res | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c1a0e42
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-wayfinder – ./
next-wayfinder.vercel.app
next-wayfinder-git-main-fedevitaledev.vercel.app
www.next-wayfinder.dev
next-wayfinder-fedevitaledev.vercel.app
next-wayfinder.dev