Middleware enables you to use code over configuration. This gives you full flexibility in Next.js, because you can run code before a request is completed. Based on the user's incoming request, you can modify the response by rewriting, redirecting, adding headers, or even streaming HTML.
Middleware enables you to run code before a request is completed. Based on the user's incoming request, you can modify the response by rewriting, redirecting, adding headers, or even streaming HTML.
## Usage
## Usage
1. Install the latest version of Next.js:
1. Install the canary version of Next.js:
```jsx
```jsx
npminstallnext@latest
npminstallnext@canary
```
```
2. Then, create a `middleware.ts` file under your project root directory.
2. Then, create a `middleware.ts` file under your project root directory.
...
@@ -75,7 +76,7 @@ Middleware can be used for anything that shares logic for a set of pages, includ
...
@@ -75,7 +76,7 @@ Middleware can be used for anything that shares logic for a set of pages, includ