It seems like the issue you're encountering is related to the GzipEncodeResponse middleware that is compressing the response content. However, it's important to ensure that the response is only compressed when the client can handle it and when the response has not already been compressed. Additionally, you should only compress responses with a content type that benefits from Gzip, such as text-based content.
Here's an updated version of your middleware that includes checks for the content type and ensures that the response is not already encoded:
class GzipEncodeResponse
{
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
// Only compress text-based responses
$contentType = $response->headers->get('Content-Type');
if (strpos($contentType, 'text/') === false && strpos($contentType, 'json') === false) {
return $response;
}
// Check if the response should be Gzip encoded
if (!$response->headers->has('Content-Encoding') &&
in_array('gzip', $request->getEncodings()) &&
function_exists('gzencode') &&
!$response->isRedirection() &&
($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)
) {
$content = gzencode($response->getContent(), 9);
$response->setContent($content);
$response->headers->set('Content-Encoding', 'gzip');
$response->headers->set('Vary', 'Accept-Encoding', false);
$response->headers->set('Content-Length', strlen($content));
}
return $response;
}
}
Here are the changes made:
- Added a check for the content type to ensure we're only compressing text-based content (
text/*andapplication/json). - Added a check to ensure the response is not already encoded (
Content-Encodingheader is not set). - Added a check to ensure we're not compressing redirections or responses with status codes outside the 200 range.
- Set the
Varyheader toAccept-Encodingto inform caches that the response varies based on theAccept-Encodingrequest header. - Updated the
Content-Lengthheader with the length of the compressed content.
Make sure to test this updated middleware thoroughly to ensure it works as expected with your application and does not interfere with Filament or any other part of your application.