Skip to content

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

Open
adamsilverstein wants to merge 1 commit intorankmath:masterfrom
adamsilverstein:add/script-strategy
Open

Leverage the new script API strategy feature to defer front end scripts#253
adamsilverstein wants to merge 1 commit intorankmath:masterfrom
adamsilverstein:add/script-strategy

Conversation

@adamsilverstein
Copy link
Copy Markdown

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