Skip to content

Commit 6d72ecd

Browse files
committed
Implements module preinitialization for SSR when using turbopack
1 parent 5aef219 commit 6d72ecd

File tree

21 files changed

+150
-77
lines changed

21 files changed

+150
-77
lines changed

packages/next-swc/crates/next-api/src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl AppProject {
213213
"next-dynamic".to_string(),
214214
Vc::upcast(NextDynamicTransition::new(self.client_transition())),
215215
),
216+
("next-ssr".to_string(), Vc::upcast(self.ssr_transition())),
216217
]
217218
.into_iter()
218219
.collect();

packages/next-swc/crates/next-core/js/src/build/client/app-bootstrap.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import '../shims'
66
import { appBootstrap } from 'next/dist/client/app-bootstrap'
77

88
appBootstrap(() => {
9-
require('./app-turbopack')
109
const { hydrate } = require('./app-index')
1110
hydrate()
1211
})

packages/next-swc/crates/next-core/js/src/build/client/app-turbopack.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/next-swc/crates/next-core/js/src/entry/app-renderer.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { PassThrough } from 'node:stream'
2020
import entry from 'APP_ENTRY'
2121
import BOOTSTRAP from 'APP_BOOTSTRAP'
2222
import { createServerResponse } from '../internal/http'
23-
import { createManifests, installRequireAndChunkLoad } from './app/manifest'
23+
import { createManifests } from './app/manifest'
2424
import { join } from 'node:path'
2525
import { nodeFs } from 'next/dist/server/lib/node-fs-methods'
2626
import { IncrementalCache } from 'next/dist/server/lib/incremental-cache'
@@ -29,8 +29,6 @@ const {
2929
renderToHTMLOrFlight,
3030
} = require('next/dist/compiled/next-server/app-page.runtime.dev')
3131

32-
installRequireAndChunkLoad()
33-
3432
const MIME_TEXT_HTML_UTF8 = 'text/html; charset=utf-8'
3533

3634
startOperationStreamHandler(async (renderData: RenderData, respond) => {
@@ -94,10 +92,6 @@ async function runOperation(renderData: RenderData) {
9492
},
9593
ComponentMod: {
9694
...entry,
97-
__next_app__: {
98-
require: __next_require__,
99-
loadChunk: __next_chunk_load__,
100-
},
10195
pages: ['page.js'],
10296
},
10397
incrementalCache: new IncrementalCache({

packages/next-swc/crates/next-core/js/src/entry/app/edge-page-bootstrap.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import { renderToHTMLOrFlight } from 'next/dist/server/app-render/app-render'
55
;('TURBOPACK { chunking-type: isolatedParallel }')
66
import entry from 'APP_ENTRY'
77
import BOOTSTRAP from 'APP_BOOTSTRAP'
8-
import { createManifests, installRequireAndChunkLoad } from './manifest'
8+
import { createManifests } from './manifest'
99
import type { NextRequest, NextFetchEvent } from 'next/server'
1010
import type { RenderOpts } from 'next/dist/server/app-render/types'
1111
import type { ParsedUrlQuery } from 'querystring'
1212

13-
installRequireAndChunkLoad()
14-
1513
// avoid limiting stack traces to 10 lines
1614
Error.stackTraceLimit = 100
1715

@@ -43,10 +41,6 @@ async function render(request: NextRequest, event: NextFetchEvent) {
4341
},
4442
ComponentMod: {
4543
...entry,
46-
__next_app__: {
47-
require: __next_require__,
48-
loadChunk: __next_chunk_load__,
49-
},
5044
pages: ['page.js'],
5145
},
5246
clientReferenceManifest,

packages/next-swc/crates/next-core/js/src/entry/app/hydrate.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ReactDOMClient from 'react-dom/client'
22
import React, { use } from 'react'
33
import type { ReactElement } from 'react'
44
import { version } from 'next/package.json'
5-
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack/client'
5+
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-turbopack/client'
66
import { callServer } from 'next/dist/client/app-call-server'
77
import { linkGc } from 'next/dist/client/app-link-gc'
88

@@ -19,12 +19,6 @@ window.next = {
1919
appDir: true,
2020
}
2121

22-
globalThis.__next_require__ = (data) => {
23-
const [client_id] = JSON.parse(data)
24-
return __turbopack_require__(client_id)
25-
}
26-
globalThis.__next_chunk_load__ = __turbopack_load__
27-
2822
const appElement = document
2923

3024
const getCacheKey = () => {

packages/next-swc/crates/next-core/js/src/entry/app/manifest.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,3 @@ export function createManifests() {
114114

115115
return { clientReferenceManifest }
116116
}
117-
118-
export function installRequireAndChunkLoad() {
119-
globalThis.__next_require__ = (data) => {
120-
const [, , ssr_id] = JSON.parse(data)
121-
return __turbopack_require__(ssr_id)
122-
}
123-
globalThis.__next_chunk_load__ = () => Promise.resolve()
124-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
declare module 'next/dist/compiled/react-server-dom-webpack/client'
2+
declare module 'next/dist/compiled/react-server-dom-turbopack/client'

packages/next-swc/crates/next-core/js/types/globals.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ declare global {
88
var __webpack_public_path__: string | undefined
99
var __DEV_MIDDLEWARE_MATCHERS: any[]
1010

11-
var __next_require__: (id: string) => any
12-
var __next_chunk_load__: (id: string) => Promise
1311
var __next_f: (
1412
| [isBootStrap: 0]
1513
| [isNotBootstrap: 1, responsePartial: string]

packages/next-swc/crates/next-core/src/app_source.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,12 @@ impl AppRenderer {
968968
formatdoc!(
969969
"
970970
\"TURBOPACK {{ chunking-type: isolatedParallel; transition: {rsc_transition} }}\";
971-
import GlobalErrorMod from \"next/dist/client/components/error-boundary\"
971+
import GlobalErrorMod from \"next/dist/client/components/error-boundary\";
972972
const {{ GlobalError }} = GlobalErrorMod;
973973
\"TURBOPACK {{ chunking-type: isolatedParallel; transition: {rsc_transition} }}\";
974-
import base from \"next/dist/server/app-render/entry-base\"\n
974+
import base from \"next/dist/server/app-render/entry-base\";
975+
\"TURBOPACK {{ chunking-type: isolatedParallel; transition: next-ssr }}\";
976+
import baseSsr from \"next/dist/server/app-render/entry-base-ssr\";\n
975977
"
976978
)
977979
.into_bytes(),
@@ -991,7 +993,7 @@ impl AppRenderer {
991993
result,
992994
// Need this hack because "export *" from CommonJS will trigger a warning
993995
// otherwise
994-
"__turbopack_export_value__({{ tree, GlobalError, pathname, ...base }});\n"
996+
"__turbopack_export_value__({{ tree, GlobalError, pathname, ...base, ...baseSsr }});\n"
995997
)?;
996998

997999
let file = File::from(result.build());

0 commit comments

Comments
 (0)