-
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.
add edge rendering for app dir for Turbopack (#50830)
### What? * allow to use `runtime = "edge"` for app dir in turbopack * move common imports from next-app-loader to `packages/next/src/server/app-render/entry-base.ts` * move common turbopack code to communicate between JS and Rust into separate files ### Why? A lot test cases depend on edge rendering ### How? --------- Co-authored-by: Alex Kirszenberg <alex.kirszenberg@vercel.com>
- Loading branch information
Showing
38 changed files
with
1,372 additions
and
709 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/next-swc/crates/next-core/js/src/entry/app-edge-renderer.tsx
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,82 @@ | ||
// IPC need to be the first import to allow it to catch errors happening during | ||
// the other imports | ||
import startOperationStreamHandler from '../internal/operation-stream' | ||
|
||
import { join } from 'path' | ||
import { parse as parseUrl } from 'node:url' | ||
|
||
import { runEdgeFunction } from '../internal/edge' | ||
import { headersFromEntries, initProxiedHeaders } from '../internal/headers' | ||
import { NodeNextRequest } from 'next/dist/server/base-http/node' | ||
|
||
import type { IncomingMessage } from 'node:http' | ||
import type { RenderData } from 'types/turbopack' | ||
|
||
import chunkGroup from 'INNER_EDGE_CHUNK_GROUP' | ||
import { attachRequestMeta } from '../internal/next-request-helpers' | ||
import { Readable } from 'stream' | ||
|
||
startOperationStreamHandler(async (renderData: RenderData, respond) => { | ||
const { response } = await runOperation(renderData) | ||
|
||
if (response == null) { | ||
throw new Error('no html returned') | ||
} | ||
|
||
const channel = respond({ | ||
status: response.status, | ||
// @ts-expect-error Headers is iterable since node.js 18 | ||
headers: [...response.headers], | ||
}) | ||
|
||
if (response.body) { | ||
const reader = response.body.getReader() | ||
for (;;) { | ||
let { done, value } = await reader.read() | ||
if (done) { | ||
break | ||
} | ||
channel.chunk(Buffer.from(value!)) | ||
} | ||
} | ||
|
||
channel.end() | ||
}) | ||
|
||
async function runOperation(renderData: RenderData) { | ||
const edgeInfo = { | ||
name: 'edge', | ||
paths: chunkGroup.map((chunk: string) => | ||
join(process.cwd(), '.next/server/app', chunk) | ||
), | ||
wasm: [], | ||
env: Object.keys(process.env), | ||
assets: [], | ||
} | ||
|
||
const parsedUrl = parseUrl(renderData.originalUrl, true) | ||
const incoming = new Readable() as IncomingMessage | ||
incoming.push(null) | ||
incoming.url = renderData.originalUrl | ||
incoming.method = renderData.method | ||
incoming.headers = initProxiedHeaders( | ||
headersFromEntries(renderData.rawHeaders), | ||
renderData.data?.serverInfo | ||
) | ||
const req = new NodeNextRequest(incoming) | ||
attachRequestMeta(req, parsedUrl, req.headers.host!) | ||
|
||
const res = await runEdgeFunction({ | ||
edgeInfo, | ||
outputDir: 'edge-pages', | ||
req, | ||
query: renderData.rawQuery, | ||
params: renderData.params, | ||
path: renderData.path, | ||
onWarning(warning) { | ||
console.warn(warning) | ||
}, | ||
}) | ||
|
||
return res as { response: Response } | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/next-swc/crates/next-core/js/src/entry/app/app-entry.tsx
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 @@ | ||
// This file is generated by app_source.rs |
Oops, something went wrong.