### Install laravel-page-cache via Composer Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/README.md Install the package via Composer. ```bash composer require jeffersongoncalves/laravel-page-cache ``` -------------------------------- ### Test theme-aware cache keys with cookie Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Pest test confirming that the cache key includes the theme cookie: light and dark themes get separate MISS entries, and repeating light returns HIT. ```php it('stores a separate entry per theme cookie', function () { $this->withUnencryptedCookie('theme', 'light')->get('/cached')->assertHeader('X-Page-Cache', 'MISS'); $this->withUnencryptedCookie('theme', 'dark')->get('/cached')->assertHeader('X-Page-Cache', 'MISS'); $this->withUnencryptedCookie('theme', 'light')->get('/cached')->assertHeader('X-Page-Cache', 'HIT'); }); ``` -------------------------------- ### Test caching a 200 GET response with X-Page-Cache header Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Pest test verifying that a 200 GET response is cached: first request returns MISS, second returns HIT, and the content is identical. Uses the array cache store in tests. ```php use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; use JeffersonGoncalves\PageCache\Middleware\CachePublicPage; it('caches a 200 GET response', function () { Route::middleware(CachePublicPage::class)->get('/cached', fn () => (string) Str::uuid()); $first = $this->get('/cached')->assertHeader('X-Page-Cache', 'MISS'); $second = $this->get('/cached')->assertHeader('X-Page-Cache', 'HIT'); expect($second->getContent())->toBe($first->getContent()); }); ``` -------------------------------- ### Register CachePublicPage middleware Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/README.md Register CachePublicPage after StartSession and authentication middleware inside the web group. First request stores with X-Page-Cache: MISS; subsequent requests serve with X-Page-Cache: HIT. Only stateless GET requests returning 200 from guests are cached. ```php use Illuminate\Support\Facades\Route; use JeffersonGoncalves\PageCache\Middleware\CachePublicPage; Route::middleware(['web', CachePublicPage::class])->group(function () { Route::get('/', HomeController::class); Route::get('/{slug}', ShowController::class); }); ``` -------------------------------- ### Register CachePublicPage middleware on route group Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Registers CachePublicPage as the outermost middleware on a public route group. Only stateless GET requests from guests are cached; 200 responses are stored with X-Page-Cache: MISS, cache-served responses carry X-Page-Cache: HIT, and when page-cache.enabled is false the middleware is a no-op. ```php use Illuminate\Support\Facades\Route; use JeffersonGoncalves\PageCache\Middleware\CachePublicPage; Route::middleware(CachePublicPage::class)->group(function () { Route::get('/', HomeController::class); }); ``` -------------------------------- ### Run tests, coverage, static analysis, and formatting Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Commands for running the test suite, coverage, static analysis, and code formatting for the package. ```bash # Run all tests vendor/bin/pest # Run with coverage vendor/bin/pest --coverage # Static analysis vendor/bin/phpstan analyse # Code formatting vendor/bin/pint ``` -------------------------------- ### Publish laravel-page-cache config Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/README.md Publish the package configuration file. ```bash php artisan vendor:publish --tag="laravel-page-cache-config" ``` -------------------------------- ### Run tests with composer test Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/README.md Runs the test suite for the package using Composer. Use this command to execute all tests defined in the project. ```bash composer test ``` -------------------------------- ### Package structure for laravel-page-cache Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Shows the package's directory layout: the service provider, the CachePublicPage middleware, and the config file. ```text src/ PageCacheServiceProvider.php # Registers the package + config file Middleware/ CachePublicPage.php # Full-page response cache middleware config/ page-cache.php # enabled, ttl, key (locale + theme cookie) ``` -------------------------------- ### Default page-cache.php configuration Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Default configuration for the package: enabled flag, TTL in seconds, and key segments for locale and theme. The theme segment resolves to 'dark' unless the cookie equals 'light'. ```php return [ 'enabled' => env('PAGE_CACHE_ENABLED', true), 'ttl' => (int) env('PAGE_CACHE_TTL', 3600), 'key' => [ 'locale' => true, 'theme' => [ 'enabled' => true, 'cookie' => 'theme', ], ], ]; ``` -------------------------------- ### laravel-page-cache config file Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/README.md Published config file. Defaults: enabled true, TTL 3600 (0 or below means cache forever), include_query_string true, locale true, accept_encoding true, theme enabled with cookie 'theme'. ```php return [ 'enabled' => env('PAGE_CACHE_ENABLED', true), // A TTL of 0 (or below) means "cache forever". 'ttl' => (int) env('PAGE_CACHE_TTL', 3600), // Fold a normalized query string into the cache key (safe default). 'include_query_string' => env('PAGE_CACHE_INCLUDE_QUERY_STRING', true), 'key' => [ 'locale' => true, // Vary the cache on the negotiated content encoding. 'accept_encoding' => true, 'theme' => [ 'enabled' => true, 'cookie' => 'theme', ], ], ]; ``` -------------------------------- ### Test authenticated requests are not cached Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Pest test confirming that authenticated requests are not cached: the X-Page-Cache header is absent when a user is authenticated. ```php use Illuminate\Foundation\Auth\User; it('does not cache authenticated requests', function () { $this->actingAs(new User); $this->get('/cached')->assertHeaderMissing('X-Page-Cache'); }); ``` -------------------------------- ### Test cache invalidation via flush() Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Pest test verifying that CachePublicPage::flush() invalidates cached pages: after a MISS then HIT, flushing causes the next request to be MISS again. ```php it('invalidates cached pages on flush', function () { $this->get('/cached')->assertHeader('X-Page-Cache', 'MISS'); $this->get('/cached')->assertHeader('X-Page-Cache', 'HIT'); CachePublicPage::flush(); $this->get('/cached')->assertHeader('X-Page-Cache', 'MISS'); }); ``` -------------------------------- ### Cache key format for laravel-page-cache Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/resources/boost/skills/page-cache-development/SKILL.md Cache key format: segments joined with colons. The page and version tokens are always present; locale and theme segments are optional based on config. The path (not full URL) is hashed so query strings cannot flood the cache. ```text page : {version} : {locale?} : {theme?} : sha1(path) ``` -------------------------------- ### Flush page cache from model observer Source: https://github.com/jeffersongoncalves/laravel-page-cache/blob/master/README.md Invalidate all cached pages from model observers by calling CachePublicPage::flush(). flush() bumps an internal version token, bypassing every previously cached page on the next request. ```php use JeffersonGoncalves\PageCache\Middleware\CachePublicPage; class ProjectObserver { public function saved(Project $project): void { CachePublicPage::flush(); } public function deleted(Project $project): void { CachePublicPage::flush(); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.