Skip to content

Commit

Permalink
fix: skip prenonnect when HTML is disabled (#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Apr 7, 2024
1 parent 974863d commit db67c92
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions packages/core/src/plugins/networkPerformance.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import { isHtmlDisabled } from '@rsbuild/shared';
import type { RsbuildPlugin } from '../types';

export const pluginNetworkPerformance = (): RsbuildPlugin => ({
name: 'rsbuild:network-performance',

setup(api) {
api.modifyBundlerChain(
async (chain, { CHAIN_ID, isServer, isWebWorker, isServiceWorker }) => {
const config = api.getNormalizedConfig();
const {
performance: { dnsPrefetch, preconnect },
} = config;
api.modifyBundlerChain(async (chain, { CHAIN_ID, target }) => {
const config = api.getNormalizedConfig();
const {
performance: { dnsPrefetch, preconnect },
} = config;

if (isServer || isWebWorker || isServiceWorker) {
return;
}
if (isHtmlDisabled(config, target)) {
return;
}

const { HtmlNetworkPerformancePlugin } = await import(
'../rspack/HtmlNetworkPerformancePlugin'
);
const { HtmlNetworkPerformancePlugin } = await import(
'../rspack/HtmlNetworkPerformancePlugin'
);

if (dnsPrefetch) {
chain
.plugin(CHAIN_ID.PLUGIN.HTML_DNS_PREFETCH)
.use(HtmlNetworkPerformancePlugin, [dnsPrefetch, 'dnsPrefetch']);
}
if (dnsPrefetch) {
chain
.plugin(CHAIN_ID.PLUGIN.HTML_DNS_PREFETCH)
.use(HtmlNetworkPerformancePlugin, [dnsPrefetch, 'dnsPrefetch']);
}

if (preconnect) {
chain
.plugin(CHAIN_ID.PLUGIN.HTML_PRECONNECT)
.use(HtmlNetworkPerformancePlugin, [preconnect, 'preconnect']);
}
},
);
if (preconnect) {
chain
.plugin(CHAIN_ID.PLUGIN.HTML_PRECONNECT)
.use(HtmlNetworkPerformancePlugin, [preconnect, 'preconnect']);
}
});
},
});

0 comments on commit db67c92

Please sign in to comment.