@@ -81,16 +81,29 @@ public function handle(Request $request, Closure $next): Response
8181 $ jsonContent = json_encode ($ content );
8282
8383 if (in_array ('gzip ' , $ request ->getEncodings ()) && function_exists ('gzencode ' )) {
84- $ level = 9 ; // best compression;
85- $ jsonContent = gzencode ($ jsonContent , $ level );
84+ $ level = 9 ; // Best compression.
85+ $ compressedContent = gzencode ($ jsonContent , $ level );
86+
87+ // Create a new response object with compressed content.
88+ $ response = new Response ($ compressedContent );
89+
90+ // Add necessary headers.
8691 $ response ->headers ->add ([
87- 'Content-type ' => 'application/json; charset=utf-8 ' ,
88- 'Content-Length ' => strlen ($ jsonContent ),
92+ 'Content-Type ' => 'application/json; charset=utf-8 ' ,
93+ 'Content-Length ' => strlen ($ compressedContent ),
8994 'Content-Encoding ' => 'gzip ' ,
9095 ]);
96+
97+ return $ response ; // Return the response object directly.
98+ } else {
99+ // Fallback for clients that do not support gzip.
100+ $ response = new Response ($ jsonContent );
101+ $ response ->headers ->add ([
102+ 'Content-Type ' => 'application/json; charset=utf-8 ' ,
103+ ]);
104+
105+ return $ response ;
91106 }
92- $ response ->setContent ($ jsonContent );
93- return $ response ;
94107 }
95108
96109 public function listenToDB (): void
0 commit comments