Search code examples
laravelhttp-headerslaravel-livewireback-buttonlaravel-middleware

Laravel Livewire 3 overwrites HTTP response headers


We have this middleware in my app because internal security asked us to overwrite these response headers.

class NoCacheControlHeaders
{
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);

        $response->headers->set('Pragma', 'no-cache');
        $response->headers->set('Expires', '0');
        $response->headers->set('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store, no-transform, private');

        return $response;
    }
}

Suddenly after upgrade of Livewire to version 3 it doesnt work. HTTP response contain these headers:

Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: max-age=0, must-revalidate, no-cache, no-store, private

I set my NoCacheControlHeaders middleware to be executed as last one in Http/Kernel.php's $middlewarePriority, but it doesnt help.


Solution

  • Turns out Livewire 3 has a feature called DisableBackButtonCache which uses livewire/src/Features/SupportDisablingBackButtonCache/DisableBackButtonCacheMiddleware.php middleware to overwrite the headers.

    At the time of writing this post there is no option to customize the headers or to turn this feature off in config.

    However it can be turned of for every component at runtime on mount event. Add this to your AppServiceProvider.php:boot():

    Livewire::listen('mount', function ($component) {
        $component->enableBackButtonCache();
    });
    

    GitHub discussion: https://github.com/livewire/livewire/discussions/8198