Skip to content

Commit

Permalink
feat: support preRender
Browse files Browse the repository at this point in the history
  • Loading branch information
answershuto committed Nov 29, 2023
1 parent f926df2 commit 3f89726
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/ice/src/middlewares/renderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ export default function createRenderMiddleware(options: Options): Middleware {
req,
res,
};

// Simulating window object for wormhole.
// @ts-ignore
global.window = {};

serverModule.renderToResponse(requestContext, {
renderMode,
documentOnly,
preRender: true,
streamOptions: {
onAllReady: (params) => {
console.log('middleware onAllReady');
console.log('params', params);
res.write(params.renderAssets.join(','));
},
},
});
}
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/runServerApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface RenderOptions {
documentDataLoader?: DocumentDataLoaderConfig;
Document?: DocumentComponent;
documentOnly?: boolean;
preRender?: boolean;
renderMode?: RenderMode;
// basename is used both for server and client, once set, it will be sync to client.
basename?: string;
Expand Down
15 changes: 14 additions & 1 deletion packages/runtime/src/server/streamRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ export function renderToNodeStream(
renderToNodeStreamOptions: RenderToNodeStreamOptions,
): NodeWritablePiper {
return (res, options) => {
const {
renderOptions,
} = renderToNodeStreamOptions;
const {
preRender = false,
} = renderOptions;
console.log('preRender', preRender);
const { pipe } = ReactDOMServer.renderToPipeableStream(element, {
onShellReady() {
pipe(res);
if (!preRender) {
pipe(res);
}
options?.onShellReady && options.onShellReady();
},
onShellError(error) {
Expand Down Expand Up @@ -66,6 +75,10 @@ export function renderToNodeStream(
options?.onAllReady && options?.onAllReady({
renderAssets,
});

if (preRender) {
pipe(res);
}
},
});
};
Expand Down

0 comments on commit 3f89726

Please sign in to comment.