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

Leverage the new script API strategy feature to defer front end scripts #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adamsilverstein
Copy link

This PR adds the defer attribute to the front end script js/rank-math.js enqueued in includes/frontend/class-frontend.php.

Context

WordPress introduced a new strategy feature in the Script API in version 6.3. See this post for more details. Even though js/rank-math.js is only enqueued when the wp-admin bar is shown, it still seems helpful to leverage defer and switch away from the boolean in_footer setting.

Note that this PR includes a version check and slightly different behavior for 6.3 vs. 6.4 where a potential regression is fixed.

Summary

  • Detects WordPress version, using new approach when available and falling back to existing approach
  • Keeps script in footer for 6.3 while moving to header in 6.4; see https://core.trac.wordpress.org/ticket/59599 for details on why.

Test instructions

  • Verify that the script tag includes the defer attribute before and at WordPress 6.3.
  • With WordPress 6.3 the script tag will print in the footer and also contain data-wp-strategy="defer indicating the API has added the attribute.
  • With WordPress 6.4 the tag will print in the header.

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.

1 participant