Skip to content

Fix broken "Re-enable transient caching" button for "Transient caching of parsed stylesheets is disabled" site health test #6552

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

Merged
merged 8 commits into from
Aug 25, 2021

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Aug 21, 2021

Summary

Revert to using a timeout to set the button functionality as originally done by @schlessera (see 5cbe86b). This gives the Site Health screen some time to add all the direct tests so that we can then add the necessary callback to the button.

This PR also ensures the button works when using the Site Health plugin.

Fixes #6549

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@pierlon pierlon added this to the v2.1.4 milestone Aug 21, 2021
@pierlon pierlon requested a review from westonruter August 21, 2021 04:10
@github-actions
Copy link
Contributor

github-actions bot commented Aug 21, 2021

Plugin builds for 1edd22a are ready 🛎️!

@@ -84,7 +85,7 @@ public function register_ajax_script( $hook_suffix ) {
} );
} );
} );
} );
}, 1000 );
Copy link
Member

Choose a reason for hiding this comment

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

Why 1 second specifically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an estimation to give a bit of leeway for all direct site health tests to be added to the page. One alternative could be to add a setTimeout to keep checking for when this specific site health test gets added to the page, but that seems like a waste of CPU cycles.

Copy link
Member

Choose a reason for hiding this comment

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

But the test is not async so why would it not be on the page initially?

Comment on lines 65 to 67
window.addEventListener( 'DOMContentLoaded', ( event ) => {
var selector = SELECTOR;
var selector = SELECTOR;
setTimeout( function () {
Copy link
Member

Choose a reason for hiding this comment

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

Is the problem rather that the DOMContentLoaded event is being added to the window as opposed to the document? Or what if the load event on the window were used instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is that the DOMContentLoaded may fire before the site health test is added to the page. I guess the button was working before because the site health test was being added just in time before this event listener fires, and now that there are direct tests being added before this test, the event listener now fires before the site health test is added to the page.

Copy link
Member

Choose a reason for hiding this comment

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

Switching this instead to run at the load event of window should do the trick, since it always runs after all DOMContentLoaded event handlers have run.

Copy link
Contributor Author

@pierlon pierlon Aug 23, 2021

Choose a reason for hiding this comment

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

The issue lies between the time the site health test DOM element is added to the page and when our JS executes. Both DOMContentLoaded and load events may have already passed by the time the site health is added to the page unfortunately.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like that code is running in jQuery( function( $ ) {}) which is the same as DOMContentLoaded. It doesn't work when switching to use the load event on window?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm it works in Chrome but not Firefox. The button is still being inserted after the load event handler callback executes.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I was getting inconsistent results as well. I think any timeout is going to be problematic and that a different approach is needed that doesn't rely on the element being in the DOM. Namely, this should have used event delegation from the start. Please try again after c577735.

<script>
(( $, { selector, action, postArgs } ) => {
document.addEventListener( 'DOMContentLoaded', () => {
$( '.health-check-body' ).on( 'click', selector, ( event ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh cool workaround! And TIL you could pass a selector to a jQuery event handler to filter the element that triggered the event.

Copy link
Member

Choose a reason for hiding this comment

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

My stoneage JS knowledge still holds some surprises I guess. 😊

@pierlon
Copy link
Contributor Author

pierlon commented Aug 25, 2021

Everything seems to be good here, giving my 👍.

@pierlon pierlon requested a review from westonruter August 25, 2021 02:10
@westonruter
Copy link
Member

The reason for 1edd22a is that I found that even with the Site Health plugin active, I could access both versions of the page: /wp-admin/site-health.php?tab and /wp-admin/tools.php?page=health-check. Granted, the former was not linked to anymore from the admin menu, but still it could be accessed directly.

@westonruter westonruter merged commit 1450617 into develop Aug 25, 2021
@westonruter westonruter deleted the fix/6549-site-health-broken-button branch August 25, 2021 04:57
westonruter added a commit that referenced this pull request Aug 25, 2021
…g of parsed stylesheets is disabled" site health test (#6552)

Co-authored-by: Pierre Gordon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken “Re-enable transient caching” button for “Transient caching of parsed stylesheets is disabled” site health test
2 participants