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 263.
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_update_swjs""nebula_sw_cache_version"
Need a new filter hook? Request one here.
Actions
"nebula_wrote_sw_js""qm/info"
Need a new action hook? Request one here.
PHP
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); } 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 META_ICON = ')(.+)(';)/m", "/(const MANIFEST = ')(.+)(';)/m", "/(const HOME_URL = ')(.+)(';)/m", "/(const START_URL = ')(.+)(';)/m", ); //$new_cache_name = "nebula-" . strtolower(get_option('stylesheet')) . "-" . random_int(100000, 999999); //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", "$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); do_action('nebula_wrote_sw_js'); do_action('qm/info', 'Updated sw.js File'); } $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');