Skip to content

Commit 4ff914a

Browse files
committed
fix(@angular/build): allow additional module preloads up to limit
If the module preload limit is not met by shallow (depth 1) initial scripts, deeper initial scripts can now be added. This allows for deeper import graphs to take advantage of the browser's module preloading. Additionally, the limit has been increased to ten now that the module preloads are added at the end of the body along with the actual script elements.
1 parent 461e78f commit 4ff914a

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

packages/angular/build/src/tools/esbuild/index-html-generator.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { BuildOutputFile, BuildOutputFileType, InitialFileRecord } from './bundl
1616
* The maximum number of module preload link elements that should be added for
1717
* initial scripts.
1818
*/
19-
const MODULE_PRELOAD_MAX = 3;
19+
const MODULE_PRELOAD_MAX = 10;
2020

2121
export async function generateIndexHtml(
2222
initialFiles: Map<string, InitialFileRecord>,
@@ -45,27 +45,25 @@ export async function generateIndexHtml(
4545
assert(indexHtmlOptions, 'indexHtmlOptions cannot be undefined.');
4646

4747
if (!externalPackages && indexHtmlOptions.preloadInitial) {
48-
let modulePreloadCount = 0;
48+
const modulePreloads = [];
4949
for (const [key, value] of initialFiles) {
5050
if (value.entrypoint || value.serverFile) {
5151
// Entry points are already referenced in the HTML
5252
continue;
5353
}
5454

55-
// Only add shallow preloads
56-
if (value.depth > 1) {
57-
continue;
58-
}
59-
60-
if (value.type === 'script' && modulePreloadCount < MODULE_PRELOAD_MAX) {
61-
modulePreloadCount++;
62-
hints.push({ url: key, mode: 'modulepreload' as const });
55+
if (value.type === 'script') {
56+
modulePreloads.push({ url: key, mode: 'modulepreload' as const, depth: value.depth });
6357
} else if (value.type === 'style') {
6458
// Provide an "as" value of "style" to ensure external URLs which may not have a
6559
// file extension are treated as stylesheets.
6660
hints.push({ url: key, mode: 'preload' as const, as: 'style' });
6761
}
6862
}
63+
64+
// Limit the number of module preloads with smallest depth given priority
65+
modulePreloads.sort((a, b) => a.depth - b.depth);
66+
hints.push(...modulePreloads.slice(0, MODULE_PRELOAD_MAX));
6967
}
7068

7169
/** Virtual output path to support reading in-memory files. */

0 commit comments

Comments
 (0)