rsnext/packages/next/build/webpack/loaders
Damien Simonin Feugas bf089562c7
feat(middleware)!: forbids middleware response body (#36835)
_Hello Next.js team! First PR here, I hope I've followed the right practices._

### What's in there?

It has been decided to only support the following uses cases in Next.js' middleware:
- rewrite the URL (`x-middleware-rewrite` response header)
- redirect to another URL (`Location` response header)
- pass on to the next piece in the request pipeline (`x-middleware-next` response header)

1. during development, a warning on console tells developers when they are returning a response (either with `Response` or `NextResponse`).
2. at build time, this warning becomes an error.
3. at run time, returning a response body will trigger a 500 HTTP error with a JSON payload containing the detailed error.

All returned/thrown errors contain a link to the documentation.

This is a breaking feature compared to the _beta_ middleware implementation, and also removes `NextResponse.json()` which makes no sense any more.

### How to try it?
- runtime behavior: `HEADLESS=true yarn jest test/integration/middleware/core`
- build behavior : `yarn jest test/integration/middleware/build-errors`
- development behavior: `HEADLESS=true yarn jest test/development/middleware-warnings`

### Notes to reviewers

The limitation happens in next's web adapter. ~The initial implementation was to check `response.body` existence, but it turns out [`Response.redirect()`](https://github.com/vercel/next.js/blob/canary/packages/next/server/web/spec-compliant/response.ts#L42-L53) may set the response body (https://github.com/vercel/next.js/pull/31886). Hence why the proposed implementation specifically looks at response headers.~
`Response.redirect()` and `NextResponse.redirect()` do not need to include the final location in their body: it is handled by next server https://github.com/vercel/next.js/blob/canary/packages/next/server/next-server.ts#L1142

Because this is a breaking change, I had to adjust several tests cases, previously returning JSON/stream/text bodies. When relevant, these middlewares are returning data using response headers.

About DevEx: relying on AST analysis to detect forbidden use cases is not as good as running the code.
Such cases are easy to detect:
```js
new Response('a text value')
new Response(JSON.stringify({ /* whatever */ })
```
But these are false-positive cases:
```js
function returnNull() { return null }
new Response(returnNull())

function doesNothing() {}
new Response(doesNothing())
```
However, I see no good reasons to let users ship middleware such as the one above, hence why the build will fail, even if _technically speaking_, they are not setting the response body. 



## Feature

- [x] 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`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
2022-05-19 22:02:20 +00:00
..
css-loader fix: data url handling in css-loader (#34034) 2022-02-06 11:10:14 -06:00
next-middleware-ssr-loader Decouple entries for server components and client components (#36860) 2022-05-13 19:48:53 +02:00
next-serverless-loader Deprecate nested Middleware in favor of root middleware (#36772) 2022-05-19 15:46:21 +00:00
next-style-loader Update loader-utils (#30743) 2021-11-02 16:13:15 +01:00
postcss-loader Lazy-load postcss (#31762) 2021-11-25 18:41:20 +01:00
resolve-url-loader Move resolve-url-loader into Next.js (#32932) 2022-01-02 16:16:17 -06:00
error-loader.ts Pre-compile more dependencies continued (#32679) 2021-12-21 16:13:45 +01:00
get-module-build-info.ts Decouple entries for server components and client components (#36860) 2022-05-13 19:48:53 +02:00
next-client-pages-loader.ts Decouple entries for server components and client components (#36860) 2022-05-13 19:48:53 +02:00
next-flight-client-entry-loader.ts Decouple entries for server components and client components (#36860) 2022-05-13 19:48:53 +02:00
next-flight-client-loader.ts Fix flight loader (#36282) 2022-04-19 20:14:30 +02:00
next-flight-server-loader.ts Decouple entries for server components and client components (#36860) 2022-05-13 19:48:53 +02:00
next-image-loader.js use loader-utils 2 for resolve-url-loader to fix ./data: urls in sass (#31134) 2021-11-08 18:37:25 +00:00
next-middleware-loader.ts feat(middleware)!: forbids middleware response body (#36835) 2022-05-19 22:02:20 +00:00
next-middleware-wasm-loader.ts Refactor Page Paths utils and Middleware Plugin (#36576) 2022-04-30 11:19:27 +00:00
next-swc-loader.js Add support for browserslist and legacyBrowsers experimental option (#36584) 2022-05-17 15:09:34 +00:00
next-view-loader.ts Add additional layer for server components case (#36921) 2022-05-16 11:46:45 +02:00
noop-loader.ts feat: webpack inlining with configuration for v4 / v5 (#20598) 2021-01-13 20:59:08 -05:00
utils.ts Move FlightManifestPlugin to server compilers (#36810) 2022-05-10 21:20:13 +02:00