Skip to content
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

Closed
wants to merge 17 commits into from
Closed

Add new middleware docs #37514

wants to merge 17 commits into from

Conversation

molebox
Copy link
Collaborator

@molebox molebox commented Jun 7, 2022

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

  • Related issues linked using fixes #number
  • Integration tests added
  • Errors have helpful link attached, see contributing.md

Feature

  • Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
  • Related issues linked using fixes #number
  • 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

Documentation / Examples

  • Make sure the linting passes by running pnpm lint
  • The examples guidelines are followed from our contributing doc

docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
kodiakhq bot pushed a commit that referenced this pull request Jun 9, 2022
…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`
Copy link
Contributor

@timeyoutakeit timeyoutakeit left a 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!!

docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Show resolved Hide resolved
docs/advanced-features/middleware.md Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/advanced-features/middleware.md Outdated Show resolved Hide resolved
docs/api-routes/edge-api-routes.md Outdated Show resolved Hide resolved
docs/api-routes/edge-api-routes.md Outdated Show resolved Hide resolved
docs/api-routes/edge-api-routes.md Outdated Show resolved Hide resolved
@molebox molebox marked this pull request as ready for review June 22, 2022 17:37
Co-authored-by: Amy Burns <amy.burns@vercel.com>
@molebox molebox requested a review from cramforce June 23, 2022 06:36

| --------- | ------------------------------------------------------------------------------------------ |

| `v12.2.0` | Middleware GA |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:

Comment on lines +18 to +19
- 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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 👍

Comment on lines +21 to +26
Unlike API Routes, Edge API Routes:

- Can stream responses
- Run _after_ the cache
- Can cache responses
- Have zero cold starts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines +32 to +44
### 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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).
Copy link
Member

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 👍

Copy link
Contributor

@jescalan jescalan left a 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//named export
// named export

- Can stream responses
- Run _after_ the cache
- Can cache responses
- Have zero cold starts
Copy link
Contributor

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

@leerob
Copy link
Member

leerob commented Jun 26, 2022

Replaced by #37992.

@leerob leerob closed this Jun 26, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants