diff --git a/src/StaticCaching/Cachers/AbstractCacher.php b/src/StaticCaching/Cachers/AbstractCacher.php index cc1731a18b5..d29c38f9516 100644 --- a/src/StaticCaching/Cachers/AbstractCacher.php +++ b/src/StaticCaching/Cachers/AbstractCacher.php @@ -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 } /** diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index d791675dc86..35fd4df8929 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -3,6 +3,7 @@ namespace Statamic\StaticCaching\Middleware; use Closure; +use Statamic\Statamic; use Statamic\StaticCaching\Cacher; class Cache @@ -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; } } diff --git a/src/StaticCaching/Middleware/Retrieve.php b/src/StaticCaching/Middleware/Retrieve.php deleted file mode 100644 index 70ac2caae66..00000000000 --- a/src/StaticCaching/Middleware/Retrieve.php +++ /dev/null @@ -1,49 +0,0 @@ -cacher = $cacher; - } - - /** - * Handle an incoming request. - * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed - */ - public function handle($request, Closure $next) - { - if ($this->canBeCached($request) && ($cached = $this->cacher->getCachedPage($request))) { - return response($cached); - } - - return $next($request); - } - - protected function canBeCached($request) - { - if ($request->method() !== 'GET') { - return false; - } - - if (Statamic::isCpRoute()) { - return false; - } - - return true; - } -} diff --git a/src/StaticCaching/ServiceProvider.php b/src/StaticCaching/ServiceProvider.php index 71ff4224429..70fb55b093e 100644 --- a/src/StaticCaching/ServiceProvider.php +++ b/src/StaticCaching/ServiceProvider.php @@ -4,7 +4,6 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; -use Statamic\StaticCaching\Middleware\Retrieve; class ServiceProvider extends LaravelServiceProvider { @@ -34,8 +33,6 @@ public function register() public function boot() { - $this->app['router']->prependMiddlewareToGroup('web', Retrieve::class); - Event::subscribe(Invalidate::class); } } diff --git a/tests/StaticCaching/CacherTest.php b/tests/StaticCaching/CacherTest.php index 2e12ee67167..02aa0954263 100644 --- a/tests/StaticCaching/CacherTest.php +++ b/tests/StaticCaching/CacherTest.php @@ -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, @@ -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() {