Same name and namespace in other branches
  1. 6.0.x src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::generateHtaccess() 1 comment
  2. 8.x-3.x src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::generateHtaccess() 1 comment
  3. 8.x-4.x src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::generateHtaccess() 1 comment

Generate an htaccess file in the optimized asset dirs to improve serving.

Parameters

string $extension: The file type to use - either CSS or JS.

bool $regenerate: Whether to regenerate if the file already exists.

3 calls to AssetOptimizer::generateHtaccess()
advagg_cron in ./advagg.module
Implements hook_cron().
advagg_update_8301 in ./advagg.install
Implements hook_update_N().
SettingsForm::submitForm in src/Form/SettingsForm.php

File

src/Asset/AssetOptimizer.php, line 478

Class

AssetOptimizer
Defines the base AdvAgg optimizer.

Namespace

Drupal\advagg\Asset

Code

public static function generateHtaccess($extension, $regenerate = FALSE) {
    $fileSystem = \Drupal::service('file_system');
    $path = "public://{$extension}/optimized";
    $file = $path . '/.htaccess';
    if (!$regenerate && file_exists($file)) {
        return;
    }
    
    /** @var \Drupal\Core\Config\Config $config */
    $config = \Drupal::config('advagg.settings');
    if ($extension === 'js') {
        $type = 'application/javascript';
    }
    else {
        $type = 'text/css';
    }
    $fileSystem->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
    $immutable = $config->get('immutable') ? ', immutable' : '';
    $options = '';
    if ($config->get('symlinks')) {
        $options = 'Options +FollowSymlinks';
    }
    elseif ($config->get('symlinksifownermatch')) {
        $options = 'Options +SymLinksIfOwnerMatch';
    }
    $htaccess = <<<EOT
      {<span class="php-variable">$options</span>}
      <IfModule mod_rewrite.c>
        RewriteEngine on
        <IfModule mod_headers.c>
          # Serve brotli compressed {<span class="php-variable">$extension</span>} files if they exist and the client accepts br.
          RewriteCond %{HTTP:Accept-encoding} br
          RewriteCond %{REQUEST_FILENAME}\\.br -s
          RewriteRule ^(.*)\\.{<span class="php-variable">$extension</span>} \$1\\.{<span class="php-variable">$extension</span>}\\.br [QSA]
          RewriteRule \\.{<span class="php-variable">$extension</span>}\\.br\$ - [T={<span class="php-variable">$type</span>},E=no-gzip:1]

          <FilesMatch "\\.{<span class="php-variable">$extension</span>}\\.br\$">
            # Serve correct encoding type.
            Header set Content-Encoding br
            # Force proxies to cache br/gzip/non-gzipped assets separately.
            Header append Vary Accept-Encoding
          </FilesMatch>

          # Serve gzip compressed {<span class="php-variable">$extension</span>} files if they exist and the client accepts gzip.
          RewriteCond %{HTTP:Accept-encoding} gzip
          RewriteCond %{REQUEST_FILENAME}\\.gz -s
          RewriteRule ^(.*)\\.{<span class="php-variable">$extension</span>} \$1\\.{<span class="php-variable">$extension</span>}\\.gz [QSA]
          RewriteRule \\.{<span class="php-variable">$extension</span>}\\.gz\$ - [T=application/javascript,E=no-gzip:1]

          <FilesMatch "\\.{<span class="php-variable">$extension</span>}\\.gz\$">
            # Serve correct encoding type.
            Header set Content-Encoding gzip
            # Force proxies to cache br/gzip/non-gzipped assets separately.
            Header append Vary Accept-Encoding
          </FilesMatch>
        </IfModule>
      </IfModule>

      <FilesMatch "{<span class="php-variable">$extension</span>}(\\.gz|\\.br)?">
        # No mod_headers. Apache module headers is not enabled.
        <IfModule !mod_headers.c>
          # No mod_expires. Apache module expires is not enabled.
          <IfModule !mod_expires.c>
            # Use ETags.
            FileETag MTime Size
          </IfModule>
        </IfModule>

        # Use Expires Directive if apache module expires is enabled.
        <IfModule mod_expires.c>
          # Do not use ETags.
          FileETag None
          # Enable expirations.
          ExpiresActive On
          # Cache all aggregated {<span class="php-variable">$extension</span>} files for 52 weeks after access (A).
          ExpiresDefault A31449600
        </IfModule>

        # Use Headers Directive if apache module headers is enabled.
        <IfModule mod_headers.c>
          # Do not use etags for cache validation.
          Header unset ETag
          # Serve correct content type.
          Header set Content-Type {<span class="php-variable">$type</span>}
          <IfModule !mod_expires.c>
            # Set a far future Cache-Control header to 52 weeks.
            Header set Cache-Control "max-age=31449600, no-transform, public{<span class="php-variable">$immutable</span>}"
          </IfModule>
          <IfModule mod_expires.c>
            Header append Cache-Control "no-transform, public{<span class="php-variable">$immutable</span>}"
          </IfModule>
        </IfModule>
        ForceType {<span class="php-variable">$type</span>}
      </FilesMatch>
EOT;
    $fileSystem->saveData($htaccess, $file, FileSystemInterface::EXISTS_REPLACE);
}