Skip to Content


Update critical resources in the service worker JavaScript file for caching.

PHP February 19, 2018


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

Additional Notes

This function looks for variables set between //BEGIN Automated edits and //END Automated edits in the service worker JavaScript file (default: sw.js)

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 250.

    3 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.

    Need a new filter hook? Request one here.

    Need a new action hook? Request one here.

            public function update_sw_js($version=false){
                $this->timer('Update SW');
                $override = apply_filters('pre_nebula_update_swjs', null);
                if ( isset($override) ){return;}
                if ( empty($version) ){
                    $version = apply_filters('nebula_sw_cache_version', $version);
                global $wp_filesystem;
                $sw_js = $wp_filesystem->get_contents($this->sw_location(false));
                if ( !empty($sw_js) ){
                    $find = array(
                        "/(const THEME_NAME = ')(.+)(';)/m",
                        "/(const NEBULA_VERSION = ')(.+)(';)(.+$)?/m",
                        "/(const OFFLINE_URL = ')(.+)(';)/m",
                        "/(const OFFLINE_IMG = ')(.+)(';)/m",
                        "/(const OFFLINE_GA_DIMENSION = ')(.+)(';)/m",
                        "/(const META_ICON = ')(.+)(';)/m",
                        "/(const MANIFEST = ')(.+)(';)/m",
                        "/(const HOME_URL = ')(.+)(';)/m",
                        "/(const START_URL = ')(.+)(';)/m",
                    //$new_cache_name = "nebula-" . strtolower(get_option('stylesheet')) . "-" . mt_rand(10000, 99999); //PHP 7.4 use numeric separators here
                    $replace = array(
                        "$1" . strtolower(get_option('stylesheet')) . "$3",
                        "$1" . 'v' . $version . "$3 //" . date('l, F j, Y g:i:s A'),
                        "$1" . home_url('/') . "offline/$3",
                        "$1" . get_theme_file_uri('/assets/img') . "/offline.svg$3",
                        "$1cd" . $this->ga_definition_index($this->get_option('cd_offline')) . "$3",
                        "$1" . get_theme_file_uri('/assets/img/meta') . "/android-chrome-512x512.png$3",
                        "$1" . $this->manifest_json_location() . "$3",
                        "$1" . home_url('/') . "$3",
                        "$1" . home_url('/') . "?utm_source=pwa$3", //If getting "start_url does not respond" when offline in Lighthouse, make sure you are not disabling the cache in DevTools Network tab!
                    $sw_js = preg_replace($find, $replace, $sw_js);
                    $update_sw_js = $wp_filesystem->put_contents($this->sw_location(false), $sw_js);
                $this->timer('Update SW', 'end');
                return false;


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

    add_filter('pre_nebula_update_swjs', 'my_custom_update_sw_js'); 
    function my_custom_update_sw_js(){ 
        //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:

     add_filter('pre_nebula_update_swjs', '__return_false');