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

Remove duplicate and unused AMP component scripts from the response #3489

Closed
westonruter opened this issue Oct 12, 2019 · 7 comments · Fixed by #3705
Closed

Remove duplicate and unused AMP component scripts from the response #3489

westonruter opened this issue Oct 12, 2019 · 7 comments · Fixed by #3705
Assignees
Labels
Enhancement New feature or improvement of an existing one QA passed Has passed QA and is done Sanitizers
Milestone

Comments

@westonruter
Copy link
Member

westonruter commented Oct 12, 2019

Feature description

If you include more than one copy of an AMP component script on a page, e.g. amp-form, then the validator gives a warning:

image

In the same way, if there are scripts on the page which are not used, a validation error is also raised:

image

These issues come up often in the Reader mode templates which don't send the entire templates through the post-processor (which will be fixed in #2202), but it can also happen in Standard/Transitional mode when duplicate scripts are manually output in wp_head or wp_footer actions (which normally should not be done since the plugin adds them already).

Cf. #3253 (comment)


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

The AMP_Theme_Support::ensure_required_markup() method is passed a list of $script_handles for the AMP scripts that are required to be in the page. If those scripts are not already present in the DOM, then they are added:

// Create scripts for any components discovered from output buffering.
foreach ( array_diff( $script_handles, array_keys( $amp_scripts ) ) as $missing_script_handle ) {
if ( ! wp_script_is( $missing_script_handle, 'registered' ) ) {
continue;
}
$attrs = [
'src' => wp_scripts()->registered[ $missing_script_handle ]->src,
'async' => '',
];
if ( 'amp-mustache' === $missing_script_handle ) {
$attrs['custom-template'] = $missing_script_handle;
} else {
$attrs['custom-element'] = $missing_script_handle;
}
$amp_scripts[ $missing_script_handle ] = AMP_DOM_Utils::create_node( $dom, 'script', $attrs );
}

There are two things missing here:

  1. If there is more than one copy of a given script on the page, the duplicates need to be removed.
  2. If there are AMP scripts present on the page which are not present in $script_handles then they should be removed, as otherwise there can be an AMP validation error (as they are unused).

QA testing instructions

  • Add a "Custom HTML" block to a post.
  • Paste the following component into the block:
     <script async custom-element="amp-kaltura-player" src="https://cdn.ampproject.org/v0/amp-kaltura-player-0.1.js"></script>
  • Go to the non-AMP version of the page.
  • View the source of the page.
  • Verify: The "amp-kaltura-player" script can be found in the source of the page.
  • Go to the AMP version of the page.
  • View the source of the page.
  • Verify: The "amp-kaltura-player" script cannot be found in the source of the page.

Demo

Changelog entry

@westonruter westonruter added Enhancement New feature or improvement of an existing one Sanitizers labels Oct 12, 2019
@westonruter westonruter added this to the v1.3.1 milestone Oct 13, 2019
@swissspidy swissspidy modified the milestones: v1.3.1, v1.4 Oct 17, 2019
@westonruter westonruter removed this from the v1.4 milestone Oct 17, 2019
@kienstra kienstra self-assigned this Oct 30, 2019
@kienstra
Copy link
Contributor

kienstra commented Oct 30, 2019

Hi @westonruter,
Hope you had a great day.

Would the duplicate component scripts, like in the screenshot above, be output with an amp_post_template_head hook, like this?

add_action( 
        'amp_post_template_head', 
        function() {
                // Echo component scripts here.
        }
);

It looks like the recommended way of adding component scripts won't normally have duplicates.

Thanks, Weston!

@kienstra kienstra removed their assignment Oct 31, 2019
@westonruter westonruter changed the title Remove duplicate AMP component scripts from the response Remove duplicate and unused AMP component scripts from the response Nov 4, 2019
@westonruter
Copy link
Member Author

@kienstra Sorry for the delay. They could be added in that way, yes, but this won't be the concern in this issue at the moment. Handling scripts added in Reader mode would be fixed as part of #2202, as the entire document would be then sent through the post-processor.

So for this issue, focus on scripts that are added in Standard/Transitional mode. That means scripts that are added at the wp_head or wp_footer actions. I've updated the summary of the issue and provided an implementation brief. The scope is also expanded to include not just duplicate script but also unused scripts.

@kienstra
Copy link
Contributor

kienstra commented Nov 4, 2019

Thanks, @westonruter! No problem, I didn't expect or need a quick response, with the stable release and WCUS this past week.

@schlessera schlessera self-assigned this Nov 10, 2019
@schlessera
Copy link
Collaborator

I'm currently working on this and I'll do two separate PRs, as both the implementation as well as the ramifications are different for the two cases enumerated above.

@westonruter
Copy link
Member Author

I'm currently working on this and I'll do two separate PRs, as both the implementation as well as the ramifications are different for the two cases enumerated above.

The other PR would be about removing the duplicates? It seems to me that the existing PR could include the de-duplication as well.

@schlessera
Copy link
Collaborator

schlessera commented Nov 14, 2019

The other PR would be about removing the duplicates? It seems to me that the existing PR could include the de-duplication as well.

The thinking was that removing duplicates is a no-brainer, always fixing and never breaking. However, removing unused AMP scripts is more complicated and could potentially lead to us automatically breaking an otherwise working page, because we're missing something.

Therefore, splitting this into two PRs would mean we'll be able to merge one either way, while the other might need to be put on hold.

@westonruter westonruter added this to the v1.5 milestone Nov 15, 2019
@csossi
Copy link

csossi commented Nov 19, 2019

Verified in QA

@csossi csossi added the QA passed Has passed QA and is done label Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or improvement of an existing one QA passed Has passed QA and is done Sanitizers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants