You are here

function pwa_admin_configuration_sw in Progressive Web App 7

Same name and namespace in other branches
  1. 7.2 pwa.admin.inc \pwa_admin_configuration_sw()

Configure PWA settings for Service Worker.

1 string reference to 'pwa_admin_configuration_sw'
pwa_menu in ./pwa.module
Implements hook_menu().

File

./pwa.admin.inc, line 113
PWA administration forms.

Code

function pwa_admin_configuration_sw() {
  $form = [];
  $form['sw'] = [
    '#type' => 'fieldset',
    '#title' => t('Service Worker'),
    '#description' => t('Configure behavior of the Service Worker. These are advanced settings that don\'t need to be changed unless you know what you\'re doing.'),
  ];
  $form['sw']['pwa_sw_registration_event'] = [
    '#type' => 'select',
    '#title' => t('When should the Service Worker be registered?'),
    '#options' => [
      'immediate' => t('as soon as possible'),
      'documentready' => t('when DOM is ready'),
      'windowonload' => t('after CSS, JS and images have loaded'),
    ],
    '#description' => t('Registering during page load can have adverse effects on performance. The default is to wait until CSS, JS, and images have finished loading.'),
    '#default_value' => variable_get('pwa_sw_registration_event', PWA_SW_REGISTRATION_EVENT_DEFAULT),
  ];
  $form['sw']['pwa_sw_cache_exclude'] = [
    '#type' => 'textarea',
    '#title' => t('Exclude URLs patterns'),
    '#description' => t('Paths matching these patterns will not be cached by the Service Worker. One JavaScript regex per line.'),
    '#default_value' => variable_get('pwa_sw_cache_exclude', implode("\n", PWA_SW_CACHE_EXCLUDE)),
  ];
  $form['sw']['pwa_sw_cache_urls'] = [
    '#type' => 'textarea',
    '#title' => t('URLs to cache on install'),
    '#description' => t('Cache these URLs when the Service Worker is installed. If a URL is a page all its CSS and JS will be cached automatically.'),
    '#default_value' => variable_get('pwa_sw_cache_urls', implode("\n", [
      '/',
      '/offline',
      variable_get('pwa_start_url', ''),
    ])),
  ];
  $form['sw']['pwa_sw_cache_version'] = [
    '#type' => 'textfield',
    '#title' => t('Cache version'),
    '#description' => t('Changing this number will invalidate all Service Worker caches. Use it when assets have significantly changed or if you want to force a cache refresh for all clients.'),
    '#size' => 5,
    '#default_value' => variable_get('pwa_sw_cache_version', 1),
  ];
  $form = system_settings_form($form);

  // Wait for all the values to be saved before refreshing cache.
  $form['#submit'][] = 'pwa_admin_configuration_submit';
  return $form;
}