function _pwa_serviceworker_file in Progressive Web App 7.2
Same name and namespace in other branches
- 7 pwa.module \_pwa_serviceworker_file()
Take the serviceworker template file and replace all the variables needed.
Return value
string
File
- ./
pwa.module, line 313
Code
function _pwa_serviceworker_file() {
$path = drupal_get_path('module', 'pwa');
$sw = file_get_contents($path . '/js/serviceworker/_template.js');
$precache_page = _pwa_config_value_split('pwa_sw_precache_page', '');
$precache_asset = _pwa_config_value_split('pwa_sw_precache_asset', '');
$offline_url = url('/offline');
$offline_image = file_create_url($path . '/assets/offline-image.png');
// Look up module release from package info.
$pwa_module_version = pwa_version_assets('pwa');
// Set up placeholders.
$replace = [];
// Data for the SW scripts
$drupalPWASettings = [
'version' => $pwa_module_version . '-v' . variable_get('pwa_sw_cache_version', 1),
'debug' => (bool) variable_get('pwa_sw_debug', FALSE),
'cache' => [
// Never include these URLs in the SW cache.
'exclude' => array_merge(_pwa_config_value_split('pwa_sw_cache_exclude', PWA_SW_CACHE_EXCLUDE), [
'^/pwa/.*',
]),
// Precached URLs. Add URLs using the 'Service Worker' tab of the Drupal UI.
'precache' => [
'page' => array_merge($precache_page, [
variable_get('pwa_start_url', '/?source=pwa'),
$offline_url,
]),
// Cached assets. These are extracted using internal HTTP requests during Drupal
// cache clears and this list will be hardcoded in the resultant SW file.
'asset' => array_merge($precache_asset, [
$offline_image,
]),
],
// Strategies for caching specific patterns.
'patterns' => [
'page' => _pwa_config_value_explode('pwa_sw_patterns_page', ''),
// Not used currently.
'asset' => _pwa_config_value_explode('pwa_sw_patterns_asset', ''),
],
'assets' => variable_get('pwa_sw_asset_config', PWA_SW_ASSETS),
'offline' => [
// When no connection is available, show this URL instead of the content that
// should be available at the URL. This URL is never shown in the browser.
'page' => $offline_url,
// When an image hasn't been cached, we use this fallback image instead.
'image' => $offline_image,
],
],
];
// @todo introduce a plugin system to deal with additional features to the serviceworker.
if (variable_get('pwa_sw_phonehome', TRUE)) {
$drupalPWASettings['phonehome'] = [
// Idle time in seconds afterwhich we should check if the user has access
// to the serviceworker.
'idle' => 10 * 60,
// Number of page requests to wait until we check user access to the
// serviceworker.
'count' => 10,
];
}
drupal_alter('pwa_serviceworker_data', $drupalPWASettings);
// Pretty print the json to make it easier to see what's in the SW.
$replace['{/*drupalPWASettings*/}'] = json_encode($drupalPWASettings, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
// List of scripts to import to build the serviceworker.
$workbox_url = variable_get('pwa_workbox_url');
$scripts = [
'utils' => $path . '/js/serviceworker/utils.js',
//'lifecycle' => $path . '/js/serviceworker/lifecycle.js',
// @todo introduce a plugin system to deal with additional features to the serviceworker.
'phonehome' => variable_get('pwa_sw_phonehome', TRUE) ? $path . '/js/serviceworker/phonehome.js' : NULL,
'workbox' => !empty($workbox_url) ? $workbox_url : PWA_WORKBOX_URL,
'cache' => $path . '/js/serviceworker/cache.js',
];
drupal_alter('pwa_serviceworker_script', $scripts);
$importScripts = [];
foreach (array_filter($scripts) as $script) {
$importScripts[] = "importScripts('" . url($script, [
'query' => [
'v' => $pwa_module_version,
],
'absolute' => TRUE,
]) . "');";
}
$replace['/*importScripts*/'] = implode("\n", $importScripts);
// Fill placeholders and return final file.
return str_replace(array_keys($replace), array_values($replace), $sw);
}