Skip to content

Commit abd3042

Browse files
authored
feat: optimize js loading for streaming ssr (#7887)
1 parent a641eb8 commit abd3042

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

packages/cli/builder/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export {
3131

3232
export {
3333
RUNTIME_CHUNK_NAME,
34+
RUNTIME_CHUNK_REGEX,
3435
SERVICE_WORKER_ENVIRONMENT_NAME,
3536
isHtmlDisabled,
3637
castArray,

packages/cli/builder/src/plugins/runtimeChunk.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RsbuildPlugin } from '@rsbuild/core';
2-
import { RUNTIME_CHUNK_NAME } from '../shared/utils';
2+
import { RUNTIME_CHUNK_NAME, RUNTIME_CHUNK_REGEX } from '../shared/utils';
33

44
export const pluginRuntimeChunk = (
55
disableInlineRuntimeChunk?: boolean,
@@ -30,12 +30,8 @@ export const pluginRuntimeChunk = (
3030
return;
3131
}
3232

33-
// RegExp like /bundler-runtime([.].+)?\.js$/
34-
// matches bundler-runtime.js and bundler-runtime.123456.js
35-
const regexp = new RegExp(`${RUNTIME_CHUNK_NAME}([.].+)?\\.js$`);
36-
3733
if (!config.output.inlineScripts) {
38-
config.output.inlineScripts = regexp;
34+
config.output.inlineScripts = RUNTIME_CHUNK_REGEX;
3935
}
4036
});
4137
},

packages/cli/builder/src/shared/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import browserslist from 'browserslist';
33

44
export const RUNTIME_CHUNK_NAME = 'builder-runtime';
55

6+
// RegExp like /builder-runtime([.].+)?\.js$/
7+
export const RUNTIME_CHUNK_REGEX = new RegExp(
8+
`${RUNTIME_CHUNK_NAME}([.].+)?\\.js$`,
9+
);
10+
611
export const SERVICE_WORKER_ENVIRONMENT_NAME = 'workerSSR';
712

813
export const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/;

packages/solutions/app-tools/src/builder/shared/builderPlugins/adapterSSR.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const builderPluginAdapterSSR = (
2929
api.modifyRsbuildConfig(config => {
3030
return mergeRsbuildConfig(config, {
3131
html: {
32-
inject: isStreamingSSR(normalizedConfig) ? 'body' : undefined,
32+
inject: isStreamingSSR(normalizedConfig) ? 'head' : undefined,
3333
},
3434
server: {
3535
// the http-compression can't handler stream http.

packages/solutions/app-tools/src/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Rspack } from '@modern-js/builder';
2+
import { RUNTIME_CHUNK_REGEX } from '@modern-js/builder';
23

34
export class HtmlAsyncChunkPlugin {
45
name: string;
@@ -15,19 +16,37 @@ export class HtmlAsyncChunkPlugin {
1516
const hooks = this.htmlWebpackPlugin.getHooks(compilation as any);
1617

1718
hooks.alterAssetTagGroups.tap(this.name, assets => {
18-
const tags = [...assets.headTags, ...assets.bodyTags];
19+
const headTags: typeof assets.headTags = [];
20+
const bodyTags: typeof assets.bodyTags = [];
1921

20-
for (const tag of tags) {
22+
const processScriptTag = (tag: (typeof assets.headTags)[0]) => {
23+
const { attributes } = tag;
24+
25+
// Convert defer to async
26+
if (attributes && attributes.defer === true) {
27+
attributes.async = true;
28+
delete attributes.defer;
29+
}
30+
31+
const src = attributes?.src as string | undefined;
32+
const isRuntimeChunk = src && RUNTIME_CHUNK_REGEX.test(src);
33+
34+
return isRuntimeChunk ? bodyTags : headTags;
35+
};
36+
37+
for (const tag of [...assets.headTags, ...assets.bodyTags]) {
2138
if (tag.tagName === 'script') {
22-
const { attributes } = tag;
23-
if (attributes && attributes.defer === true) {
24-
attributes.async = true;
25-
delete attributes.defer;
26-
}
39+
processScriptTag(tag).push(tag);
40+
} else {
41+
(assets.headTags.includes(tag) ? headTags : bodyTags).push(tag);
2742
}
2843
}
2944

30-
return assets;
45+
return {
46+
...assets,
47+
headTags,
48+
bodyTags,
49+
};
3150
});
3251
});
3352
}

0 commit comments

Comments
 (0)