-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support
runtime: edge
in api endpoints (#36947)
## Feature This PR introduces the ability to provide `runtime: "edge"` in API endpoints, the same as the experimental RSC runtime configurations. - [ ] 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 `yarn lint`
- Loading branch information
Showing
12 changed files
with
271 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/next/build/webpack/loaders/next-edge-function-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { getModuleBuildInfo } from './get-module-build-info' | ||
import { stringifyRequest } from '../stringify-request' | ||
|
||
export type EdgeFunctionLoaderOptions = { | ||
absolutePagePath: string | ||
page: string | ||
} | ||
|
||
export default function middlewareLoader(this: any) { | ||
const { absolutePagePath, page }: EdgeFunctionLoaderOptions = | ||
this.getOptions() | ||
const stringifiedPagePath = stringifyRequest(this, absolutePagePath) | ||
const buildInfo = getModuleBuildInfo(this._module) | ||
buildInfo.nextEdgeApiFunction = { | ||
page: page || '/', | ||
} | ||
|
||
return ` | ||
import { adapter } from 'next/dist/server/web/adapter' | ||
// The condition is true when the "process" module is provided | ||
if (process !== global.process) { | ||
// prefer local process but global.process has correct "env" | ||
process.env = global.process.env; | ||
global.process = process; | ||
} | ||
var mod = require(${stringifiedPagePath}) | ||
var handler = mod.middleware || mod.default; | ||
if (typeof handler !== 'function') { | ||
throw new Error('The Edge Function "pages${page}" must export a \`default\` function'); | ||
} | ||
export default function (opts) { | ||
return adapter({ | ||
...opts, | ||
page: ${JSON.stringify(page)}, | ||
handler, | ||
}) | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test/integration/react-streaming-and-server-components/switchable-runtime/pages/api/hello.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default (req) => { | ||
return new Response(`Hello from ${req.url}`) | ||
} | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} |
Oops, something went wrong.