Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Adds async/defer capability to JavaScript loading #39

Closed
wants to merge 2 commits into from

Conversation

mor10
Copy link
Contributor

@mor10 mor10 commented Oct 16, 2018

  • Adds async/defer capability to JavaScript loading.
  • Defers loading of twentynineteen-skip-link-focus-fix to improve performance.

async/defer is new best practice for JS loading and should be used for all enqueued JS on the front end in place of loading in footer. This feature allows control of JS loading by adding the following code to any enqueued script:

wp_script_add_data( '[label]', '[async/defer]', true );

Corresponds with https://core.trac.wordpress.org/ticket/12009 and gracefully falls back if/when this feature is added to core.

Code by @westonruter, originally contributed to WP Rig

functions.php Outdated
@@ -117,7 +117,8 @@ function twentynineteen_content_width() {
function twentynineteen_scripts() {
wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri() );

wp_enqueue_script( 'twentynineteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );
wp_enqueue_script( 'twentynineteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', false );
wp_script_add_data( 'twentynineteen-skip-link-focus-fix', 'defer', true );
Copy link
Member

Choose a reason for hiding this comment

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

This will be unnecessary (for skip-link-focus-fix) with this change applied: Automattic/_s#1323

See also note on wprig/wprig#139 for why defer is not ideal for skip-link-focus-fix either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I totally forgot. The functionality still stands though (I expect some JS will be added to this theme at some point.)

Copy link
Member

Choose a reason for hiding this comment

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

PR to apply the fix to the blocking script: #47

@mor10
Copy link
Contributor Author

mor10 commented Oct 16, 2018

I've updated the PR to not defer twentynineteen-skip-link-focus-fix as it is handled in #47. Functionality remains for future JS enqueueing additions.

@mor10
Copy link
Contributor Author

mor10 commented Nov 20, 2018

Closed in favor of #646

@mor10 mor10 closed this Nov 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants