-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RSC payload t orender server components on server
- Loading branch information
1 parent
a3ad230
commit 3cace71
Showing
17 changed files
with
216 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
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,59 @@ | ||
import fs from 'fs'; | ||
import * as React from 'react'; | ||
import RSDWClient from 'react-server-dom-webpack/client.node'; | ||
import transformRSCStream from './transformRSCNodeStreamAndReplayConsoleLogs'; | ||
import loadJsonFile from './loadJsonFile'; | ||
|
||
if (!('use' in React && typeof React.use === 'function')) { | ||
throw new Error('React.use is not defined. Please ensure you are using React 18 with experimental features enabled or React 19+ to use server components.'); | ||
} | ||
|
||
const { use } = React; | ||
|
||
export type RSCServerRootProps = { | ||
getRscPromise: NodeJS.ReadableStream, | ||
reactClientManifestFileName: string, | ||
reactServerManifestFileName: string, | ||
} | ||
|
||
const createFromFetch = (stream: NodeJS.ReadableStream, ssrManifest: Record<string, unknown>) => { | ||
const transformedStream = transformRSCStream(stream); | ||
return RSDWClient.createFromNodeStream(transformedStream, ssrManifest); | ||
} | ||
|
||
const createSSRManifest = (reactServerManifestFileName: string, reactClientManifestFileName: string) => { | ||
const reactServerManifest = loadJsonFile(reactServerManifestFileName); | ||
const reactClientManifest = loadJsonFile(reactClientManifestFileName); | ||
|
||
const ssrManifest = { | ||
moduleLoading: { | ||
prefix: "/webpack/development/", | ||
crossOrigin: null, | ||
}, | ||
moduleMap: {} as Record<string, unknown>, | ||
}; | ||
|
||
Object.entries(reactClientManifest).forEach(([aboluteFileUrl, clientFileBundlingInfo]) => { | ||
const serverFileBundlingInfo = reactServerManifest[aboluteFileUrl]; | ||
ssrManifest.moduleMap[(clientFileBundlingInfo as { id: string }).id] = { | ||
'*': { | ||
id: (serverFileBundlingInfo as { id: string }).id, | ||
chunks: (serverFileBundlingInfo as { chunks: string[] }).chunks, | ||
name: '*', | ||
} | ||
}; | ||
}); | ||
|
||
return ssrManifest; | ||
} | ||
|
||
const RSCServerRoot = ({ | ||
getRscPromise, | ||
reactClientManifestFileName, | ||
reactServerManifestFileName, | ||
}: RSCServerRootProps) => { | ||
const ssrManifest = createSSRManifest(reactServerManifestFileName, reactClientManifestFileName); | ||
return use(createFromFetch(getRscPromise, ssrManifest)); | ||
}; | ||
|
||
export default RSCServerRoot; |
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,7 +1,10 @@ | ||
import ReactOnRails from './ReactOnRails'; | ||
import streamServerRenderedReactComponent from './streamServerRenderedReactComponent'; | ||
import RSCServerRoot from './RSCServerRoot'; | ||
|
||
ReactOnRails.streamServerRenderedReactComponent = streamServerRenderedReactComponent; | ||
// @ts-expect-error eeee | ||
ReactOnRails.RSCServerRoot = RSCServerRoot; | ||
|
||
export * from './ReactOnRails'; | ||
export { default } from './ReactOnRails'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
const loadedJsonFiles = new Map<string, { [key: string]: unknown; }>(); | ||
|
||
export default function loadJsonFile(fileName: string) { | ||
// Asset JSON files are uploaded to node renderer. | ||
// Renderer copies assets to the same place as the server-bundle.js and rsc-bundle.js. | ||
// Thus, the __dirname of this code is where we can find the manifest file. | ||
const filePath = path.resolve(__dirname, fileName); | ||
if (!loadedJsonFiles.has(filePath)) { | ||
try { | ||
const file = JSON.parse(fs.readFileSync(filePath, 'utf8')); | ||
loadedJsonFiles.set(filePath, file); | ||
} catch (error) { | ||
console.error(`Failed to load JSON file: ${filePath}`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
return loadedJsonFiles.get(filePath)!; | ||
} |
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
import React from 'react'; | ||
import ReactOnRails from './ReactOnRails'; | ||
import RSCServerRoot, { RSCServerRootProps } from './RSCServerRoot'; | ||
import { ReactComponent } from './types'; | ||
|
||
const registerServerComponent = (components: { [id: string]: ReactComponent }) => { | ||
const componentsWrappedInRSCServerRoot = Object.entries(components).reduce( | ||
(acc, [name]) => ({ | ||
...acc, | ||
[name]: (props: RSCServerRootProps) => React.createElement(RSCServerRoot, { | ||
...props, | ||
}) | ||
}), | ||
{} | ||
); | ||
ReactOnRails.register(componentsWrappedInRSCServerRoot); | ||
}; | ||
|
||
export default registerServerComponent; |
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,8 @@ | ||
import ReactOnRails from './ReactOnRails'; | ||
import { ReactComponent } from './types'; | ||
|
||
const registerServerComponent = (components: { [id: string]: ReactComponent }) => { | ||
ReactOnRails.register(components); | ||
}; | ||
|
||
export default registerServerComponent; |
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
31 changes: 31 additions & 0 deletions
31
node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.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,31 @@ | ||
import { Transform } from 'stream'; | ||
|
||
export default function transformRSCStream(stream: NodeJS.ReadableStream): NodeJS.ReadableStream { | ||
const decoder = new TextDecoder(); | ||
let lastIncompleteChunk = ''; | ||
|
||
const htmlExtractor = new Transform({ | ||
transform(oneOrMoreChunks, _, callback) { | ||
try { | ||
const decodedChunk = lastIncompleteChunk + decoder.decode(oneOrMoreChunks); | ||
const separateChunks = decodedChunk.split('\n').filter(chunk => chunk.trim() !== ''); | ||
|
||
if (!decodedChunk.endsWith('\n')) { | ||
lastIncompleteChunk = separateChunks.pop() ?? ''; | ||
} else { | ||
lastIncompleteChunk = ''; | ||
} | ||
|
||
for (const chunk of separateChunks) { | ||
const parsedData = JSON.parse(chunk) as { html: string }; | ||
this.push(parsedData.html); | ||
} | ||
callback(); | ||
} catch (error) { | ||
callback(error as Error); | ||
} | ||
} | ||
}); | ||
|
||
return stream.pipe(htmlExtractor); | ||
} |
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.