Skip to Content
Menu

sw_location()

Get the path or URL of the service worker JavaScript file.

PHP February 19, 2018

Usage

PHP
nebula()->sw_location($uri)

Parameters

$uri
(Optional) (Boolean) Return the full URL of the Service Worker file
Default: true

Parameter Notes

Passing false to $uri will return the path.

Request or provide clarification »

Additional Notes

The default filename for the Nebula service worker is sw.js and typical Nebula installation instructs for this file to be moved to the root directory. However, all of this can be overridden by using the pre_sw_location action (See below for full code snippet). If you intend on using an alternate filename or location, you must override this function in your child theme!

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/Functions.php on line 251.

    1 Hook

    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_sw_location"
    Need a new filter hook? Request one here.

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

    PHP
            public function sw_location($uri=true){
                $override = apply_filters('pre_sw_location', null, $uri);
                if ( isset($override) ){return $override;}
    
                if ( !empty($uri) ){
                    return get_site_url() . '/sw.js';
                }
    
                return get_home_path() . 'sw.js';
            }
    

    Override

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

    PHP
    add_filter('pre_sw_location', 'my_custom_sw_location', 10, 2); //The last integer must be 1 more than the actual parameters
    function my_custom_sw_location($null, $uri){ //$null is required, but can be ignored
        //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_sw_location', '__return_false');