Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/StaticCaching/Cachers/AbstractCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function getBaseUrl()
*/
public function getDefaultExpiration()
{
return $this->config('default_cache_length');
return $this->config('expiry')
?? $this->config('default_cache_length'); // deprecated
}

/**
Expand Down
43 changes: 30 additions & 13 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\StaticCaching\Middleware;

use Closure;
use Statamic\Statamic;
use Statamic\StaticCaching\Cacher;

class Cache
Expand All @@ -26,34 +27,50 @@ public function __construct(Cacher $cacher)
*/
public function handle($request, Closure $next)
{
return $next($request);
if ($this->canBeCached($request) && ($cached = $this->cacher->getCachedPage($request))) {
return response($cached);
}

$response = $next($request);

if ($this->shouldBeCached($request, $response)) {
$this->cacher->cachePage($request, $response);
}

return $response;
}

/**
* Perform any final actions for the request lifecycle.
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public function terminate($request, $response)
private function canBeCached($request)
{
if ($request->method() !== 'GET') {
return false;
}

if (Statamic::isCpRoute()) {
return false;
}

return true;
}

private function shouldBeCached($request, $response)
{
// Only GET requests should be cached. For instance, Live Preview hits frontend URLs as
// POST requests to preview the changes. We don't want those to trigger any caching,
// or else pending changes will be shown immediately, even without hitting save.
if ($request->method() !== 'GET') {
return;
return false;
}

// Draft pages should not be cached.
if ($response->headers->has('X-Statamic-Draft')) {
return;
return false;
}

if ($response->getStatusCode() !== 200 || $response->getContent() == '') {
return;
return false;
}

$this->cacher->cachePage($request, $response);
return true;
}
}
49 changes: 0 additions & 49 deletions src/StaticCaching/Middleware/Retrieve.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/StaticCaching/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Statamic\StaticCaching\Middleware\Retrieve;

class ServiceProvider extends LaravelServiceProvider
{
Expand Down Expand Up @@ -34,8 +33,6 @@ public function register()

public function boot()
{
$this->app['router']->prependMiddlewareToGroup('web', Retrieve::class);

Event::subscribe(Invalidate::class);
}
}
21 changes: 21 additions & 0 deletions tests/StaticCaching/CacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function gets_config_values()

/** @test */
public function gets_default_expiration()
{
$cacher = $this->cacher([
'expiry' => 10,
]);

$this->assertEquals(10, $cacher->getDefaultExpiration());
}

/** @test */
public function gets_default_expiration_using_deprecated_key()
{
$cacher = $this->cacher([
'default_cache_length' => 10,
Expand All @@ -31,6 +41,17 @@ public function gets_default_expiration()
$this->assertEquals(10, $cacher->getDefaultExpiration());
}

/** @test */
public function gets_default_expiration_where_new_key_takes_precedence_over_deprecated_key()
{
$cacher = $this->cacher([
'expiry' => 2,
'default_cache_length' => 10,
]);

$this->assertEquals(2, $cacher->getDefaultExpiration());
}

/** @test */
public function gets_a_url()
{
Expand Down