You are here

function hook_pwa_cache_urls_alter in Progressive Web App 8

Same name and namespace in other branches
  1. 2.x pwa.api.php \hook_pwa_cache_urls_alter()

Alters cached urls list for the service worker file.

This hook allows adding or removing urls to the whitelist for the cache urls. This is the same as the "URLs to cache on install" list on the service worker admin page.

After you make your modifications you do NOT need to return the results. Since $cacheUrls is passed by reference, any changes made to $cacheUrls are automatically registered.

Parameters

array &$cacheUrls: List of urls to cache.

\Drupal\Core\Cache\CacheableMetadata $cacheableMetadata: Cacheability metadata.

1 invocation of hook_pwa_cache_urls_alter()
PWAController::pwa_serviceworker_file_data in src/Controller/PWAController.php
Replace the serviceworker file with variables from Drupal config.

File

./pwa.api.php, line 31
Hooks provided by the Progressive Web App module.

Code

function hook_pwa_cache_urls_alter(&$cacheUrls, CacheableMetadata &$cacheableMetadata) {

  // Get a node URL and its cacheability metadata.
  $generated_url = \Drupal\node\Entity\Node::load('1')
    ->toUrl()
    ->toString(TRUE);

  // Add the URL to the list.
  $cacheUrls[] = $generated_url
    ->getGeneratedUrl();

  // Merge the cacheability metadata.
  $cacheableMetadata = $cacheableMetadata
    ->merge($generated_url);
}