From 739e113cea8e8a52bb1909c016baa5118105fd1d Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sat, 22 Jun 2024 23:47:59 -0400 Subject: [PATCH] feat: Added a **Set No-Cache Headers** setting (defaulting to on) to set `no-cache` headers on the redirect response to prevent client-side caching ([#306](https://github.com/nystudio107/craft-retour/issues/306)) --- src/config.php | 6 ++++++ src/models/Settings.php | 5 +++++ src/services/Redirects.php | 4 ++++ src/templates/settings/_includes/general.twig | 12 +++++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/config.php b/src/config.php index e049134c..95571c8e 100644 --- a/src/config.php +++ b/src/config.php @@ -36,6 +36,12 @@ // Should the query string be stripped from all 404 URLs before their evaluation? 'alwaysStripQueryString' => false, + // Should the query string be preserved and passed along to the redirected URL? + 'preserveQueryString' => false, + + // Should `no-cache` headers be set on the redirect response to prevent client-side caching? + 'setNoCacheHeaders' => true, + // Should the query string be stripped from the saved statistics source URLs? 'stripQueryStringFromStats' => true, diff --git a/src/models/Settings.php b/src/models/Settings.php index 2cb7956c..39f80e76 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -55,6 +55,11 @@ class Settings extends Model */ public bool $preserveQueryString = false; + /** + * @var bool Should `no-cache` headers be set on the redirect response to prevent client-side caching? + */ + public bool $setNoCacheHeaders = true; + /** * @var bool Should the anonymous ip address of the client causing a 404 be * recorded? diff --git a/src/services/Redirects.php b/src/services/Redirects.php index d7c4e77c..7dc8ae6a 100644 --- a/src/services/Redirects.php +++ b/src/services/Redirects.php @@ -878,6 +878,10 @@ public function doRedirect(string $fullUrl, string $pathOnly, ?array $redirect): } // Sanitize the URL $dest = UrlHelper::sanitizeUrl($dest); + // Optionally set the no-cache headers + if (Retour::$settings->setNoCacheHeaders) { + $response->setNoCacheHeaders(); + } // Add any additional headers (existing ones will be replaced) if (!empty(Retour::$settings->additionalHeaders)) { foreach (Retour::$settings->additionalHeaders as $additionalHeader) { diff --git a/src/templates/settings/_includes/general.twig b/src/templates/settings/_includes/general.twig index 13379cb4..f1771101 100644 --- a/src/templates/settings/_includes/general.twig +++ b/src/templates/settings/_includes/general.twig @@ -70,6 +70,16 @@ warning: configWarning("preserveQueryString", "retour"), errors: settings.getErrors("preserveQueryString"), }) }} - + + {{ forms.lightswitchField({ + label: "Set No-Cache Headers"|t("retour"), + instructions: "Should `no-cache` headers be set on the redirect response to prevent client-side caching?"|t("retour"), + id: "setNoCacheHeaders", + name: "setNoCacheHeaders", + on: settings.setNoCacheHeaders, + warning: configWarning("setNoCacheHeaders", "retour"), + errors: settings.getErrors("setNoCacheHeaders"), + }) }} + {% endnamespace %}