Skip to content

Commit

Permalink
fix(common): use DOCUMENT token to query for preconnect links (#47353)
Browse files Browse the repository at this point in the history
`PreconnectLinkChecker` checks to see if preconnect links have been added to the `<head>` element but uses `document` directly which does not exist when rendering in Angular Universal. This PR switches the `PreconnectLinkChecker` to use the `DOCUMENT` token instead so that the query works when SSR'ing

PR Close #47353
  • Loading branch information
yharaskrik authored and AndrewKushnir committed Sep 6, 2022
1 parent c0d7ac9 commit 1875ce5
Showing 1 changed file with 4 additions and 2 deletions.
Expand Up @@ -49,6 +49,8 @@ export const PRECONNECT_CHECK_BLOCKLIST =
*/
@Injectable({providedIn: 'root'})
export class PreconnectLinkChecker {
private document = inject(DOCUMENT);

/**
* Set of <link rel="preconnect"> tags found on this page.
* The `null` value indicates that there was no DOM query operation performed.
Expand All @@ -66,7 +68,7 @@ export class PreconnectLinkChecker {

constructor() {
assertDevMode('preconnect link checker');
const win = inject(DOCUMENT).defaultView;
const win = this.document.defaultView;
if (typeof win !== 'undefined') {
this.window = win;
}
Expand Down Expand Up @@ -127,7 +129,7 @@ export class PreconnectLinkChecker {
private queryPreconnectLinks(): Set<string> {
const preconnectUrls = new Set<string>();
const selector = 'link[rel=preconnect]';
const links: HTMLLinkElement[] = Array.from(document.querySelectorAll(selector));
const links: HTMLLinkElement[] = Array.from(this.document.querySelectorAll(selector));
for (let link of links) {
const url = getUrl(link.href, this.window!);
preconnectUrls.add(url.origin);
Expand Down

0 comments on commit 1875ce5

Please sign in to comment.