Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rabol's avatar
Level 14

Cache-Control header

Hi

I'm trying to set the cache-control headers on some of my routes like this:

Route::middleware('cache.headers:public;max_age=2628000;etag')->group(function () {
    Route::get('/',[\App\Http\Controllers\LandingPageController::class,'index'])->name('root');
    // Static pages
    Route::get('about',[\App\Http\Controllers\StaticPagesController::class,'about'])->name('about');
    Route::get('sustainability',[\App\Http\Controllers\StaticPagesController::class,'sustainability'])->name('sustainability');
    Route::get('take-a-tour',[\App\Http\Controllers\StaticPagesController::class,'takeatour'])->name('take-a-tour');
});

When I then look at the response header via debug bar I see this:

"cache-control" => array:1 [▼
    0 => "max-age=0, must-revalidate, no-cache, no-store, private"
  ]

The middleware configuration is from the Laravel documentation.

Did I miss something ?

Thanks

0 likes
7 replies
martinbean's avatar

@rabol The cache.headers middleware will add the directives to your HTTP response headers. So view them in your browser’s dev tools.

rabol's avatar
Level 14

@martinbean - please read my message :)

The response header have max-age=0, private... despite that I set a max-age and public for the route

I did view them in the browser's dev-tool

martinbean's avatar

@rabol Right? So that’s why I asked you to check in your browser’s dev tools. To inspect what was actually getting returned in the headers.

If your values are getting overridden then the answer is as simple as that really: you have some code somewhere overriding cache headers.

Also, check that your browser doesn’t have “Disable cache” enabled, otherwise it might be rewriting response headers in order to ensure it’s not cached 🙃

STEREOH's avatar

Tried it with a fresh app and it works perfectly for me.

rabol's avatar
Level 14

@martinbean Found the reason, but not the solution

it is a JetStream, Livewire based app, and I have created a new navigation based on the one that is default for logged in users. I have just removed the parts that required an authenticated user and added a few menu items. As soon as I have that 'component' on the page, the cache headers are not set, now I just need to figure out why :-)

martinbean's avatar

@rabol I imagine Livewire. Livewire works by updating the DOM database sing AJAX requests. It’ll be to ensure it’s AJAX updates are fresh and not cached.

usernotnull's avatar

@rabol it's due to a livewire update which added 'back_button_cache' => false in your livewire config. If you change it to true it will not override your max-age. Otherwise, any page that has any livewire component will have the max-age set to 0 regardless of your setting. I just noticed it and trying to see what to make of it because most of my pages have a livewire component...

Please or to participate in this conversation.