Skip to content

Commit

Permalink
Add config to exclude certain response codes (broken unit test)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispenny committed Aug 15, 2023
1 parent 855b70f commit 90a22ff
Show file tree
Hide file tree
Showing 3 changed files with 38 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

Default behaviour is that 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, add them here

```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
15 changes: 15 additions & 0 deletions src/Publisher/FilesystemPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class FilesystemPublisher extends Publisher
{
private static array $disallowed_status_codes = [];

/**
* @var string
*/
Expand Down Expand Up @@ -101,19 +103,32 @@ public function publishURL($url, $forcePublish = false)
{
if (!$url) {
user_error('Bad url:' . var_export($url, true), E_USER_WARNING);

return;
}

$success = false;
$response = $this->generatePageResponse($url);
$statusCode = $response->getStatusCode();
$doPublish = ($forcePublish && $this->getFileExtension() === 'php') || $statusCode < 400;

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);
} elseif ($doPublish) {
$success = $this->publishPage($response, $url);
}

return [
'published' => $doPublish,
'success' => $success,
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 @@ -268,6 +268,17 @@ public function testErrorPageWhenPHP(): void
$this->assertSame(404, $phpCacheConfig['responseCode']);
}

public function testNoPublishOnDisallowedResponseCode(): void
{
$this->logOut();

Config::modify()->set(FilesystemPublisher::class, 'disallowed_status_codes', [404]);

$this->fsp->publishURL('page_not_exists', true);
$this->assertFileDoesNotExist($this->fsp->getDestPath() . 'page_not_exists.html');
$this->assertFileDoesNotExist($this->fsp->getDestPath() . 'page_not_exists.php');
}

public function testRedirectorPageWhenPHP(): void
{
$redirectorPage = RedirectorPage::create();
Expand Down

0 comments on commit 90a22ff

Please sign in to comment.