You are here

function pwa_admin_configuration_sw_patterns in Progressive Web App 7.2

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

File

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

Code

function pwa_admin_configuration_sw_patterns() {
  $form = [];
  $form['intro'] = [
    '#markup' => '<p>' . t('Configure caching strategies for pages and assets, this configuration will be used by the workbox library.') . '</p>',
  ];
  $form['pwa_sw_cache_exclude'] = [
    '#type' => 'textarea',
    '#title' => t('Do not cache'),
    '#description' => t('URLs paths matching one of these patterns will never be cached by the Service Worker. One JavaScript regex per line.'),
    '#default_value' => variable_get('pwa_sw_cache_exclude', PWA_SW_CACHE_EXCLUDE),
  ];
  $form['asset'] = [
    '#type' => 'fieldset',
    '#title' => t('Assets configuration'),
    '#tree' => TRUE,
    '#description' => t('Define the strategy to use while serving the asset type, a maximum size above which requests will not be cached on the client side, and if responses from third party domains will be cached (useful if there is a CDN)'),
  ];
  $merged = array_replace_recursive(PWA_SW_ASSETS, variable_get('pwa_sw_asset_config', []));
  foreach ($merged as $type => $default) {
    $form['asset'][$type] = [
      '#type' => 'fieldset',
      '#title' => $type,
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ];
    $form['asset'][$type]['strategy'] = [
      '#type' => 'select',
      '#title' => t('Strategy'),
      '#options' => [
        'DoNotCache' => t('Do not cache'),
        'CacheFirst' => t('Cache first'),
        'NetworkFirst' => t('Network first'),
        'StaleWhileRevalidate' => t('Stale while revalidate'),
        'NetworkOnly' => t('Network only'),
        'CacheOnly' => t('Cache only'),
      ],
      '#default_value' => $default['strategy'],
    ];
    $form['asset'][$type]['external'] = [
      '#type' => 'checkbox',
      '#title' => t('Cache third party'),
      '#default_value' => $default['external'],
      '#prefix' => '&nbsp;&nbsp;&nbsp;',
      '#states' => [
        'invisible' => [
          'select[name="asset[' . $type . '][strategy]"]' => [
            'value' => 'DoNotCache',
          ],
        ],
      ],
    ];
    $form['asset'][$type]['limitMaxSize'] = [
      '#type' => 'checkbox',
      '#title' => t('Limit maximum size'),
      '#default_value' => $default['limitMaxSize'],
      '#prefix' => '&nbsp;&nbsp;&nbsp;',
      '#states' => [
        'invisible' => [
          'select[name="asset[' . $type . '][strategy]"]' => [
            'value' => 'DoNotCache',
          ],
        ],
      ],
    ];

    // Maximum size in KB of the responses to put in the cache.
    // The default value is taken from the httparchive: https://httparchive.org/reports/state-of-images#bytesImg
    $form['asset'][$type]['maxSize'] = [
      '#type' => 'textfield',
      '#title' => t('Maxium size'),
      '#default_value' => $default['maxSize'],
      '#size' => 4,
      '#field_suffix' => 'kB',
      '#attributes' => [
        'placeholder' => PWA_SW_ASSETS[$type]['maxSize'],
      ],
      '#prefix' => '&nbsp;&nbsp;&nbsp;',
      '#states' => [
        'invisible' => [
          [
            'input[name="asset[' . $type . '][limitMaxSize]"]' => [
              'checked' => FALSE,
            ],
          ],
          [
            'select[name="asset[' . $type . '][strategy]"]' => [
              'value' => 'DoNotCache',
            ],
          ],
        ],
      ],
    ];
  }
  $form['pwa_sw_patterns_page'] = [
    '#type' => 'textarea',
    '#title' => t('Caching strategy for pages'),
    '#description' => t('The format is Strategy|Pattern, for example <code>NetworkFirst|/node/.*</code>. <br>Available strategies: CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate.'),
    '#default_value' => variable_get('pwa_sw_patterns_page', ''),
  ];
  $form = system_settings_form($form);

  // Transform the values for pwa_sw_assets variable.
  array_unshift($form['#submit'], 'pwa_admin_configuration_sw_patterns_submit');

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