Skip to Content

update_sw_js()

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

PHP February 19, 2018

Usage

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)

Source File

Located in /libs/Functions.php on line 216.

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.

Filters
"pre_nebula_update_swjs"
"nebula_sw_cache_version"
Need a new filter hook? Request one here.

Actions
"nebula_wrote_sw_js"
Need a new action hook? Request one here.

PHP
    public function update_sw_js(){
        $this->timer('Update SW');

        $override = apply_filters('pre_nebula_update_swjs', null);
        if ( isset($override) ){return;}

        WP_Filesystem();
        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);

            $nebula_version = $this->version('full');
            if ( is_child_theme() ){
                $nebula_version = $this->child_version();
            }

            $replace = array(
                "$1" . strtolower(get_option('stylesheet')) . "$3",
                "$1" . 'v' . apply_filters('nebula_sw_cache_version', $nebula_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!
            );

            $sw_js = preg_replace($find, $replace, $sw_js);
            $update_sw_js = $wp_filesystem->put_contents($this->sw_location(false), $sw_js);
            do_action('nebula_wrote_sw_js');
        }

        $this->timer('Update SW', 'end');
        return false;
    }

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_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:

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