Script Loader: Introduce an API to register resource hints.

Resource hints allow browsers to prefetch specific pages or render them in the background to perform DNS lookups or to begin the connection handshake (DNS, TCP, TLS) in the background.

By default, `wp_resource_hints()` prints hints for "s.w.org" (the WordPress.org CDN) and for all scripts and styles which are enqueued from external hosts.
Use the `wp_resource_hints` filter to add custom domains and URLs for `dns-prefetch`, `preconnect`, `prefetch` or `prerender`.

Props voldemortensen, swissspidy.
Fixes #34292.

git-svn-id: https://develop.svn.wordpress.org/trunk@37920 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2016-06-29 19:35:27 +00:00
parent 328ca0ed12
commit 92dd472359
4 changed files with 247 additions and 0 deletions

View File

@ -43,6 +43,9 @@ add_action( 'admin_head', 'wp_color_scheme_settings' );
add_action( 'admin_head', 'wp_site_icon' );
add_action( 'admin_head', '_ipad_meta' );
// Prerendering.
add_filter( 'admin_head', 'wp_resource_hints' );
add_action( 'admin_print_scripts-post.php', 'wp_page_reload_on_back_button_js' );
add_action( 'admin_print_scripts-post-new.php', 'wp_page_reload_on_back_button_js' );

View File

@ -239,6 +239,7 @@ add_action( 'wp_head', 'wp_print_styles', 8 );
add_action( 'wp_head', 'wp_print_head_scripts', 9 );
add_action( 'wp_head', 'wp_generator' );
add_action( 'wp_head', 'rel_canonical' );
add_action( 'wp_head', 'wp_resource_hints' );
add_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
add_action( 'wp_head', 'wp_site_icon', 99 );
add_action( 'wp_footer', 'wp_print_footer_scripts', 20 );

View File

@ -2787,6 +2787,97 @@ function wp_site_icon() {
}
}
/**
* Prints resource hints to browsers for pre-fetching, pre-rendering and pre-connecting to web sites.
*
* Gives hints to browsers to prefetch specific pages or render them in the background,
* to perform DNS lookups or to begin the connection handshake (DNS, TCP, TLS) in the background.
*
* These performance improving indicators work by using `<link rel"…">`.
*
* @since 4.6.0
*/
function wp_resource_hints() {
$hints = array(
'dns-prefetch' => wp_resource_hints_scripts_styles(),
'preconnect' => array( 's.w.org' ),
'prefetch' => array(),
'prerender' => array(),
);
foreach ( $hints as $relation_type => $urls ) {
/**
* Filters domains and URLs for resource hints.
*
* @since 4.6.0
*
* @param array $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for, e.g. 'preconnect' or 'prerender'.
*/
$urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
$urls = array_unique( $urls );
foreach ( $urls as $url ) {
$url = esc_url( $url, array( 'http', 'https' ) );
if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) {
$parsed = parse_url( $url );
if ( ! empty( $parsed['scheme'] ) ) {
$url = $parsed['scheme'] . '://' . $parsed['host'];
} else {
$url = $parsed['host'];
}
}
printf( "<link rel='%s' href='%s'>\r\n", $relation_type, $url );
}
}
}
/**
* Adds dns-prefetch for all scripts and styles enqueued from external hosts.
*
* @since 4.6.0
*/
function wp_resource_hints_scripts_styles() {
global $wp_scripts, $wp_styles;
$unique_hosts = array();
if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) {
foreach ( $wp_scripts->registered as $registered_script ) {
$src = $registered_script->src;
// Make sure the URL has a scheme, otherwise parse_url() could fail to pass the host.
if ( '//' == substr( $src, 0, 2 ) ) {
$src = set_url_scheme( $src );
}
$this_host = parse_url( $src, PHP_URL_HOST );
if ( ! empty( $this_host ) && ! in_array( $this_host, $unique_hosts ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
$unique_hosts[] = $this_host;
}
}
}
if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) {
foreach ( $wp_styles->registered as $registered_style ) {
$src = $registered_style->src;
// Make sure the URL has a scheme, otherwise parse_url() could fail to pass the host.
if ( '//' == substr( $src, 0, 2 ) ) {
$src = set_url_scheme( $src );
}
$this_host = parse_url( $src, PHP_URL_HOST );
if ( ! empty( $this_host ) && ! in_array( $this_host, $unique_hosts ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
$unique_hosts[] = $this_host;
}
}
}
return $unique_hosts;
}
/**
* Whether the user should have a WYSIWIG editor.
*

View File

@ -0,0 +1,152 @@
<?php
/**
* @group template
* @ticket 34292
*/
class Tests_WP_Resource_Hints extends WP_UnitTestCase {
private $old_wp_scripts;
private $old_wp_styles;
function setUp() {
parent::setUp();
$this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
$this->old_wp_styles = isset( $GLOBALS['wp_styles'] ) ? $GLOBALS['wp_styles'] : null;
remove_action( 'wp_default_scripts', 'wp_default_scripts' );
remove_action( 'wp_default_styles', 'wp_default_styles' );
$GLOBALS['wp_scripts'] = new WP_Scripts();
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
$GLOBALS['wp_styles'] = new WP_Styles();
$GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' );
}
function tearDown() {
$GLOBALS['wp_scripts'] = $this->old_wp_scripts;
$GLOBALS['wp_styles'] = $this->old_wp_styles;
add_action( 'wp_default_scripts', 'wp_default_scripts' );
add_action( 'wp_default_styles', 'wp_default_styles' );
parent::tearDown();
}
function test_should_have_defaults_on_frontend() {
$expected = "<link rel='preconnect' href='http://s.w.org'>\r\n";
$this->expectOutputString( $expected );
wp_resource_hints();
}
function test_dns_prefetching() {
$expected = "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" .
"<link rel='dns-prefetch' href='https://google.com'>\r\n" .
"<link rel='dns-prefetch' href='make.wordpress.org'>\r\n" .
"<link rel='preconnect' href='http://s.w.org'>\r\n";
add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 );
$actual = get_echo( 'wp_resource_hints' );
remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ) );
$this->assertEquals( $expected, $actual );
}
function _add_dns_prefetch_domains( $hints, $method ) {
if ( 'dns-prefetch' === $method ) {
$hints[] = 'http://wordpress.org';
$hints[] = 'https://google.com';
$hints[] = '//make.wordpress.org';
}
return $hints;
}
function test_prerender() {
$expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" .
"<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" .
"<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" .
"<link rel='prerender' href='//core.trac.wordpress.org'>\r\n";
add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 );
$actual = get_echo( 'wp_resource_hints' );
remove_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ) );
$this->assertEquals( $expected, $actual );
}
function _add_prerender_urls( $hints, $method ) {
if ( 'prerender' === $method ) {
$hints[] = 'https://make.wordpress.org/great-again';
$hints[] = 'http://jobs.wordpress.net';
$hints[] = '//core.trac.wordpress.org';
}
return $hints;
}
function test_parse_url_dns_prefetch() {
$expected = "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n" .
"<link rel='preconnect' href='http://s.w.org'>\r\n";
add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 );
$actual = get_echo( 'wp_resource_hints' );
remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ) );
$this->assertEquals( $expected, $actual );
}
function _add_dns_prefetch_long_urls( $hints, $method ) {
if ( 'dns-prefetch' === $method ) {
$hints[] = 'http://make.wordpress.org/wp-includes/css/editor.css';
}
return $hints;
}
/**
* @group foo
*/
function test_dns_prefetch_styles() {
$expected = "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n" .
"<link rel='preconnect' href='http://s.w.org'>\r\n";
$args = array(
'family' => 'Open+Sans:400',
'subset' => 'latin',
);
wp_enqueue_style( 'googlefonts', add_query_arg( $args, '//fonts.googleapis.com/css' ) );
$actual = get_echo( 'wp_resource_hints' );
wp_dequeue_style( 'googlefonts' );
$this->assertEquals( $expected, $actual );
}
function test_dns_prefetch_scripts() {
$expected = "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n" .
"<link rel='preconnect' href='http://s.w.org'>\r\n";
$args = array(
'family' => 'Open+Sans:400',
'subset' => 'latin',
);
wp_enqueue_script( 'googlefonts', add_query_arg( $args, '//fonts.googleapis.com/css' ) );
$actual = get_echo( 'wp_resource_hints' );
wp_dequeue_style( 'googlefonts' );
$this->assertEquals( $expected, $actual );
}
}