Skip to content

Commit b0a04d8

Browse files
authored
chore(rsc): Extract importRsdwServer() and move type assertions to utils (#11688)
1 parent 02341ce commit b0a04d8

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

packages/router/src/rsc/clientSsr.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import path from 'node:path'
22

3-
import type { default as RSDWClientModule } from 'react-server-dom-webpack/client.edge'
4-
import type { default as RSDWServerModule } from 'react-server-dom-webpack/server.edge'
5-
63
import { getPaths } from '@redwoodjs/project-config'
74

85
import { moduleMap } from './ssrModuleMap.js'
9-
import { importRsdwClient, importReact } from './utils.js'
6+
import { importRsdwClient, importRsdwServer, importReact } from './utils.js'
107
import { makeFilePath } from './utils.js'
118

12-
type RSDWClientType = typeof RSDWClientModule
13-
type RSDWServerType = typeof RSDWServerModule
14-
159
async function getEntries() {
1610
const entriesPath = getPaths().web.distRscEntries
1711
const entries = await import(makeFilePath(entriesPath))
@@ -110,17 +104,7 @@ export async function renderRoutesSsr(pathname: string) {
110104
)
111105

112106
const { createElement } = await importReact()
113-
114-
// We need to do this weird import dance because we need to import a version
115-
// of react-server-dom-webpack/server.edge that has been built with the
116-
// `react-server` condition. If we just did a regular import, we'd get the
117-
// generic version in node_modules, and it'd throw an error about not being
118-
// run in an environment with the `react-server` condition.
119-
const dynamicImport = ''
120-
const { renderToReadableStream }: RSDWServerType = await import(
121-
/* @vite-ignore */
122-
dynamicImport + 'react-server-dom-webpack/server.edge'
123-
)
107+
const { renderToReadableStream } = await importRsdwServer()
124108

125109
console.log('clientSsr.ts right before renderToReadableStream')
126110
// We're in clientSsr.ts, but we're supposed to be pretending we're in the
@@ -132,7 +116,7 @@ export async function renderRoutesSsr(pathname: string) {
132116
// react-server-dom-webpack/client.edge that uses the same bundled version
133117
// of React as all the client components. Also see comment in
134118
// streamHelpers.ts about the rd-server import for some more context
135-
const { createFromReadableStream }: RSDWClientType = await importRsdwClient()
119+
const { createFromReadableStream } = await importRsdwClient()
136120

137121
// Here we use `createFromReadableStream`, which is equivalent to
138122
// `createFromFetch` as used in the browser

packages/router/src/rsc/utils.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import path from 'node:path'
22
import { pathToFileURL } from 'node:url'
33

4+
import type { default as RSDWClientModule } from 'react-server-dom-webpack/client.edge'
5+
import type { default as RSDWServerModule } from 'react-server-dom-webpack/server.edge'
6+
47
import { getPaths } from '@redwoodjs/project-config'
58

9+
type RSDWClientType = typeof RSDWClientModule
10+
type RSDWServerType = typeof RSDWServerModule
11+
612
export function makeFilePath(path: string) {
713
return pathToFileURL(path).href
814
}
915

1016
/**
11-
*
1217
* See vite/streamHelpers.ts.
1318
*
1419
* This function ensures we load the same version of rsdw_client to prevent multiple instances of React
@@ -21,16 +26,28 @@ export async function importReact() {
2126
}
2227

2328
/**
24-
*
2529
* See vite/streamHelpers.ts.
2630
*
2731
* This function ensures we load the same version of rsdw_client to prevent multiple instances of React
2832
*/
29-
export async function importRsdwClient() {
33+
export async function importRsdwClient(): Promise<RSDWClientType> {
3034
const distSsr = getPaths().web.distSsr
3135
const rsdwClientPath = makeFilePath(
3236
path.join(distSsr, '__rwjs__rsdw-client.mjs'),
3337
)
3438

3539
return (await import(rsdwClientPath)).default
3640
}
41+
42+
export async function importRsdwServer(): Promise<RSDWServerType> {
43+
// We need to do this weird import dance because we need to import a version
44+
// of react-server-dom-webpack/server.edge that has been built with the
45+
// `react-server` condition. If we just did a regular import, we'd get the
46+
// generic version in node_modules, and it'd throw an error about not being
47+
// run in an environment with the `react-server` condition.
48+
const dynamicImport = ''
49+
return import(
50+
/* @vite-ignore */
51+
dynamicImport + 'react-server-dom-webpack/server.edge'
52+
)
53+
}

0 commit comments

Comments
 (0)