Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular/ssr): simplify preload append logic in metadata #29550

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,21 +327,31 @@ function appendPreloadToMetadata(
metadata: ServerConfigRouteTreeNodeMetadata,
includeDynamicImports: boolean,
): void {
if (!entryPointToBrowserMapping) {
const existingPreloads = metadata.preload ?? [];
if (!entryPointToBrowserMapping || existingPreloads.length >= MODULE_PRELOAD_MAX) {
return;
}

const preload = entryPointToBrowserMapping[entryName];
if (!preload?.length) {
return;
}

// Merge existing preloads with new ones, ensuring uniqueness and limiting the total to the maximum allowed.
const combinedPreloads: Set<string> = new Set(existingPreloads);
for (const { dynamicImport, path } of preload) {
if (dynamicImport && !includeDynamicImports) {
continue;
}

combinedPreloads.add(path);

if (preload?.length) {
// Merge existing preloads with new ones, ensuring uniqueness and limiting the total to the maximum allowed.
const preloadPaths =
preload
.filter(({ dynamicImport }) => includeDynamicImports || !dynamicImport)
.map(({ path }) => path) ?? [];
const combinedPreloads = [...(metadata.preload ?? []), ...preloadPaths];
metadata.preload = Array.from(new Set(combinedPreloads)).slice(0, MODULE_PRELOAD_MAX);
if (combinedPreloads.size === MODULE_PRELOAD_MAX) {
break;
}
}

metadata.preload = Array.from(combinedPreloads);
}

/**
Expand Down