Make WordPress Core

Changeset 37920


Ignore:
Timestamp:
06/29/2016 07:35:27 PM (9 years ago)
Author:
ocean90
Message:

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.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/admin-filters.php

    r37619 r37920  
    4343add_action( 'admin_head', 'wp_site_icon'             );
    4444add_action( 'admin_head', '_ipad_meta'               );
     45
     46// Prerendering.
     47add_filter( 'admin_head', 'wp_resource_hints' );
    4548
    4649add_action( 'admin_print_scripts-post.php',     'wp_page_reload_on_back_button_js' );
  • trunk/src/wp-includes/default-filters.php

    r37915 r37920  
    240240add_action( 'wp_head',             'wp_generator'                           );
    241241add_action( 'wp_head',             'rel_canonical'                          );
     242add_action( 'wp_head',             'wp_resource_hints'                      );
    242243add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
    243244add_action( 'wp_head',             'wp_site_icon',                    99    );
  • trunk/src/wp-includes/general-template.php

    r37866 r37920  
    27892789
    27902790/**
     2791 * Prints resource hints to browsers for pre-fetching, pre-rendering and pre-connecting to web sites.
     2792 *
     2793 * Gives hints to browsers to prefetch specific pages or render them in the background,
     2794 * to perform DNS lookups or to begin the connection handshake (DNS, TCP, TLS) in the background.
     2795 *
     2796 * These performance improving indicators work by using `<link rel"…">`.
     2797 *
     2798 * @since 4.6.0
     2799 */
     2800function wp_resource_hints() {
     2801    $hints = array(
     2802        'dns-prefetch' => wp_resource_hints_scripts_styles(),
     2803        'preconnect'   => array( 's.w.org' ),
     2804        'prefetch'     => array(),
     2805        'prerender'    => array(),
     2806    );
     2807
     2808    foreach ( $hints as $relation_type => $urls ) {
     2809        /**
     2810         * Filters domains and URLs for resource hints.
     2811         *
     2812         * @since 4.6.0
     2813         *
     2814         * @param array  $urls          URLs to print for resource hints.
     2815         * @param string $relation_type The relation type the URLs are printed for, e.g. 'preconnect' or 'prerender'.
     2816         */
     2817        $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
     2818        $urls = array_unique( $urls );
     2819
     2820        foreach ( $urls as $url ) {
     2821            $url = esc_url( $url, array( 'http', 'https' ) );
     2822
     2823            if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) {
     2824                $parsed = parse_url( $url );
     2825
     2826                if ( ! empty( $parsed['scheme'] ) ) {
     2827                    $url = $parsed['scheme'] . '://' . $parsed['host'];
     2828                } else {
     2829                    $url = $parsed['host'];
     2830                }
     2831            }
     2832
     2833            printf( "<link rel='%s' href='%s'>\r\n", $relation_type, $url );
     2834        }
     2835    }
     2836}
     2837
     2838/**
     2839 * Adds dns-prefetch for all scripts and styles enqueued from external hosts.
     2840 *
     2841 * @since 4.6.0
     2842 */
     2843function wp_resource_hints_scripts_styles() {
     2844    global $wp_scripts, $wp_styles;
     2845
     2846    $unique_hosts = array();
     2847
     2848    if ( is_object( $wp_scripts ) && ! empty( $wp_scripts->registered ) ) {
     2849        foreach ( $wp_scripts->registered as $registered_script ) {
     2850            $src = $registered_script->src;
     2851            // Make sure the URL has a scheme, otherwise parse_url() could fail to pass the host.
     2852            if ( '//' == substr( $src, 0, 2 ) ) {
     2853                $src = set_url_scheme( $src );
     2854            }
     2855
     2856            $this_host = parse_url( $src, PHP_URL_HOST );
     2857            if ( ! empty( $this_host ) && ! in_array( $this_host, $unique_hosts ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2858                $unique_hosts[] = $this_host;
     2859            }
     2860        }
     2861    }
     2862
     2863    if ( is_object( $wp_styles ) && ! empty( $wp_styles->registered ) ) {
     2864        foreach ( $wp_styles->registered as $registered_style ) {
     2865            $src = $registered_style->src;
     2866            // Make sure the URL has a scheme, otherwise parse_url() could fail to pass the host.
     2867            if ( '//' == substr( $src, 0, 2 ) ) {
     2868                $src = set_url_scheme( $src );
     2869            }
     2870
     2871            $this_host = parse_url( $src, PHP_URL_HOST );
     2872            if ( ! empty( $this_host ) && ! in_array( $this_host, $unique_hosts ) && $this_host !== $_SERVER['SERVER_NAME'] ) {
     2873                $unique_hosts[] = $this_host;
     2874            }
     2875        }
     2876    }
     2877
     2878    return $unique_hosts;
     2879}
     2880
     2881/**
    27912882 * Whether the user should have a WYSIWIG editor.
    27922883 *
Note: See TracChangeset for help on using the changeset viewer.