Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-9620: Add assets to Varnish cache, new asset purge configuration #210

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ services:
image: druidfi/varnish:6-drupal
environment:
VARNISH_BACKEND_HOST: "${COMPOSE_PROJECT_NAME}-app"
VARNISH_CACHE_STATIC_FILES: "1"
depends_on:
- app
networks:
Expand Down
96 changes: 57 additions & 39 deletions public/sites/default/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,6 @@ function drupal_get_env(string|array $variables) : mixed {
}


if ($varnish_host = getenv('DRUPAL_VARNISH_HOST')) {
$config['varnish_purger.settings.default']['hostname'] = $varnish_host;
$config['varnish_purger.settings.varnish_purge_all']['hostname'] = $varnish_host;

if (!isset($config['system.performance']['cache']['page']['max_age'])) {
$config['system.performance']['cache']['page']['max_age'] = 86400;
}
}

if ($varnish_port = getenv('DRUPAL_VARNISH_PORT')) {
$config['varnish_purger.settings.default']['port'] = $varnish_port;
$config['varnish_purger.settings.varnish_purge_all']['port'] = $varnish_port;
}

if ($navigation_authentication_key = getenv('DRUPAL_NAVIGATION_API_KEY')) {
$config['helfi_navigation.api']['key'] = $navigation_authentication_key;
}
Expand All @@ -182,34 +168,66 @@ function drupal_get_env(string|array $variables) : mixed {
$config['helfi_api_base.environment_resolver.settings']['environment_name'] = getenv('APP_ENV');
$config['helfi_api_base.environment_resolver.settings']['project_name'] = getenv('PROJECT_NAME');

// settings.php doesn't know about existing configuration yet so we can't
// just append new headers to an already existing headers array here.
// If you have configured any extra headers in your purge settings
// you must add them in your all.settings.php as well.
// @todo Replace this with config override service?
$config['varnish_purger.settings.default']['headers'] = [
[
'field' => 'Cache-Tags',
'value' => '[invalidation:expression]',
],
];
if ($varnish_host = getenv('DRUPAL_VARNISH_HOST')) {
// Cache everything for 1 year by default.
$config['system.performance']['cache']['page']['max_age'] = 31536000;

$config['varnish_purger.settings.varnish_purge_all']['headers'] = [
[
'field' => 'X-VC-Purge-Method',
'value' => 'regex',
],
];
$varnish_backend = parse_url($drush_options_uri, PHP_URL_HOST);

if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) {
$config['varnish_purger.settings.default']['headers'][] = [
'field' => 'X-VC-Purge-Key',
'value' => $varnish_purge_key,
];
$config['varnish_purger.settings.varnish_purge_all']['headers'][] = [
'field' => 'X-VC-Purge-Key',
'value' => $varnish_purge_key,
if (getenv('APP_ENV') === 'local') {
// Varnish backend is something like varnish-helfi-kymp.docker.so on
// local env.
$varnish_backend = 'varnish-' . $varnish_backend;
}

// settings.php doesn't know about existing configuration yet so we can't
// just append new headers to an already existing headers array here.
// If you have configured any extra headers in your purge settings
// you must add them in your all.settings.php as well.
// @todo Replace this with config override service?
$varnishConfiguration = [
'default' => [
[
'field' => 'Cache-Tags',
'value' => '[invalidation:expression]',
],
],
'assets' => [
[
'field' => 'X-VC-Purge-Method',
'value' => 'regex',
],
[
'field' => 'Host',
'value' => $varnish_backend,
],
],
'varnish_purge_all' => [
[
'field' => 'X-VC-Purge-Method',
'value' => 'regex',
],
],
];

foreach ($varnishConfiguration as $name => $headers) {
$config['varnish_purger.settings.' . $name]['hostname'] = $varnish_host;

if ($varnish_port = getenv('DRUPAL_VARNISH_PORT')) {
$config['varnish_purger.settings.' . $name]['port'] = $varnish_port;
}

foreach ($headers as $header) {
$config['varnish_purger.settings.' . $name]['headers'][] = $header;
}

if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) {
$config['varnish_purger.settings.' . $name]['headers'][] = [
'field' => 'X-VC-Purge-Key',
'value' => $varnish_purge_key,
];
}
}
}

if ($stage_file_proxy_origin = getenv('STAGE_FILE_PROXY_ORIGIN')) {
Expand Down