You are here

public function PWAController::_pwa_fetch_offline_page_resources in Progressive Web App 8

Same name and namespace in other branches
  1. 2.x src/Controller/PWAController.php \Drupal\pwa\Controller\PWAController::_pwa_fetch_offline_page_resources()

Fetch all resources.

Parameters

array $pages: The page URL.

Return value

array Returns an array of the CSS and JS file URLs.

1 call to PWAController::_pwa_fetch_offline_page_resources()
PWAController::pwa_serviceworker_file_data in src/Controller/PWAController.php
Replace the serviceworker file with variables from Drupal config.

File

src/Controller/PWAController.php, line 101
Replace values in serviceworker.js

Class

PWAController
Default controller for the pwa module.

Namespace

Drupal\pwa\Controller

Code

public function _pwa_fetch_offline_page_resources($pages) {

  // 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.
  $resources = [];
  foreach ($pages as $page) {
    try {

      // URL is validated as internal in ConfigurationForm.php.
      $url = Url::fromUserInput($page, [
        'absolute' => TRUE,
      ])
        ->toString(TRUE);
      $url_string = $url
        ->getGeneratedUrl();
      $response = \Drupal::httpClient()
        ->get($url_string, array(
        'headers' => array(
          'Accept' => 'text/plain',
        ),
      ));
      $data = $response
        ->getBody();
      if (empty($data)) {
        continue;
      }
    } catch (\Exception $e) {
      continue;
    }
    $page_resources = [];

    // Get all DOM data.
    $dom = new \DOMDocument();
    @$dom
      ->loadHTML($data);
    $xpath = new \DOMXPath($dom);
    foreach ($xpath
      ->query('//script[@src]') as $script) {
      $page_resources[] = $script
        ->getAttribute('src');
    }
    foreach ($xpath
      ->query('//link[@rel="stylesheet"][@href]') as $stylesheet) {
      $page_resources[] = $stylesheet
        ->getAttribute('href');
    }
    foreach ($xpath
      ->query('//style[@media="all" or @media="screen"]') as $stylesheets) {
      preg_match_all("#(/(\\S*?\\.\\S*?))(\\s|\\;|\\)|\\]|\\[|\\{|\\}|,|\"|'|:|\\<|\$|\\.\\s)#ie", ' ' . $stylesheets->textContent, $matches);
      $page_resources = array_merge($page_resources, $matches[0]);
    }
    foreach ($xpath
      ->query('//img[@src]') as $image) {
      $page_resources[] = $image
        ->getAttribute('src');
    }

    // Allow other modules to alter cached asset URLs for this page.
    $this->moduleHandler
      ->alter('pwa_cache_urls_assets_page', $page_resources, $page, $xpath);
    $resources = array_merge($resources, $page_resources);
  }
  $dedupe = array_unique($resources);
  $dedupe = array_values($dedupe);

  // Allow other modules to alter the final list of cached asset URLs.
  $this->moduleHandler
    ->alter('pwa_cache_urls_assets', $dedupe);
  return $dedupe;
}