-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new middleware docs #37514
Add new middleware docs #37514
Conversation
…37556) This commit enables the following patterns in Middleware: ```ts // with a dot notation const { ENV_VAR, "ENV-VAR": myEnvVar } = process.env; // or with an object access const { ENV_VAR2, "ENV-VAR2": myEnvVar2 } = process["env"]; ``` ### Related - @cramforce asked this fixed here: #37514 (comment) ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md`
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.
This looks really good!!
Co-authored-by: Amy Burns <amy.burns@vercel.com>
|
||
| --------- | ------------------------------------------------------------------------------------------ | | ||
|
||
| `v12.2.0` | Middleware GA | |
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.
| `v12.2.0` | Middleware GA | | |
| `v12.2.0` | Middleware is stable | |
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. | ||
Middleware allows you to run code before a request is completed, then based on the incoming request, you can modify the response by rewriting, redirecting, adding headers, or setting cookies. | ||
|
||
When a request is made, it will first hit the Middleware, _then_ the cache, meaning you can personalize static content and implement authentication, run A/B tests, deliver personalized pages based on geolocation, and perform bot protection. |
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.
When a request is made, it will first hit the Middleware, _then_ the cache, meaning you can personalize static content and implement authentication, run A/B tests, deliver personalized pages based on geolocation, and perform bot protection. | |
Middleware runs _before_ cached content, so you can personalize static files and pages. Common examples of Middleware would be authentication, A/B testing, localized pages, bot protection, and more. |
|
||
When a request is made, it will first hit the Middleware, _then_ the cache, meaning you can personalize static content and implement authentication, run A/B tests, deliver personalized pages based on geolocation, and perform bot protection. | ||
|
||
## Summary of Middleware |
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.
## Summary of Middleware | |
## Quickstart |
Or similar
|
||
## Summary of Middleware | ||
|
||
- You create a single `middleware.ts` or `middleware.js` file at your projects root, with an exported function |
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.
- You create a single `middleware.ts` or `middleware.js` file at your projects root, with an exported function | |
- Create a `middleware.ts` (or `middleware.js`) file at the root of your project |
## Summary of Middleware | ||
|
||
- You create a single `middleware.ts` or `middleware.js` file at your projects root, with an exported function | ||
- The function can be a named, or default export. If the function is a named export, then is **must** be called `middleware`. For a default export, you are free to name it anything you like |
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.
- The function can be a named, or default export. If the function is a named export, then is **must** be called `middleware`. For a default export, you are free to name it anything you like | |
- Export a function named `middleware` |
This feels like a strange this to emphasize so early. Let's put them on the happy path and then add a caveat somewhere at the bottom of the page for default exports.
|
||
API Routes can be configured to run at the Edge using an experimental flag. When deployed on Vercel, these API Routes run as [Edge Functions](https://vercel.com/docs/concepts/functions/vercel-edge-functions). | ||
|
||
Edge API Routes are similar to API Routes but with different infrastructure: |
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.
Edge API Routes are similar to API Routes but with different infrastructure: | |
Edge API Routes are similar to API Routes but use the Edge Runtime: |
- On every invocation API Routes are run from the same region | ||
- Edge API Routes are copied across the [Vercel Edge Network](https://vercel.com/docs/concepts/edge-network/overview), so on every invocation, the region that is closets to you will run the function, reducing latency massively |
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.
- On every invocation API Routes are run from the same region | |
- Edge API Routes are copied across the [Vercel Edge Network](https://vercel.com/docs/concepts/edge-network/overview), so on every invocation, the region that is closets to you will run the function, reducing latency massively |
This isn't relevant to open-source Next.js - Vercel specific stuff 👍
Unlike API Routes, Edge API Routes: | ||
|
||
- Can stream responses | ||
- Run _after_ the cache | ||
- Can cache responses | ||
- Have zero cold starts |
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.
Unlike API Routes, Edge API Routes: | |
- Can stream responses | |
- Run _after_ the cache | |
- Can cache responses | |
- Have zero cold starts | |
Unlike API Routes, Edge API Routes: | |
- Can stream responses | |
- Run _after_ the cache |
You can cache API routes. and cold boots relates to the infra provider, so let's put that in Vercel docs.
It's more about the edge runtime.
### API Route | ||
|
||
```typescript | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
export default (req: NextApiRequest, res: NextApiResponse) => { | ||
res | ||
.status(200) | ||
.json({ name: `Hello, from ${req.url} I'm a Serverless Function'` }) | ||
} | ||
``` | ||
|
||
### Edge API Route |
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.
### API Route | |
```typescript | |
import type { NextApiRequest, NextApiResponse } from 'next' | |
export default (req: NextApiRequest, res: NextApiResponse) => { | |
res | |
.status(200) | |
.json({ name: `Hello, from ${req.url} I'm a Serverless Function'` }) | |
} | |
``` | |
### Edge API Route |
Let's just show Edge API routes directly, we have other docs on normal API routes
- Libraries using Node.js APIs can't be used in Edge API Routes | ||
- Dynamic code execution (such as `eval`) is not allowed | ||
|
||
For more information on limitations when using Edge API Routes, see the Vercel Edge Functions [Limitations documentation](https://vercel.com/docs/concepts/functions/vercel-edge-functions/limitations). |
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.
This should be the limitations of the Edge Runtime, nothing to do with Vercel 👍
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.
This is great!
- You create a single `middleware.ts` or `middleware.js` file at your projects root, with an exported function | ||
- The function can be a named, or default export. If the function is a named export, then is **must** be called `middleware`. For a default export, you are free to name it anything you like | ||
```js | ||
//named export |
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.
//named export | |
// named export |
- Can stream responses | ||
- Run _after_ the cache | ||
- Can cache responses | ||
- Have zero cold starts |
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.
Might be worth linking to some context on what a cold start is here
Replaced by #37992. |
This PR adds new content for the middleware docs. Including the new APIs, how to use them, common questions around middleware, and code examples.
Bug
fixes #number
contributing.md
Feature
fixes #number
contributing.md
Documentation / Examples
pnpm lint