Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor(core): don't duplicate styles when adding the same host mult…
…iple times

This shouldn't have been happening before, but now that we're going to start calling `addHost` during root component boostrap, we may call `addHost` on the same node multiple times and don't want to duplicate styles.
  • Loading branch information
dgp1130 committed Apr 9, 2026
commit 8ff9efd4553afe1fa3a205991951dea8ca9d74b4
2 changes: 2 additions & 0 deletions packages/platform-browser/src/dom/shared_styles_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ export class SharedStylesHost implements ɵSharedStylesHost, OnDestroy {
}

addHost(hostNode: Node): void {
if (this.hosts.has(hostNode)) return;

this.hosts.add(hostNode);
Comment thread
dgp1130 marked this conversation as resolved.

// Add existing styles to new host
Expand Down
16 changes: 16 additions & 0 deletions packages/platform-browser/test/dom/shared_styles_host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ describe('SharedStylesHost', () => {
expect(doc.head.innerHTML).toContain('<style ng-style-reused="">a {};</style>');
expect(doc.head.innerHTML).not.toContain('ng-app-id');
});

it('should not duplicate styles when the same host is added multiple times', () => {
ssh.addStyles(['a {};']);
ssh.addHost(doc.head);
ssh.addHost(doc.head);

expect(doc.head.querySelectorAll('style')).toHaveSize(1);
});
});

describe('external', () => {
Expand Down Expand Up @@ -141,6 +149,14 @@ describe('SharedStylesHost', () => {
);
expect(doc.head.innerHTML).not.toContain('ng-app-id');
});

it('should not duplicate styles when the same host is added multiple times', () => {
ssh.addStyles([], ['component-1.css']);
ssh.addHost(doc.head);
ssh.addHost(doc.head);

expect(doc.head.querySelectorAll('link')).toHaveSize(1);
});
});

describe('removeHost', () => {
Expand Down