Skip to content

Commit 4c7c108

Browse files
committed
fix: when ssg is enabled, the default render mode should be stream
1 parent c8f93d5 commit 4c7c108

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

packages/cli/plugin-ssg/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export const createServer = async (
120120
method: 'GET',
121121
headers: {
122122
host: 'localhost',
123+
'x-modern-ssg-render': 'true',
123124
},
124125
});
125126

packages/runtime/plugin-runtime/src/cli/code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function getSSRMode(
2727
const { ssr, ssrByEntries } = config.server;
2828

2929
if (config.output.ssg || config.output.ssgByEntries) {
30-
return 'string';
30+
return 'stream';
3131
}
3232

3333
return checkSSRMode(ssrByEntries?.[entry] || ssr);

packages/runtime/plugin-runtime/src/core/server/requestHandler.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ function createSSRContext(
143143
config.ssr,
144144
config.ssrByEntries,
145145
);
146-
const ssrMode = getSSRMode(ssrConfig);
146+
let ssrMode = getSSRMode(ssrConfig);
147+
148+
// Force stream mode for SSG rendering
149+
const isSsgRender = headers.get('x-modern-ssg-render') === 'true';
150+
if (isSsgRender) {
151+
ssrMode = 'stream';
152+
}
147153

148154
const loaderFailureMode =
149155
typeof ssrConfig === 'object' ? ssrConfig.loaderFailureMode : undefined;

packages/runtime/plugin-runtime/src/core/server/stream/createReadableStream.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
3636
// When a crawler visit the page, we should waiting for entrie content of page
3737

3838
const isbot = checkIsBot(request.headers.get('user-agent'));
39-
const onReady = isbot || forceStream2String ? 'onAllReady' : 'onShellReady';
39+
const isSsgRender = request.headers.get('x-modern-ssg-render') === 'true';
40+
const onReady =
41+
isbot || isSsgRender || forceStream2String
42+
? 'onAllReady'
43+
: 'onShellReady';
4044

4145
const internalRuntimeContext = getGlobalInternalRuntimeContext();
4246
const hooks = internalRuntimeContext.hooks;

packages/runtime/plugin-runtime/src/core/server/stream/createReadableStream.worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
5656
});
5757

5858
const isbot = checkIsBot(request.headers.get('user-agent'));
59-
if (isbot) {
60-
// However, when a crawler visits your page, or if you’re generating the pages at the build time,
59+
const isSsgRender = request.headers.get('x-modern-ssg-render') === 'true';
60+
if (isbot || isSsgRender) {
61+
// However, when a crawler visits your page, or if you're generating the pages at the build time,
6162
// you might want to let all of the content load first and then produce the final HTML output instead of revealing it progressively.
6263
// from: https://react.dev/reference/react-dom/server/renderToReadableStream#handling-different-errors-in-different-ways
6364
await readableOriginal.allReady;

0 commit comments

Comments
 (0)