statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.71k stars 508 forks source link

Static Cache strips out custom headers #8478

Closed AntonCooper closed 5 months ago

AntonCooper commented 1 year ago

Bug description

Using the Static Caching half measures we've encountered an issue with response headers, applied via custom middleware or controller logic, being stripped out of a cached response.

Statamic\StaticCaching\Cachers\ApplicationCacher only seems to cache the response contents and ignores any other response object details, including headers. This can result in cached responses missing important headers which may have been defined anywhere in the request stack.

How to reproduce

Create a controller which returns a response with a text/plain header.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Statamic\Facades\Entry;

class AdsTxtController
{
    public function __invoke(Request $request): Response
    {
        $entry = Entry::find(1);

        return response($entry->content)->withHeaders([
            'Content-Type' => 'text/plain',
        ]);
    }
}

Add a route with the statamic.web middleware

    Route::get('ads.txt', AdsTxtController::class)
        ->middleware('statamic.web');

Make a request to the endpoint - observe the content-type is different from the request without the cache middleware. With middleware image

Without middleware image

Logs

No response

Environment

Environment
Application Name: ****
Laravel Version: 9.52.7
PHP Version: 8.2.5
Composer Version: 2.5.7
Environment: local
Debug Mode: ENABLED
URL: ****.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: pusher
Cache: redis
Database: mysql
Logs: stack / single
Mail: log
Queue: redis
Session: file

Statamic
Addons: 7
Antlers: runtime
Stache Watcher: Disabled
Static Caching: half
Version: 4.11.0 PRO

Statamic Addons
jacksleight/statamic-bard-mutator: 2.3.0
jacksleight/statamic-bard-texstyle: 3.1.3
jacksleight/statamic-distill: 0.4.2
mitydigital/feedamic: 2.2.5
statamic/collaboration: 0.7.2
statamic/eloquent-driver: 2.3.1
transformstudios/review: 4.1.0

Installation

Existing Laravel app

Antlers Parser

None

Additional details

No response

wesort commented 11 months ago

I think this relates to #2259 which (in my opinion) was prematurely closed.

duncanmcclean commented 5 months ago

This has been fixed in Statamic 5, which we're hoping to release in the next few days/weeks.