-
Notifications
You must be signed in to change notification settings - Fork 381
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 5 commits
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 |
---|---|---|
|
@@ -464,38 +464,43 @@ function amp_register_default_scripts( $wp_scripts ) { | |
] | ||
); | ||
|
||
// Get all AMP components as defined in the spec. | ||
$extensions = []; | ||
// Register all AMP components as defined in the spec. | ||
foreach ( AMP_Allowed_Tags_Generated::get_allowed_tag( 'script' ) as $script_spec ) { | ||
if ( isset( $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']['name'], $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']['version'] ) ) { | ||
$versions = $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']['version']; | ||
array_pop( $versions ); | ||
$extensions[ $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']['name'] ] = array_pop( $versions ); | ||
if ( ! isset( $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec'] ) ) { | ||
continue; | ||
} | ||
} | ||
|
||
if ( isset( $extensions['amp-carousel'] ) ) { | ||
/* | ||
* The 0.2 version of amp-carousel depends on the amp-base-carousel component, but this is still experimental. | ||
* Also, the validator spec does not currently specify what base dependencies a given component has. | ||
* @todo Revisit once amp-base-carousel is no longer experimental. Add support for obtaining a list of extensions that depend on other extensions to include in the script dependencies when registering below. | ||
*/ | ||
$extensions['amp-carousel'] = '0.1'; | ||
} | ||
$extension_spec = $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']; | ||
$versions = array_filter( | ||
$extension_spec['version'], | ||
function ( $version ) { | ||
// @todo Why are we including the latest version in the generated spec in the first place? We can just include the highest number version. | ||
return 'latest' !== $version; | ||
westonruter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
); | ||
|
||
foreach ( $extensions as $extension => $version ) { | ||
$src = sprintf( | ||
'https://cdn.ampproject.org/v0/%s-%s.js', | ||
$extension, | ||
$version | ||
$extension_spec['name'], | ||
array_pop( $versions ) | ||
); | ||
|
||
$wp_scripts->add( | ||
$extension, | ||
$extension_spec['name'], | ||
$src, | ||
[ 'amp-runtime' ], | ||
null | ||
); | ||
|
||
$wp_scripts->add_data( $extension_spec['name'], 'amp_requires_usage', ! empty( $extension_spec['requires_usage'] ) ); | ||
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. I'm not super happy with what I did here. The post-processor shouldn't be looking at the 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. How about even moving the entire registration to a point where we know what is needed and what is not? That would avoid registering scripts we don't need, and you could have all checks against the spec in one place... 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. These scripts are needing to be registered in |
||
} | ||
|
||
if ( $wp_scripts->query( 'amp-carousel', 'registered' ) ) { | ||
/* | ||
* The 0.2 version of amp-carousel depends on the amp-base-carousel component, but this is still experimental. | ||
* Also, the validator spec does not currently specify what base dependencies a given component has. | ||
* @todo Revisit once amp-base-carousel is no longer experimental. Add support for obtaining a list of extensions that depend on other extensions to include in the script dependencies when registering below. | ||
*/ | ||
$wp_scripts->registered['amp-carousel']->src = 'https://cdn.ampproject.org/v0/amp-carousel-0.1.js'; | ||
} | ||
} | ||
|
||
|
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.
The Python parser should probably be smarter about extracting all of the extension specs up front to then provide them in a single call, like
AMP_Allowed_Tags_Generated::get_extension_specs()
.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.
I think we should at one point rethink the way the spec is parsed. I've created an issue for that: #3730
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.
I love that.
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.
While we plan out improving how the spec is parsed, I've implemented a
AMP_Allowed_Tags_Generated::get_extension_specs()
helper to improve things in the meantime. See 9956c90.