rsnext/packages/next/server
Hannes Bornö a9b9e00703
fix(switchable-runtime): Make it possible to switch between edge and server runtime in dev (#39327)
Makes it possible to switch between edge/server runtime in dev without
breaking the server.

Fixes slack:
[1](https://vercel.slack.com/archives/CGU8HUTUH/p1659082535540549)
[2](https://vercel.slack.com/archives/C02CDC2ALJH/p1658978287244359)
[3](https://vercel.slack.com/archives/C03KAR5DCKC/p1656869427468779)

#### middleware-plugin.ts
`middlewareManifest` moved from module scope to local scope. Stale state
from earlier builds ended up in `middleware-manifest.json`. Functions
that changed from edge to server runtime stayed in the manifest as edge
functions.

#### on-demand-entry-handler.ts
When a server or edge entry is added we check if it has switched
runtime. If that's the case the old entry is removed.

#### Reproduce
Create edge API route and visit `/api/hello`
```js
// pages/api/hello.js
export const config = {
  runtime: 'experimental-edge',
}

export default () => new Response('Hello')
```

Change it to a server api route and visit `/api/hello`, it will explode.
```js
// pages/api/hello.js
export default function (req, res) {
  res.send('Hello')
}
```

#### Bug not fixed
One EDGE case is not fixed. It occurs if you switch between edge and
server runtime several times without changing the content of the file:

Edge runtime
```js
export const config = {
  runtime: 'experimental-edge',
}

export default () => new Response('Hello')
```

Change it to a server runtime
```js
export default function (req, res) {
  res.send('Hello')
}
```

Change back to edge runtime, the content of the file is the same as the
first time we compiled the edge runtime version.
```js
export const config = {
  runtime: 'experimental-edge',
}

export default () => new Response('Hello')
```

The reason is that both the edge and server compiler emits to the same
file (/.next/server/pages/api/hello.js) which makes this check fail in
webpack:
https://github.com/webpack/webpack/blob/main/lib/Compiler.js#L849-L861
Possible solution is to use different output folders for edge and server
https://vercel.slack.com/archives/CGU8HUTUH/p1661163106667559

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-09-07 13:42:32 -07:00
..
api-utils Ensure path can be specified for clearPreviewData (#40238) 2022-09-05 13:37:08 -07:00
base-http Upgrade typescript to 4.8.2 (#39979) 2022-08-29 16:56:02 +00:00
dev fix(switchable-runtime): Make it possible to switch between edge and server runtime in dev (#39327) 2022-09-07 13:42:32 -07:00
lib Bypass empty pages folder for layouts (#40132) 2022-09-03 00:13:47 +00:00
response-cache Eliminate path polyfill and incremental-cache from base server (#39548) 2022-08-12 15:25:47 +00:00
send-payload Enable @typescript-eslint/no-use-before-define for functions (#39602) 2022-08-15 10:29:51 -04:00
web Update to detect GSSP with edge runtime during build (#40076) 2022-08-30 18:18:02 +00:00
accept-header.ts Enable @typescript-eslint/no-use-before-define for functions (#39602) 2022-08-15 10:29:51 -04:00
app-render.tsx Add prefetch to new router (#39866) 2022-09-06 17:29:09 +00:00
base-server.ts Improved route resolution in next-app-loader (#40109) 2022-09-06 10:03:21 -07:00
body-streams.ts Fix unhandled rejections with edge runtime (#39091) 2022-07-28 07:50:51 +00:00
config-schema.ts Add experimental proxy timeout option (#40289) 2022-09-06 20:14:08 -07:00
config-shared.ts Add experimental proxy timeout option (#40289) 2022-09-06 20:14:08 -07:00
config-utils.ts Setup require hook in next-server for styled-jsx resolving (#39305) 2022-08-08 20:27:42 -05:00
config.ts Update to stable: next/future/image, remotePatterns, unoptimized (#40142) 2022-08-31 22:44:17 +00:00
crypto-utils.ts Move next-server directory files to server directory (#26756) 2021-06-30 13:44:40 +02:00
font-utils.ts Move next-server directory files to server directory (#26756) 2021-06-30 13:44:40 +02:00
get-app-route-from-entrypoint.ts App Build Stats (#38884) 2022-08-10 19:31:01 +00:00
get-page-files.ts Add initial handling for routing tests (#36635) 2022-05-03 10:37:23 +00:00
get-route-from-entrypoint.ts App Build Stats (#38884) 2022-08-10 19:31:01 +00:00
htmlescape.ts Upgrade to Prettier 2 (#13061) 2020-05-18 15:24:37 -04:00
image-optimizer.ts Update to stable: next/future/image, remotePatterns, unoptimized (#40142) 2022-08-31 22:44:17 +00:00
load-components.ts Bypass empty pages folder for layouts (#40132) 2022-09-03 00:13:47 +00:00
match-bundle.ts App Build Stats (#38884) 2022-08-10 19:31:01 +00:00
next-server.ts Add experimental proxy timeout option (#40289) 2022-09-06 20:14:08 -07:00
next.ts Handle rewriting WebSocket requests (#39463) 2022-08-10 17:00:30 +00:00
node-polyfill-fetch.js Pre-compile more dependencies (#32742) 2022-01-17 15:17:22 +00:00
node-polyfill-web-streams.js Update Edge Runtime (#38862) 2022-07-21 18:29:19 +00:00
node-web-streams-helper.ts Improved server CSS handling (#39664) 2022-08-17 10:56:52 +00:00
optimize-amp.ts Move next-server directory files to server directory (#26756) 2021-06-30 13:44:40 +02:00
post-process.ts Optimize Edge SSR bundle size (#38570) 2022-07-12 23:39:18 +00:00
render-result.ts Add unstable_useFlushEffects hook (#34117) 2022-02-18 00:18:28 +00:00
render.tsx Enable @typescript-eslint/no-use-before-define for functions (#39602) 2022-08-15 10:29:51 -04:00
request-meta.ts Fix handling with custom _error and pages/500 (#40110) 2022-08-30 18:14:12 -05:00
require.ts Add support for optional catchall with new router (#38444) 2022-07-08 10:29:41 +00:00
router.ts Revert "Refactor Server Router" (#40328) 2022-09-07 13:24:29 -07:00
serve-static.ts fix(#39706): add avif support for node serve static (#39733) 2022-08-18 17:57:12 +00:00
server-route-utils.ts Enable @typescript-eslint/no-use-before-define variables,enums,typedefs for core files (#39511) 2022-08-11 16:32:52 -05:00
utils.ts chore: Clean up imports and unused code (#39044) 2022-07-26 21:41:59 +00:00
web-server.ts Improved route resolution in next-app-loader (#40109) 2022-09-06 10:03:21 -07:00