Skip to Content
Menu

prebrowsing()

Determine what domains to preconnect, what files to prefetch, and what pages to prerender.

PHP January 5, 2019

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Examples

Prefetch a resource without this function (for reference).

PHP
<link rel="dns-prefetch preconnect" href="//cdnjs.cloudflare.com" />

Preconnect to a domain with this function

PHP
add_filter('nebula_preconnect', 'my_preconnects');
function my_preconnects($array){
    $array[] = '//example.com';
    return $array;
}

Additional Notes

DNS-Prefetch: Resolve the DNS only to a domain.
Preconnect: Resolve both DNS and TCP to a domain.
Prefetch: Fully request a single resource and store it in cache until needed.
Prerender: Render an entire page (useful for comment next page navigation). Use Audience > User Flow report in Google Analytics for better predictions.

This function does not use parameters; it relies exclusively on hooks with add_filter() (see examples).

Note: WordPress does add dns-prefetch to all enqueued resource domains automatically.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Optimization.php on line 430.

    4 Hooks

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_prebrowsing"
    "nebula_preconnect"
    "nebula_prefetches"
    "nebula_preloads"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function prebrowsing(){
                $override = apply_filters('pre_nebula_prebrowsing', null);
                if ( isset($override) ){return;}
    
                $debug_comment = ( $this->is_dev() )? '<!-- Server-side -->' : '';
    
                /*==========================
                 DNS-Prefetch & Preconnect
                 Resolve DNS and TCP to a domain.
                 ===========================*/
    
                $default_preconnects = array();
    
                //Google fonts if used
                if ( $this->get_option('remote_font_url') ){
                    if ( strpos($this->get_option('remote_font_url'), 'google') || strpos($this->get_option('remote_font_url'), 'gstatic') ){
                        $default_preconnects[] = '//fonts.gstatic.com';
                    } elseif ( strpos($this->get_option('remote_font_url'), 'typekit') ){
                        $default_preconnects[] = '//use.typekit.net';
                    }
                }
    
                //Loop through all of the preconnects
                $preconnects = apply_filters('nebula_preconnect', $default_preconnects);
                if ( !empty($preconnects) && is_array($preconnects) ){
                    foreach ( $preconnects as $preconnect ){
                        echo '<link rel="preconnect" href="' . $preconnect . '" crossorigin="anonymous" />' . $debug_comment;
                    }
                }
    
                /*==========================
                 Prefetch
                 Fully request a single resource and store it in cache until needed. Do not combine with preload!
                 ===========================*/
    
                $default_prefetches = array();
    
                //Subpages
                // if ( !is_front_page() ){
                //     //$default_prefetches[] = home_url('/'); //Prefetch the home page on subpages. Disabled because this may be loading more than we want on initial pageload
                // }
    
                //Search Results
                if ( is_search() ){
                    global $wp_query;
                    if ( !empty($wp_query->posts) && !empty($wp_query->posts['0']) ){ //If has search results
                        $default_prefetches[] = get_permalink($wp_query->posts['0']->ID); //Prefetch the first search result
                    }
                }
    
                //404 Pages
                if ( is_404() ){
                    //If Nebula finds a match based on context clues, prefetch that too
                    if ( !empty($this->error_404_exact_match) ){
                        $default_prefetches[] = get_permalink($this->error_404_exact_match->ID);
                    }
    
                    //If has page suggestions prefetch the first one
                    if ( !empty($this->error_query) && $this->error_query->have_posts() ){
                        $default_prefetches[] = get_permalink($this->error_query->posts[0]->ID);
                    }
                }
    
                //Loop through all of the prefetches
                $prefetches = apply_filters('nebula_prefetches', $default_prefetches); //Allow child themes and plugins to prefetch resources via Nebula too
                if ( !empty($prefetches) && is_array($prefetches) ){
                    foreach ( $prefetches as $prefetch ){
                        echo '<link rel="prefetch" href="' . $prefetch . '" crossorigin="anonymous" />' . $debug_comment;
                    }
                }
    
                /*==========================
                 Preload
                 Fully request a single resource before it is needed. Do not combine with prefetch! These must be used within a few seconds of window load.
                 ===========================*/
    
                $default_preloads = array();
    
                //Google fonts if used
    /*
                if ( $this->get_option('remote_font_url') ){
                    $default_preloads[] = $this->get_option('remote_font_url'); //Oct 2019: disabling this for now as it delays render in Chrome
                }
    */
    
                //Loop through all of the preloads
                $preloads = apply_filters('nebula_preloads', $default_preloads); //Allow child themes and plugins to preload resources via Nebula too
                if ( !empty($preloads) && is_array($preloads) ){
                    foreach ( $preloads as $preload ){
                        switch ( $preload ){
                            case strpos($preload, '.css'):
                                $filetype = 'style';
                                break;
                            case strpos($preload, '.js'):
                                $filetype = 'script';
                                break;
                            case strpos($preload, 'fonts.googleapis'):
                            case strpos($preload, '.woff'): //Captures both .woff and .woff2
                                $filetype = 'font';
                                break;
                            case strpos($preload, '.jpg'):
                            case strpos($preload, '.jpeg'):
                            case strpos($preload, '.png'):
                            case strpos($preload, '.gif'):
                                $filetype = 'image';
                                break;
                            case strpos($preload, '.mp4'):
                            case strpos($preload, '.ogv'):
                            case strpos($preload, '.mov'):
                                $filetype = 'video';
                                break;
                            default:
                                $filetype = 'fetch';
                                break;
                        }
    
                        echo '<link rel="preload" href="' . $preload . '" as="' . $filetype . '" crossorigin="anonymous" />';
                    }
                }
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_prebrowsing', 'my_custom_prebrowsing'); 
    function my_custom_prebrowsing(){ 
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_prebrowsing', '__return_false');