public function PWAController::pwa_serviceworker_file_data in Progressive Web App 8
Same name and namespace in other branches
- 2.x src/Controller/PWAController.php \Drupal\pwa\Controller\PWAController::pwa_serviceworker_file_data()
Replace the serviceworker file with variables from Drupal config.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object.
Return value
mixed
1 string reference to 'PWAController::pwa_serviceworker_file_data'
File
- src/
Controller/ PWAController.php, line 172 - Replace values in serviceworker.js
Class
- PWAController
- Default controller for the pwa module.
Namespace
Drupal\pwa\ControllerCode
public function pwa_serviceworker_file_data(Request $request) {
$path = drupal_get_path('module', 'pwa');
$sw = file_get_contents($path . '/js/serviceworker.js');
// Get module configuration.
$config = \Drupal::config('pwa.config');
// Get URLs from config.
$cacheUrls = pwa_str_to_list($config
->get('urls_to_cache'));
$cacheUrls[] = $config
->get('offline_page');
$exclude_cache_url = pwa_str_to_list($config
->get('urls_to_exclude'));
// Initialize a CacheableMetadata object.
$cacheable_metadata = new CacheableMetadata();
$cacheable_metadata
->addCacheableDependency($config);
$cacheable_metadata
->setCacheMaxAge(86400)
->setCacheContexts([
'url',
]);
// Get icons list and convert into array of sources.
$manifest = Json::decode($this->manifest
->getOutput());
$cacheIcons = [];
if (!empty($manifest['icons'])) {
foreach ($manifest['icons'] as $icon) {
$cacheIcons[] = $icon['src'];
}
}
// Combine URLs from admin UI with manifest icons.
$cacheWhitelist = array_merge($cacheUrls, $cacheIcons);
// Allow other modules to alter the URL's. Also pass the CacheableMetadata
// object so these modules can add cacheability metadata to the response.
$this->moduleHandler
->alter('pwa_cache_urls', $cacheWhitelist, $cacheable_metadata);
$this->moduleHandler
->alter('pwa_exclude_urls', $exclude_cache_url, $cacheable_metadata);
// Active languages on the site.
$languages = \Drupal::languageManager()
->getLanguages();
// Get the skip-waiting setting.
$skip_waiting = $config
->get('skip_waiting') ? 'true' : 'false';
// Set up placeholders.
$replace = [
'[/*cacheUrls*/]' => Json::encode($cacheWhitelist),
'[/*activeLanguages*/]' => Json::encode(array_keys($languages)),
'[/*exclude_cache_url*/]' => Json::encode($exclude_cache_url),
"'/offline'/*offlinePage*/" => "'" . $config
->get('offline_page') . "'",
'[/*modulePath*/]' => '/' . drupal_get_path('module', 'pwa'),
'1/*cacheVersion*/' => '\'' . $this
->pwa_get_cache_version() . '\'',
'false/*pwaSkipWaiting*/' => $skip_waiting,
];
if (!empty($cacheUrls)) {
$replace['[/*cacheUrlsAssets*/]'] = Json::encode($this
->_pwa_fetch_offline_page_resources($cacheUrls));
}
$this->moduleHandler
->alter('pwa_replace_placeholders', $replace);
// Fill placeholders and return final file.
$data = str_replace(array_keys($replace), array_values($replace), $sw);
$response = new CacheableResponse($data, 200, [
'Content-Type' => 'application/javascript',
'Service-Worker-Allowed' => '/',
]);
$response
->addCacheableDependency($cacheable_metadata);
return $response;
}