You are here

function pwa_preprocess_html in Progressive Web App 7.2

Same name and namespace in other branches
  1. 7 pwa.module \pwa_preprocess_html()

Implements hook_preprocess_html().

Add the meta tags necessary for the manifest. Serviceworker script is managed with a block.

File

./pwa.module, line 456

Code

function pwa_preprocess_html(&$variables) {
  if (_pwa_access_check() && variable_get('pwa_sw_everywhere', FALSE)) {

    // Can't use #attached in this preprocess, call library functions directly.
    $block = pwa_block_view('pwa_register');
    $library = $block['content']['#attached']['library'][0];
    $settings = $block['content']['#attached']['js'][0];
    drupal_add_library($library[0], $library[1]);
    drupal_add_js($settings['data'], $settings['type']);
  }
  $manifest = pwa_file_data('manifest');

  // Add manifest.json to HTML
  drupal_add_html_head([
    '#tag' => 'link',
    '#attributes' => [
      'rel' => 'manifest',
      'href' => '/manifest.webmanifest?v=' . variable_get('pwa_sw_cache_version', 1),
      // Make sure that logged in users query the manifest as logged in users
      // @see https://web.dev/add-manifest/#link-manifest
      'crossorigin' => user_is_logged_in() ? 'use-credentials' : '',
    ],
    '#weight' => 10,
  ], 'manifest');

  // Add backup <meta> tag for branding colors. It should always match the
  // variable for the manifest.
  drupal_add_html_head([
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'theme-color',
      'content' => $manifest['theme_color'],
    ],
    '#weight' => 10,
  ], 'theme_color');

  // Add an icon for Apple devices. Necessary for Lighthouse 100%.
  if ($icon_src = array_search('192x192', array_column($manifest['icons'], 'sizes', 'src'))) {
    drupal_add_html_head([
      '#tag' => 'link',
      '#attributes' => [
        'rel' => 'apple-touch-icon',
        'href' => $icon_src,
      ],
      '#weight' => 80,
    ], 'apple-touch-icon');
  }
}