Skip to content

Commit

Permalink
feat: Added a **Set No-Cache Headers** setting (defaulting to on) to …
Browse files Browse the repository at this point in the history
…set `no-cache` headers on the redirect response to prevent client-side caching ([#306](#306))
  • Loading branch information
khalwat committed Jun 23, 2024
1 parent b136324 commit 739e113
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
5 changes: 5 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions src/services/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion src/templates/settings/_includes/general.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
</div>

0 comments on commit 739e113

Please sign in to comment.