38 lines
1.4 KiB
PHP
38 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Livewire\Features\SupportDisablingBackButtonCache\SupportDisablingBackButtonCache;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Guest pages carry shared public cache headers so a CDN can absorb the
|
|
* bulk of page views; authenticated views (which may embed private rows)
|
|
* must never be cached publicly. Data staleness is bounded by the TTL —
|
|
* benchmark results a couple of minutes old are correct behavior.
|
|
*
|
|
* Livewire's DisableBackButtonCacheMiddleware wraps ours and overwrites
|
|
* Cache-Control on the way out whenever its static flag is set by a
|
|
* component render. We clear that flag after the response is built so
|
|
* our CDN headers survive; browser bfcache loss is acceptable here.
|
|
*/
|
|
class CachePublicGuestPages
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$response = $next($request);
|
|
|
|
SupportDisablingBackButtonCache::$disableBackButtonCache = false;
|
|
|
|
if (auth()->check() || ! $request->isMethodSafe()) {
|
|
$response->headers->set('Cache-Control', 'private, no-store');
|
|
} else {
|
|
$ttl = (int) config('festbench.home_cache_ttl', 300);
|
|
$response->headers->set('Cache-Control', "public, max-age={$ttl}, s-maxage={$ttl}, stale-while-revalidate=60");
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|