rsnext/test/e2e
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
..
basepath Add handling for testing against deployments (#36285) 2022-04-20 12:23:09 +00:00
browserslist Add support for browserslist and legacyBrowsers experimental option (#36584) 2022-05-17 15:09:34 +00:00
browserslist-extends Ensure browserslist extends works properly (#33890) 2022-02-02 13:23:51 -06:00
config-promise-export Add support for async fn / promise in next.config.js/.mjs (#33662) 2022-02-07 08:48:35 +00:00
dynamic-route-interpolation Bug fix: dynamic page should not be interpreted as predefined page (#33808) 2022-02-02 02:57:04 +00:00
dynamic-with-suspense Fix SWC dynamic transform with suspense but without ssr (#36825) 2022-05-17 10:41:17 +00:00
getserversideprops Add handling for testing against deployments (#36285) 2022-04-20 12:23:09 +00:00
i18n-api-support Ensure non-matching API routes can be rewritten (#33966) 2022-03-01 15:53:25 -05:00
i18n-data-fetching-redirect Don't add locale to client side redirects from data fetching (#36944) 2022-05-19 00:31:43 +00:00
manual-client-base-path Add experimental config for basePath testing (#36843) 2022-05-12 16:47:34 +00:00
middleware-can-use-wasm-files feat(middleware)!: forbids middleware response body (#36835) 2022-05-19 22:02:20 +00:00
new-link-behavior Add support for passing ref to next/link with new behavior enabled (#36537) 2022-04-28 11:32:32 +02:00
next-head Fix Fast Refresh for React 18 (#35718) 2022-03-30 12:16:17 +00:00
next-script Fixes beforeInteractive scripts failing in custom document (#37000) 2022-05-18 18:36:11 +00:00
nonce-head-manager correctly assess node equality when nonce attribute is present (#27573) 2021-11-10 19:31:32 -06:00
prerender Add handling for testing against deployments (#36285) 2022-04-20 12:23:09 +00:00
prerender-native-module Migrate prerender tests to new set-up (#29245) 2021-09-21 16:21:05 +02:00
styled-jsx Flush initial styled-jsx in gIP first in concurrent rendering (#36594) 2022-05-02 22:52:46 +02:00
swc-warnings Add experimental flag to force SWC transforms (#36789) 2022-05-10 10:33:31 +00:00
type-module-interop fix(#36435): apply correct fix (#36464) 2022-04-26 11:15:49 -05:00
views-dir Add additional layer for server components case (#36921) 2022-05-16 11:46:45 +02:00
yarn-pnp/test Remove extra yarn PnP example test (#36518) 2022-04-27 11:41:34 -05:00
basepath-trailing-slash.test.ts Ensure trailingSlash is correct for index with query (#29217) 2021-09-21 16:18:42 +02:00
basepath.test.ts Add handling for testing against deployments (#36285) 2022-04-20 12:23:09 +00:00
example.txt Add util for generating new tests/error documents (#33001) 2022-01-06 09:45:04 -06:00
prerender-crawler.test.ts Add crawler blocking for fallback: true (#29121) 2021-09-16 11:01:28 -05:00
prerender-native-module.test.ts Update resolving for node_modules trace pass (#30985) 2021-11-04 20:09:37 -05:00
prerender.test.ts Update manual revalidate response cache handling (#36462) 2022-04-26 19:32:29 +00:00