Unverified Commit db1de21e authored by Joseph's avatar Joseph Committed by GitHub

docs: Middleware explicit runtime differences (#83348)

In previous docs versions, while not explicitly stated, it was rather
clearer that middleware has its own runtime.

Also, people who have used bundle-analyzer would for example be familiar
with the 3 bundles, or those who have worked with
`next.config.js`/instrumentation would understand that there's some
different runtimes at play going on there, but those who exclusively
work with middleware might still miss on this detail.

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent e97fc23d
...@@ -12,6 +12,12 @@ The `middleware.js|ts` file is used to write [Middleware](/docs/app/api-referenc ...@@ -12,6 +12,12 @@ The `middleware.js|ts` file is used to write [Middleware](/docs/app/api-referenc
Middleware executes before routes are rendered. It's particularly useful for implementing custom server-side logic like authentication, logging, or handling redirects. Middleware executes before routes are rendered. It's particularly useful for implementing custom server-side logic like authentication, logging, or handling redirects.
> **Good to know**:
>
> Middleware is meant to be invoked separately of your render code and in optimized cases deployed to your CDN for fast redirect/rewrite handling, you should not attempt relying on shared modules or globals.
>
> To pass information from Middleware to your application, use [headers](#setting-headers), [cookies](#using-cookies), [rewrites](/docs/app/api-reference/functions/next-response#rewrite), [redirects](/docs/app/api-reference/functions/next-response#redirect), or the URL.
Create a `middleware.ts` (or `.js`) file in the project root, or inside `src` if applicable, so that it is located at the same level as `pages` or `app`. Create a `middleware.ts` (or `.js`) file in the project root, or inside `src` if applicable, so that it is located at the same level as `pages` or `app`.
```tsx filename="middleware.ts" switcher ```tsx filename="middleware.ts" switcher
...@@ -281,7 +287,7 @@ export function middleware(request) { ...@@ -281,7 +287,7 @@ export function middleware(request) {
} }
``` ```
## Using Cookies ### Using Cookies
Cookies are regular headers. On a `Request`, they are stored in the `Cookie` header. On a `Response` they are in the `Set-Cookie` header. Next.js provides a convenient way to access and manipulate these cookies through the `cookies` extension on `NextRequest` and `NextResponse`. Cookies are regular headers. On a `Request`, they are stored in the `Cookie` header. On a `Response` they are in the `Set-Cookie` header. Next.js provides a convenient way to access and manipulate these cookies through the `cookies` extension on `NextRequest` and `NextResponse`.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment