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
Add AMP compatibility #535
Add AMP compatibility #535
Conversation
I'll be honest, this is a lot of AMP-specific implementation detail that I'm not particularly interested in maintaining. I assume that other plugins which need to do the same thing will also be mostly repeating this logic. I'd like to recommend that the AMP plugin provides a declarative integration layer to simplify this, like the existing add_filter( 'amp_dev_mode_scripts', function( $scripts ) {
$scripts[] = 'query-monitor';
return $scripts;
} );
add_filter( 'amp_dev_mode_styles', function( $styles ) {
$styles[] = 'query-monitor';
return $styles;
} ); Then the AMP-specific implementation details (adding the For inline scripts the existing Regarding the AMP validation loopback request, this is another piece of AMP implementation detail that will likely need maintaining, but I can't see an alternative. If the AMP plugin provided a wrapper function for this (eg. |
You're right. We actually have an issue pending for this (ampproject/amp-wp#4598) which will greatly reduce the amount code needed to identify scripts/styles for AMP Dev Mode. We'll make progress on that issue and then update this PR to make use of the new API.
This would mean adding query-monitor/dispatchers/Html.php Lines 325 to 328 in fbde50d
query-monitor/dispatchers/Html.php Lines 584 to 586 in fbde50d
So add the |
Yep, sounds good to me |
dispatchers/Html.php
Outdated
add_filter( | ||
'amp_dev_mode_element_xpaths', | ||
function ( $expressions ) { | ||
$expressions[] = '//script[ @id = "query-monitor-js-extra" ]'; |
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.
Note: This expression will only work in WP≥5.5: WordPress/wordpress-develop@d241ab3
I suppose we could do a version check and fall back to using '//script[ contains( text(), "var qm_number_format =" ) ]'
if on WP<5.5.
@westonruter Fancy seeing you here. Thanks for raising this, we just ran into this ourselves. |
We have an AMP-first site I would love to see this on. |
@rickalee Try this compatibility plugin in the meantime: https://gist.github.com/westonruter/621137b5a5ae1caaaee48c63f61ce7b7 |
I would note that AMP validator flags td scope="row" within Query Monitor tables as invalid as well. |
Ah, well that appears to be a bug in Query Monitor, as the use of the Nevertheless, I'm not seeing Query Monitor emit any such markup: |
There were a couple of instances of |
Thanks for the work on this but I'm going to close it off. It's a fair amount of implementation detail that I'm not interested in maintaining, there's never been any other requests to support the AMP plugin, and there is a compatibility plugin available from Weston for those who need it. Cheers! |
This PR is revisiting one I previously closed (#332) which prevented Query Monitor from doing anything on AMP pages. This is no longer necessary now that AMP Dev Mode is a thing: Query Monitor's full functionality can exist on AMP pages. This follows up on a discussion that we had with @schlessera around a table during the WordFest Party at WCUS 2019.
When the official AMP plugin is active on a site, the functionality provided by Query Monitor is broken on AMP pages. For example, when looking at an AMP page and AMP Developer Tools is enabled, the Admin Bar shows AMP validation errors being reported and that the AMP CSS limit is close to being breached:
When clicking the Query Monitor admin bar menu item, nothing happens:
This is expected because the invalid AMP markup is being removed by the plugin, which is indicated by the raising of validation errors. Clicking to validate the URL to show the validation results shows 5
script
elements that are getting removed due to being invalid AMP (which doesn't permit custom scripts in the traditional way):The last two inline scripts lack attribution detection from the AMP plugin, but the penultimate inline script is coming from
QM_Dispatcher_Html::before_output()
which printsvar qm = {...}
, and the last inline script is theload
event handler on thewindow
that is also printed in the same method.The validation errors for these scripts can be avoided by designating them as being part of AMP Dev Mode. This allows for AMP-invalid scripts (and other elements) from being sanitized when a page is in AMP Dev Mode which happens by default when the user is logged-in and the Admin Bar is showing. Designating elements to be exempted from sanitization is done by adding the
data-ampdevmode
attribute. This can either be done during template rendering, such as via thescript_loader_tag
filter, or afterward during post-processing via theamp_dev_mode_element_xpaths
filter.When those are added, no more validation errors are reported:
In addition to designating invalid scripts as being part of AMP Dev Mode, this should also be done for stylesheets that are related to those scripts which only appear in Dev Mode. This will prevent validation errors due to going over the CSS limit but it will also prevent required CSS rules from being removed during tree shaking. So this PR also designates
query-monitor
anddashicons
as also being part of AMP Dev Mode so that they are omitted from the AMP plugin's CSS processing. Notice how the CSS usage is decreased:Another key difference to point out here is that before Query Monitor is showing up as the source for various stylesheets that it is definitely not responsible for registering:
wp-block-library-theme
,wp-block-library
, andamp-default
. This is prevented in this PR with the addition of this bit of code inquery-monitor.php
:I couldn't determine a better place for this logic. The condition isn't particularly robust either, but I was unsure of how else to validate it being a valid AMP validation request so early. (One idea could be to check if
hash_equals( $_GET['amp_validate'], wp_hash( self::VALIDATE_QUERY_VAR . wp_nonce_tick(), 'nonce' ) )
but this would require loadingpluggable.php
too early.)When clicking “Validate URL” in the AMP plugin, a loopback request is initiated in which the
amp_validate
query param is added to indicate that an AMP validation request is being made. See logic inAMP_Validation_Manager::validate_url()
. As part of a validation request, the AMP plugin adds similar logic that Query Monitor adds to determine which theme/plugin is responsible for a given asset being added to the page. Apparently Query Monitor's source attribution logic is confusing the AMP plugin's source attribute logic, since they probably do similar things. Since it doesn't make sense for Query Monitor to be running during the loopback request for AMP validation, to me it seems the logical course of action is to just prevent Query Monitor from running during such requests.So, after the changes in this PR, Query Monitor's functionality works properly on the frontend on AMP pages:
WIth bf5f446, Query Monitor is also successfully integrated with the legacy AMP post templates: