Skip to content

Commit

Permalink
NEW Configuration property for disallowed status codes (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Nov 30, 2023
1 parent 86adc4e commit ebde61e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/en/basic_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions src/Publisher/FilesystemPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/php/Publisher/FilesystemPublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ebde61e

Please sign in to comment.