-
Notifications
You must be signed in to change notification settings - Fork 384
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 scripts for components that were not detected in output buffer #3705
Changes from 1 commit
cee8d58
7d1bce5
8744c89
dba4ead
2f9f8b9
d9469bf
879cb17
17d03ad
9956c90
16f35b4
617d17d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1727,7 +1727,7 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles | |
$script->parentNode->removeChild( $script ); // So we can move it. | ||
} | ||
|
||
// Create scripts for any components discovered from output buffering. | ||
// Create scripts for any components discovered from output buffering that are missing. | ||
foreach ( array_diff( $script_handles, array_keys( $amp_scripts ) ) as $missing_script_handle ) { | ||
if ( ! wp_script_is( $missing_script_handle, 'registered' ) ) { | ||
continue; | ||
|
@@ -1745,6 +1745,30 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles | |
$amp_scripts[ $missing_script_handle ] = AMP_DOM_Utils::create_node( $dom, 'script', $attrs ); | ||
} | ||
|
||
// Remove scripts that had already been added but couldn't be detected from output buffering. | ||
foreach ( array_diff( array_keys( $amp_scripts ), $script_handles ) as $superfluous_script_handle ) { | ||
$src = $amp_scripts[ $superfluous_script_handle ]->getAttribute( 'src' ); | ||
|
||
// Skip scripts unrelated to AMP. | ||
if ( ! $src || 0 !== strpos( $src, 'https://cdn.ampproject.org/' ) ) { | ||
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue; | ||
} | ||
|
||
// Skip AMP runtime script. | ||
if ( 'https://cdn.ampproject.org/v0.js' === $src ) { | ||
continue; | ||
} | ||
|
||
$custom_element = $amp_scripts[ $superfluous_script_handle ]->getAttribute( 'custom-element' ); | ||
|
||
// Skip dynamic CSS classes script, it has no associated element. | ||
if ( 'amp-dynamic-css-classes' === $custom_element ) { | ||
continue; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing this single check here, and depending on what the AMP validator team comes back with regarding any list of components, perhaps |
||
|
||
unset( $amp_scripts[ $superfluous_script_handle ] ); | ||
} | ||
|
||
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found | ||
* | ||
* "2. Next, preload the AMP runtime v0.js <script> tag with <link as=script href=https://cdn.ampproject.org/v0.js rel=preload>. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array_diff( array_keys( $amp_scripts ), $script_handles )
may need to bearray_intersect()
'ed with a list of components that are actually prone to cause validation errors. Scripts likeamp-video
andamp-form
don't cause any issue when being included. Perhaps this is due to some having built-in status. We need to find out.In that case, there would be no need for the
amp-dynamic-css-classes
check below either.