Skip to content

Commit

Permalink
Split preconnect and dns-prefetch into two separate links (#1065)
Browse files Browse the repository at this point in the history
* Split preconnect and dns-prefetch into two separate links

* Ensure dns-prefetch is inserted after preconnect

* Adapt expected output for spec tests
  • Loading branch information
schlessera authored Dec 15, 2020
1 parent b3193d7 commit 5049b3d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
12 changes: 10 additions & 2 deletions packages/optimizer/lib/transformers/GoogleFontsPreconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,22 @@ class GoogleFontsPreconnect {
if (this.isGoogleFontsLinkNode_(node)) {
// Preconnect to fonts.gstatic.com, where the final fonts are downloaded.
const linkPreconnect = createElement('link', {
rel: 'dns-prefetch preconnect',
rel: 'preconnect',
href: 'https://fonts.gstatic.com',
crossorigin: '',
});
const linkDnsPrefetch = createElement('link', {
rel: 'dns-prefetch',
href: 'https://fonts.gstatic.com',
});
const referenceNode = findMetaViewport(head);
insertAfter(head, linkPreconnect, referenceNode);
this.log_.debug(
'adding <link rel="dns=prefetch preconnect" href="' + linkPreconnect.attribs.href + '">'
'adding <link rel="preconnect" href="' + linkPreconnect.attribs.href + '" crossorigin>'
);
insertAfter(head, linkDnsPrefetch, linkPreconnect);
this.log_.debug(
'adding <link rel="dns-prefetch" href="' + linkDnsPrefetch.attribs.href + '">'
);

// We only need 1 preconnect, so we can skip the remaining elements and return.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1">
<link rel="dns-prefetch preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1">
<link rel="dns-prefetch preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<html >
<head>
<meta name="viewport" content="width=device-width,minimum-scale=1">
<link rel="dns-prefetch preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;
Expand Down

0 comments on commit 5049b3d

Please sign in to comment.