-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow using the
nodejs
runtime for route handlers when using turbop…
…ack (#48791) ### What? We previously ran all route handlers with the edge runtime, which means you can't use built in node.js modules With this PR, the runtime can be selected as documented in the next.js docs Fixes WEB-873
- Loading branch information
1 parent
e7c9d3c
commit 59e9b43
Showing
30 changed files
with
869 additions
and
570 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
...-core/js/src/entry/app/route-bootstrap.ts → .../js/src/entry/app/edge-route-bootstrap.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
49 changes: 49 additions & 0 deletions
49
packages/next-swc/crates/next-core/js/src/entry/app/edge-route.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,49 @@ | ||
// IPC need to be the first import to allow it to catch errors happening during | ||
// the other imports | ||
import startHandler from '../../internal/api-server-handler' | ||
|
||
import 'next/dist/server/node-polyfill-fetch.js' | ||
|
||
import { join } from 'path' | ||
import { parse as parseUrl } from 'node:url' | ||
|
||
import { | ||
NodeNextRequest, | ||
NodeNextResponse, | ||
} from 'next/dist/server/base-http/node' | ||
|
||
import { runEdgeFunction } from '../../internal/edge' | ||
import { attachRequestMeta } from '../../internal/next-request-helpers' | ||
|
||
import chunkGroup from 'ROUTE_CHUNK_GROUP' | ||
|
||
startHandler(async ({ request, response, query, params, path }) => { | ||
const req = new NodeNextRequest(request) | ||
const res = new NodeNextResponse(response) | ||
|
||
const parsedUrl = parseUrl(req.url!, true) | ||
attachRequestMeta(req, parsedUrl, request.headers.host!) | ||
|
||
const edgeInfo = { | ||
name: 'edge', | ||
paths: chunkGroup.map((chunk) => | ||
join(process.cwd(), '.next/server/app', chunk) | ||
), | ||
wasm: [], | ||
env: Object.keys(process.env), | ||
assets: [], | ||
} | ||
|
||
await runEdgeFunction({ | ||
edgeInfo, | ||
outputDir: 'app', | ||
req, | ||
res, | ||
query, | ||
params, | ||
path, | ||
onWarning(warning) { | ||
console.warn(warning) | ||
}, | ||
}) | ||
}) |
59 changes: 35 additions & 24 deletions
59
packages/next-swc/crates/next-core/js/src/entry/app/route.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 |
---|---|---|
@@ -1,39 +1,50 @@ | ||
// IPC need to be the first import to allow it to catch errors happening during | ||
// the other imports | ||
import startHandler from '../../internal/api-server-handler' | ||
import { runEdgeFunction } from '../../internal/edge' | ||
|
||
import { join } from 'path' | ||
import '../../polyfill/app-polyfills' | ||
|
||
import 'next/dist/server/node-polyfill-fetch.js' | ||
|
||
import chunkGroup from 'ROUTE_CHUNK_GROUP' | ||
import { parse as parseUrl } from 'node:url' | ||
|
||
import { | ||
NodeNextRequest, | ||
NodeNextResponse, | ||
} from 'next/dist/server/base-http/node' | ||
import { sendResponse } from 'next/dist/server/send-response' | ||
import { NextRequestAdapter } from 'next/dist/server/web/spec-extension/adapters/next-request' | ||
import { RouteHandlerManagerContext } from 'next/dist/server/future/route-handler-managers/route-handler-manager' | ||
|
||
import { attachRequestMeta } from '../../internal/next-request-helpers' | ||
|
||
import RouteModule from 'ROUTE_MODULE' | ||
import * as userland from 'ENTRY' | ||
import { PAGE, PATHNAME } from 'BOOTSTRAP_CONFIG' | ||
|
||
const routeModule = new RouteModule({ | ||
userland, | ||
pathname: PATHNAME, | ||
resolvedPagePath: `app/${PAGE}`, | ||
nextConfigOutput: undefined, | ||
}) | ||
|
||
startHandler(async ({ request, response, query, params, path }) => { | ||
const edgeInfo = { | ||
name: 'edge', | ||
paths: chunkGroup.map((chunk) => | ||
join(process.cwd(), '.next/server/app', chunk) | ||
), | ||
wasm: [], | ||
env: Object.keys(process.env), | ||
assets: [], | ||
} | ||
await runEdgeFunction({ | ||
edgeInfo, | ||
outputDir: 'app', | ||
req: new NodeNextRequest(request), | ||
res: new NodeNextResponse(response), | ||
query, | ||
const req = new NodeNextRequest(request) | ||
const res = new NodeNextResponse(response) | ||
|
||
const parsedUrl = parseUrl(req.url!, true) | ||
attachRequestMeta(req, parsedUrl, request.headers.host!) | ||
|
||
const context: RouteHandlerManagerContext = { | ||
params, | ||
path, | ||
onWarning(warning) { | ||
console.warn(warning) | ||
staticGenerationContext: { | ||
supportsDynamicHTML: true, | ||
}, | ||
}) | ||
} | ||
|
||
const routeResponse = await routeModule.handle( | ||
NextRequestAdapter.fromNodeNextRequest(req), | ||
context | ||
) | ||
|
||
await sendResponse(req, res, routeResponse) | ||
}) |
4 changes: 1 addition & 3 deletions
4
packages/next-swc/crates/next-core/js/src/entry/edge-bootstrap.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
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
27 changes: 27 additions & 0 deletions
27
packages/next-swc/crates/next-core/js/src/internal/next-request-helpers.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,27 @@ | ||
import { TLSSocket } from 'tls' | ||
import { | ||
addRequestMeta, | ||
NextUrlWithParsedQuery, | ||
} from 'next/dist/server/request-meta' | ||
import { NodeNextRequest } from 'next/dist/server/base-http/node' | ||
import { BaseNextRequest } from 'next/dist/server/base-http' | ||
import { getCloneableBody } from 'next/dist/server/body-streams' | ||
|
||
export function attachRequestMeta( | ||
req: BaseNextRequest, | ||
parsedUrl: NextUrlWithParsedQuery, | ||
host: string | ||
) { | ||
const protocol = ( | ||
(req as NodeNextRequest).originalRequest?.socket as TLSSocket | ||
)?.encrypted | ||
? 'https' | ||
: 'http' | ||
|
||
const initUrl = `${protocol}://${host}${req.url}` | ||
|
||
addRequestMeta(req, '__NEXT_INIT_URL', initUrl) | ||
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query }) | ||
addRequestMeta(req, '_protocol', protocol) | ||
addRequestMeta(req, '__NEXT_CLONABLE_BODY', getCloneableBody(req.body)) | ||
} |
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
Oops, something went wrong.