Skip to content

Commit

Permalink
Add support for preconnect hints, improve documentation and UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed May 14, 2018
1 parent 33fdd14 commit eb4250a
Show file tree
Hide file tree
Showing 24 changed files with 383 additions and 132 deletions.
84 changes: 76 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,100 @@
# Better Resource Hints
Easy preloading, prefetching, HTTP/2 server pushing for your CSS and JavaScript.
Easy preloading, prefetching, HTTP/2 server pushing, and more for your CSS and JavaScript.

## Description
Better Resource Hints will make your WordPress site faster using modern, performance-enhancing resource hints supported by most of today's browsers and servers.
Better Resource Hints will make your WordPress site or application more faster and generally more performant by intelligently leveraging resource hints like prefetch, preload, preconnect, and server push.

As it stands, WordPress isn't that bad about leveraging a base level of resource hints. A basic API has been [shipped since version 4.6.](https://make.wordpress.org/core/2016/07/06/resource-hints-in-4-6/). However, this functionality only scratches the service, providing only `dns-prefetch` tags out of the box. Better Resource Hints helps you leverage some of the most effective, cutting-edge techniques available to most browsers today, and more importantly, it allows you to implement these tactics in an unintimidating yet flexible way. Specifically, this plugin focuses on the following types of hints for your styles and JavaScript assets:
As it stands, WordPress isn't that bad about providing a base level of these hints. In fact, a basic, dedicated API has been [shipped since version 4.6.](https://make.wordpress.org/core/2016/07/06/resource-hints-in-4-6/). However, this functionality only scratches the service, providing only `dns-prefetch` tags out of the box, and there's growing opportunity to take advantage of different hints as they are introduced and gain more browser support. Specifically, this plugin focuses on the following types of hints for your styles and JavaScript assets:

**Preloading** - This occurs when the browser is told it can start downloading an asset in the background early during page load, instead of waiting until the asset is explicitly called to start the process. This hint is most beneficial for assets loaded later on in the page, but are nonetheless essential to the page's functionality. More often than not, this is a JavaScript file. Enabling this results in an overall faster load time, and quicker time to interactive.
**Preconnecting** - This hint is similar to "dns-prefetch," but a more beefier version. Instead of just resolving the DNS, the preconnect hint handles TLS negotiations and TCP handshakes, resulting in reduced page latency.

**Preloading** - Preloadin occurs when the browser is told it can start downloading an asset in the background early during page load, instead of waiting until the asset is explicitly called to start the process. This hint is most beneficial for assets loaded later on in the page, but are nonetheless essential to the page's functionality. More often than not, this is a JavaScript file. Enabling this results in an overall faster load time, and quicker time to interactive.

**Prefetching** - Prefetching assets is similar to preloading, but the assets are downloaded in low priority for the purpose of caching them for later use. For example, if a user hits your home page and is likely to go to a page that uses a heavy JavaScript file, it's wise to prefetch that asset on the home page, so it's cached and ready to go on the next. Again, the result is a quicker subsequent page load, quicker time to interactive, and an improved overall user experience. This is different from DNS prefetching, which will only resolve the DNS of a resource's host, and not actually download the resource itself.

**Server Push** - If enabled, server push will tell your server to start delivering an asset before the browser even asks for it. This results in a much faster delivery of key assets, and be toggled on for both preloaded and prefetched assets. **Note: This feature requires a server that supports server push, and is the most experimental strategy this plugin provides.**
**Server Push** - If enabled, server push will tell your server to start delivering an asset before the browser even asks for it. This results in a much faster delivery of key assets, and be toggled on for both preloaded, prefetched, and preconnected assets. **Note: This feature requires a server that supports server push, and is the most experimental strategy this plugin provides.**

As with any sort of performance-enhancing technique, just be aware that they should be used judiciously, and that the results you see will depend on the amount of resource your site loads, as well as how your server is configured. For additional reading, see some of the resources below:
As with any sort of performance-enhancing technique, just be aware that they should be used judiciously, and that the results you see will depend on the size the of resources your site loads, as well as how your server is configured. For additional reading, see some of the resources below:

[Preload, Prefetch, & Priorities in Chrome](https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf)
[Preloading Key Requests](https://developers.google.com/web/tools/lighthouse/audits/preload)
[Preload: What's It Good For?](https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/)
[Resource Hints – What is Preload, Prefetch, and Preconnect?](https://www.keycdn.com/blog/resource-hints/)

### What Makes This Plugin Stand Apart?
There's no shortage of plugins out there that aim to leverage resource hints for boosting performance. However, I've seen that several of them make the following mistakes:

**Inflexible Hint Management** Many similar plugins only provide very limited flexibility in their options, and only allow setting hints globally for every page, regardless of whether the resources are actually needed on the page. This can often result in unecessarily bloaging your bandwidth, since hints on several pages are effectively useless. In some cases, this could actually lead to a less performant site. This plugin attempts to provide options to manage hints more flexibily and intelligently, meaning you won't be unecessarily preloading assets in the background when they're not even needed on the page.

**Misunderstanding What Different Hints Do** I've come across some plugins that fail to understand and leverage different hints like they were designed. For example, promising that assets are being preloaded, when they're actually being prefetched. These and other hints have very different purposes, and should not be interchangably used if you want them to impact your site in the most effective way. This plugin attemps to leverage these hints in way to maximize their effectiveness. For example, BHR won't prefetch any assets that are enqueued on the page, because that's just now how the prefetch hint is designed to be used.

Is Better Resource Hints perfect? Absolutely not. That's why I encourage any constructive feedback or bug reports to be sent my way immediately, so that I can't improve this plugin as quickly as possible.

#### About Preloading CSS
### A Note About Preloading CSS

Because of their high placement on a page, if the option is enabled, your CSS files will be asyncronously preloaded, and _then_ turned into a stylesheet once they've completely loaded. The advantage to doing this is that while the files are downloading, they won't block the rest of the page from rendering, resulting an overall faster page load.

However, this also means that there may be a flash of unstyled content on the page for a brief moment as the files download. To prevent this, it's recommended to only preload CSS files that are not critical to the initial view of the page. This will allow you to gain some performance points without sacrificing use experience as the page loads.

## Filters

The following filters are exposed for your use.

```php
/**
* Modify the HTML link generated for preconnecting hosts.
*
* @param string $link (HTML tag)
* @param string $url (URL of the host)
* @return string
*/
add_filter('better_resource_hints_preconnect_tag', function ($link, $url) {
return $url;
}, 10, 2);
```

```php
/**
* Modify the HTML link generated for prefetching hosts.
*
* @param string $link (HTML tag)
* @param string $handle (WP handle of the resource
* @param string $type (script or style)
* @return string
*/
add_filter('better_resource_hints_prefetch_tag', function ($link, $handle, $type) {
return $url;
}, 10, 3);
```

```php
/**
* Modify the HTML link generated for preloading hosts.
*
* @param string $link (HTML tag)
* @param string $handle (WP handle of the resource
* @param string $type (script or style)
* @return string
*/
add_filter('better_resource_hints_preload_tag', function ($link, $handle, $type) {
return $url;
}, 10, 3);
```

## Installation
1. Download the plugin and upload to your plugins directory, or install the plugin through the WordPress plugins page.
2. Activate the plugin through the 'Plugins' page.
3. Use the Settings -> Better Resource Hints screen to choose whether and which assets to preload, prefetch, and server push.

## What Happens Out of the Box?
Upon activation, Better Resource Hints will optimize your resource hints in a conservative, low-risk way by only doing two things out of the box:

1. Preloading JavaScript assets enqueued in the footer.
2. Setting preconnect hints for all third party hosts that already have dns-preconnect hints.

## Using the Plugin

Upon activation, Better Resource Hints will optimize your resource hints in a consevrative, low-risk way by only preloading JavaScript assets enqueued in the footer. Beyond this, you are able to adjust settings to tweak optimization as seen fit. As a means of testing your optimizations, use a tool like [Google Lighthouse](https://developers.google.com/web/tools/lighthouse/) to measure the impact of these changes on your site's performance.
After activation, you are able to adjust settings to tweak optimization as seen fit. As a means of testing your optimizations, use a tool like [Google Lighthouse](https://developers.google.com/web/tools/lighthouse/) to measure the impact of these changes on your site's performance.

As mentioned, the techniques used here are largely supported by modern browsers, but your results may vary depending on the amount of assets being loaded on your site, as well as your server configuration.

Expand All @@ -40,6 +103,11 @@ As mentioned, the techniques used here are largely supported by modern browsers,
#### 1.0.0
* Initial release.

#### 1.1.0
* Introduce support for generating preconnect hints.
* Improve user interface.
* Improve documentation.

## Feedback
You like it? [Email](mailto:alex@macarthur.me) or [tweet](http://www.twitter.com/amacarthur) me. You hate it? [Email](mailto:alex@macarthur.me) or [tweet](http://www.twitter.com/amacarthur) me.

Expand Down
6 changes: 4 additions & 2 deletions better-resource-hints.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Plugin Name: Better Resource Hints
* Description: Easy preloading, prefetching, and HTTP/2 server pushing for your CSS and JavaScript.
* Version: 1.0.0
* Description: Easy preloading, prefetching, HTTP/2 server pushing, and more for your CSS and JavaScript.
* Version: 1.1.0
* Author: Alex MacArthur
* Author URI: http://macarthur.me
* License: GPLv2 or later
Expand All @@ -13,6 +13,7 @@

require_once 'src/Settings.php';
require_once 'src/Filters.php';
require_once 'src/Preconnector.php';
require_once 'src/Preloader.php';
require_once 'src/Prefetcher.php';
require_once 'src/Utilities.php';
Expand Down Expand Up @@ -42,6 +43,7 @@ public function __construct() {
new Settings;
new Preloader;
new Prefetcher;
new Preconnector;

add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles_and_scripts' ));
}
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "better-resource-hints",
"description": "No-nonsense preloading, prefetching, and HTTP/2 server pushing for your CSS and JavaScript assets.",
"description": "Easy preloading, prefetching, HTTP/2 server pushing, and more for your CSS and JavaScript.",
"type": "wordpress-plugin",
"minimum-stability": "stable",
"license": "GPL-2.0",
"license": "GPL-2.0",
"version": "1.1.0",
"authors": [
{
"name": "Alex MacArthur",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"author": "Alex MacArthur <alex@macarthur.me>",
"email": "alex@macarthur.me",
"url": "https://macarthur.me",
"description": "Easy preloading, prefetching, and HTTP/2 server pushing for your CSS and JavaScript assets.",
"version": "1.0.0",
"description": "Easy preloading, prefetching, HTTP/2 server pushing, and more for your CSS and JavaScript.",
"version": "1.1.0",
"license": "GPL-2.0",
"main": "better-resource-hints.php",
"repository": "https://github.com/alexmacarthur/wp-better-resource-hints",
Expand Down
Binary file modified plugin-assets/banner-1544x500.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plugin-assets/banner-772x250.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plugin-assets/icon-128x128.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plugin-assets/icon-256x256.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plugin-assets/screenshot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plugin-assets/screenshot-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plugin-assets/screenshot-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit eb4250a

Please sign in to comment.