What is wp-embed.min.js, and how can it lead to faster page loads?

Recently, in an aim to optimise page loading speeds, I’ve been paying a lot of attention to javascript files that are loaded by WordPress and plugins. I spotted that wp-embed.min.js is a file that WordPress will load by default into all pages. After a quick look (https://codex.wordpress.org/Embeds), I found that it is the plugin that is responsible for converting links into embed frames, e.g. you can paste a Youtube video link straight from the address bar into your wysiwyg content editor in your WordPress backend and the Youtube embed will be generated for you when your page is viewed, cool.

If you don’t need this functionality you can disable it with the following code in your functions.php file.

function dequeue_scripts() {
    wp_deregister_script( 'wp-embed' );
add_action( 'wp_enqueue_scripts', 'dequeue_scripts' );

Alternatively, if you’d prefer to keep the functionality of wp-embed but don’t want the additional request for it during page load, then we can concatenate it with our theme’s scripts when we run our build task. I use Gulp to prepare and build my theme assets, so in this case I add wp-embed.min.js to my json array of files to be concatenated (I needed to do some file traversal from the theme folder and ended up adding this to my list “../../../../wp-includes/js/wp-embed.min.js”). If you use a different task runner for concatenating js then you should be able to achieve the same results.

Big deal, I just saved 1 request. Well, kind of, by applying this process to plugins (e.g. we can cut down 2 js file requests from Contact Form 7) then it is possible to end up with a single js file, resulting in significantly less requests, resulting in faster load times. It’s also worth considering applying the same process to plugin’s css files too, this could help reduce requests further. One thing to bear in mind though, is that as newer versions of WordPress and plugins are released it’s important ensure that updates don’t break things, and to rerun build tasks when necessary.