From ebde61e72a0df37ac19cee78d690c6458f4545ef Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 30 Nov 2023 13:01:54 +1300 Subject: [PATCH] NEW Configuration property for disallowed status codes (#178) --- docs/en/basic_configuration.md | 12 ++++++++++++ src/Publisher/FilesystemPublisher.php | 16 ++++++++++++++++ tests/php/Publisher/FilesystemPublisherTest.php | 11 +++++++++++ 3 files changed, 39 insertions(+) diff --git a/docs/en/basic_configuration.md b/docs/en/basic_configuration.md index 0bf0005f..f11b8ed7 100644 --- a/docs/en/basic_configuration.md +++ b/docs/en/basic_configuration.md @@ -48,6 +48,18 @@ class MyFormPage extends Page } ``` +## Excluding response codes + +Be default, static files are generated for all page responses regardless of the response's status code. +If you would like to exclude certain status codes from being statically cached, set the `FilesystemPublisher.disallowed_status_codes` configuration property: + +```yaml +SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher: + disallowed_status_codes: + - 404 + - 500 +``` + ## Control when child/parent pages are regenerated in cache actions There are two configurations available, and they can both be set to one of three available values: diff --git a/src/Publisher/FilesystemPublisher.php b/src/Publisher/FilesystemPublisher.php index ca9084fd..63e0c49a 100644 --- a/src/Publisher/FilesystemPublisher.php +++ b/src/Publisher/FilesystemPublisher.php @@ -12,6 +12,11 @@ class FilesystemPublisher extends Publisher { + /** + * Status codes that aren't allowed to be statically cached. + */ + private static array $disallowed_status_codes = []; + /** * @var string */ @@ -108,6 +113,17 @@ public function publishURL($url, $forcePublish = false) $statusCode = $response->getStatusCode(); $doPublish = ($forcePublish && $this->getFileExtension() === 'php') || $statusCode < 400; + // Don't statically cache if the status code is in a deny list + if (in_array($statusCode, static::config()->get('disallowed_status_codes'))) { + return [ + 'published' => false, + // Considering this a "success" since the behaviour is as expected + 'success' => true, + 'responsecode' => $statusCode, + 'url' => $url, + ]; + } + if ($statusCode >= 300 && $statusCode < 400) { // publish redirect response $success = $this->publishRedirect($response, $url); diff --git a/tests/php/Publisher/FilesystemPublisherTest.php b/tests/php/Publisher/FilesystemPublisherTest.php index 486b3b16..73a9df27 100644 --- a/tests/php/Publisher/FilesystemPublisherTest.php +++ b/tests/php/Publisher/FilesystemPublisherTest.php @@ -246,6 +246,17 @@ public function testPurgeURLAfterSwitchingExtensions(): void $this->assertFileDoesNotExist($this->fsp->getDestPath() . 'purge-me.php'); } + public function testNoPublishOnDisallowedResponseCode(): void + { + $this->logOut(); + + FilesystemPublisher::config()->set('disallowed_status_codes', [404]); + + $this->fsp->publishURL('not_really_there', true); + $this->assertFileDoesNotExist($this->fsp->getDestPath() . 'not_really_there.html'); + $this->assertFileDoesNotExist($this->fsp->getDestPath() . 'not_really_there.php'); + } + public function testNoErrorPagesWhenHTMLOnly(): void { $this->logOut();