You are here

function _pwa_fetch_offline_page_resources in Progressive Web App 7

Discover CSS/JS assets present in offline URLs so they can be cached during the Service Worker install event.

Parameters

$pages:

Return value

array

1 call to _pwa_fetch_offline_page_resources()
_pwa_serviceworker_file in ./pwa.module
Take the serviceworker template file and replace all the variables needed.

File

./pwa.module, line 239

Code

function _pwa_fetch_offline_page_resources($pages) {
  $resources = [];

  // For each Drupal path, request the HTML response and parse any CSS/JS found
  // within the HTML. Since this is the pure HTML response, any DOM modifications
  // that trigger new requests cannot be accounted for. An example would be an
  // asynchronously-loaded webfont.
  foreach ($pages as $page) {
    $response = drupal_http_request(url($page, [
      'absolute' => TRUE,
    ]));
    $dom = new DOMDocument();
    @$dom
      ->loadHTML($response->data);
    $xpath = new DOMXPath($dom);
    foreach ($xpath
      ->query('//script[@src]') as $script) {
      $resources[] = $script
        ->getAttribute('src');
    }
    foreach ($xpath
      ->query('//link[@rel="stylesheet"][@href]') as $stylesheet) {
      $resources[] = $stylesheet
        ->getAttribute('href');
    }
    foreach ($xpath
      ->query('//img[@src]') as $image) {
      $resources[] = $image
        ->getAttribute('src');
    }
  }
  $dedupe = array_unique($resources);
  return $dedupe;
}