Skip to content

Commit 6570cf0

Browse files
authored
Merge pull request #7354 from maiieul/fix-over-prefetching
fix(rollup): prevent over-prefetching
2 parents f57e049 + 99dd625 commit 6570cf0

File tree

1 file changed

+38
-2
lines changed
  • packages/qwik/src/optimizer/src/plugins

1 file changed

+38
-2
lines changed

packages/qwik/src/optimizer/src/plugins/plugin.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,44 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
875875
// order by discovery time, so that related segments are more likely to group together
876876
function manualChunks(id: string, { getModuleInfo }: Rollup.ManualChunkMeta) {
877877
const module = getModuleInfo(id)!;
878-
const segment = module.meta.segment as SegmentAnalysis | undefined;
879-
return segment?.entry;
878+
const segment = module.meta.segment;
879+
880+
if (segment) {
881+
// We need to specifically return segment.entry for qwik-insights
882+
return segment.entry;
883+
}
884+
885+
// To prevent over-prefetching, we need to clearly seperate those chunks,
886+
// otherwise rollup can bundle them together with the first component chunk it finds.
887+
// For example, the core code could go into an Accordion.tsx chunk, which would make the whole app import accordion related chunks everywhere.
888+
if (/\/(qwik|core)\/dist\/core.*js$/.test(id)) {
889+
return 'core';
890+
}
891+
if (/\/(qwik-city|router)\/lib\/index.qwik.*js$/.test(id)) {
892+
return 'qwik-city';
893+
}
894+
if (id.endsWith('vite/preload-helper.js')) {
895+
return 'preload-helper';
896+
}
897+
898+
// We can't return a chunk for each module as that creates too many small chunks that slow down the prefetching as well,
899+
// nor can we bundle related node_modules together (e.g. all shiki modules together), as that can create very big 10MB chunks.
900+
// So here we let rollup do its job.
901+
if (id.includes('node_modules')) {
902+
return null;
903+
}
904+
905+
// Also to prevent over-prefetching, we must clearly separate those chunks so that rollup doesn't add additional imports into entry files.
906+
// We do this after the node_modules check, because some node_modules can end with .js, .ts, etc.
907+
if (/\.(qwik\.mjs|qwik\.cjs|tsx|jsx|mdx|ts|js)$/.test(id)) {
908+
const optimizer = getOptimizer();
909+
const path = optimizer.sys.path;
910+
const relativePath = path.relative(optimizer.sys.cwd(), id);
911+
const sanitizedPath = relativePath.replace(/^\/+/, '').replace(/\//g, '-');
912+
return sanitizedPath;
913+
}
914+
915+
return null;
880916
}
881917

882918
return {

0 commit comments

Comments
 (0)