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

Filter duplicate extensions by src URL #1192

Merged
merged 11 commits into from
Apr 8, 2021
8 changes: 4 additions & 4 deletions packages/optimizer/lib/transformers/ReorderHeadTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class HeadNodes {
}

_removeDuplicateCustomExtensions(extensions) {
const nodesByName = new Map();
const nodesBySrc = new Map();
for (const node of extensions) {
const name = this._getName(node);
nodesByName.set(name, node);
const src = node.attribs['src'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this remove the host to make sure a same extension is not downloaded twice from two different hosts?

nodesBySrc.set(src, node);
}
return Array.from(nodesByName.values());
return Array.from(nodesBySrc.values());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took an alternative approach to this in PHP, to do the deduplication in the registerScript function itself: https://github.com/ampproject/amp-toolbox-php/pull/130/files#diff-b0da2cc892f74bf1ebc7a84e63d7065894b24541a48f3a26089f23860ee1209fR156-R190

This also accounts for module and nomodule variations.


appendToHead(head) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html ⚡>
<head>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0.css">
<script async src="https://cdn.ampproject.org/v0.mjs" type="module" crossorigin="anonymous"></script>

This comment was marked as resolved.

<script async nomodule src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js" custom-element="amp-experiment"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.mjs" type="module" crossorigin="anonymous"></script>
<script async nomodule src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" custom-template="amp-mustache"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.mjs" type="module" crossorigin="anonymous"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the order be reversed, with the nomodule one coming after?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<link href="https://example.com/favicon.ico" rel="icon">
<link rel="preload" href="https://cdn.ampproject.org/v0.css" as="style">
<link href="https://fonts.googleapis.com/css?foobar" rel="stylesheet" type="text/css">
<link as="script" crossorigin="anonymous" href="https://cdn.ampproject.org/v0.mjs" rel="modulepreload">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the link[rel=preload] and link[rel=modulepreload] be moved to the beginning of the head?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@westonruter westonruter Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modulepreload link needs to be moved up along with the preload as well. Ref ampproject/amp-toolbox-php@a3a99fd

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patience taking it up with my empty brain today...

</head>
<body></body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html ⚡>
<head>
<script async nomodule src="https://cdn.ampproject.org/v0.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big problem: The nomodule v0.js script is getting dropped in the output. Here's one thing I had to do in the PHP version to account for this: ampproject/amp-toolbox-php@4bad718

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

<script async src="https://cdn.ampproject.org/v0.mjs" type="module" crossorigin="anonymous"></script>
<script async nomodule src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" custom-template="amp-mustache"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.mjs" type="module" crossorigin="anonymous"></script>
<script async nomodule src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js" custom-element="amp-experiment"></script>
<script async nomodule src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js" custom-element="amp-experiment"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.mjs" type="module" crossorigin="anonymous"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.mjs" type="module" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0.css">
<link href="https://fonts.googleapis.com/css?foobar" rel="stylesheet" type="text/css">
<link href="https://example.com/favicon.ico" rel="icon">
<link as="script" crossorigin="anonymous" href="https://cdn.ampproject.org/v0.mjs" rel="modulepreload">
<link rel="preload" href="https://cdn.ampproject.org/v0.css" as="style">
</head>
<body></body>
</html>