@@ -796,63 +796,57 @@ public function modifyResponse(Request $request, Response $response)
796
796
$ this ->addClockworkHeaders ($ response );
797
797
}
798
798
799
+ try {
800
+ if ($ this ->hasCollector ('views ' ) && $ response ->headers ->has ('X-Inertia ' )) {
801
+ $ content = $ response ->getContent ();
802
+
803
+ if (is_string ($ content )) {
804
+ $ content = json_decode ($ content , true );
805
+ }
806
+
807
+ if (is_array ($ content )) {
808
+ $ this ['views ' ]->addInertiaAjaxView ($ content );
809
+ }
810
+ }
811
+ } catch (Exception $ e ) {
812
+ }
813
+
814
+ if ($ app ['config ' ]->get ('debugbar.add_ajax_timing ' , false )) {
815
+ $ this ->addServerTimingHeaders ($ response );
816
+ }
817
+
799
818
if ($ response ->isRedirection ()) {
800
819
try {
801
820
$ this ->stackData ();
802
821
} catch (Exception $ e ) {
803
822
$ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
804
823
}
805
- } elseif (
806
- $ this ->isJsonRequest ($ request ) &&
807
- $ app ['config ' ]->get ('debugbar.capture_ajax ' , true )
808
- ) {
809
- try {
810
- if ($ this ->hasCollector ('views ' ) && $ response ->headers ->has ('X-Inertia ' )) {
811
- $ content = $ response ->getContent ();
812
824
813
- if (is_string ($ content )) {
814
- $ content = json_decode ($ content , true );
815
- }
825
+ return $ response ;
826
+ }
816
827
817
- if (is_array ($ content )) {
818
- $ this ['views ' ]->addInertiaAjaxView ($ content );
819
- }
820
- }
821
- } catch (Exception $ e ) {
822
- }
823
- try {
824
- $ this ->sendDataInHeaders (true );
828
+ try {
829
+ // Collect + store data, only inject the ID in theheaders
830
+ $ this ->sendDataInHeaders (true );
831
+ } catch (Exception $ e ) {
832
+ $ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
833
+ }
825
834
826
- if ($ app ['config ' ]->get ('debugbar.add_ajax_timing ' , false )) {
827
- $ this ->addServerTimingHeaders ($ response );
828
- }
829
- } catch (Exception $ e ) {
830
- $ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
831
- }
832
- } elseif (
833
- !$ app ['config ' ]->get ('debugbar.inject ' , true ) ||
834
- ($ response ->headers ->has ('Content-Type ' ) &&
835
- strpos ($ response ->headers ->get ('Content-Type ' ), 'html ' ) === false ) ||
836
- $ request ->getRequestFormat () !== 'html ' ||
837
- $ response ->getContent () === false ||
838
- $ this ->isJsonRequest ($ request )
835
+ // Check if it's safe to inject the Debugbar
836
+ if (
837
+ $ app ['config ' ]->get ('debugbar.inject ' , true )
838
+ && str_contains ($ response ->headers ->get ('Content-Type ' , 'text/html ' ), 'html ' )
839
+ && !$ this ->isJsonRequest ($ request , $ response )
840
+ && $ response ->getContent () !== false
841
+ && in_array ($ request ->getRequestFormat (), [null , 'html ' ], true )
839
842
) {
840
- try {
841
- // Just collect + store data, don't inject it.
842
- $ this ->collect ();
843
- } catch (Exception $ e ) {
844
- $ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
845
- }
846
- } else {
847
843
try {
848
844
$ this ->injectDebugbar ($ response );
849
845
} catch (Exception $ e ) {
850
846
$ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
851
847
}
852
848
}
853
849
854
-
855
-
856
850
return $ response ;
857
851
}
858
852
@@ -889,9 +883,10 @@ protected function isDebugbarRequest()
889
883
890
884
/**
891
885
* @param \Symfony\Component\HttpFoundation\Request $request
886
+ * @param \Symfony\Component\HttpFoundation\Response $response
892
887
* @return bool
893
888
*/
894
- protected function isJsonRequest (Request $ request )
889
+ protected function isJsonRequest (Request $ request, Response $ response )
895
890
{
896
891
// If XmlHttpRequest, Live or HTMX, return true
897
892
if (
@@ -904,7 +899,17 @@ protected function isJsonRequest(Request $request)
904
899
905
900
// Check if the request wants Json
906
901
$ acceptable = $ request ->getAcceptableContentTypes ();
907
- return (isset ($ acceptable [0 ]) && $ acceptable [0 ] == 'application/json ' );
902
+ if (isset ($ acceptable [0 ]) && in_array ($ acceptable [0 ], ['application/json ' , 'application/javascript ' ], true )) {
903
+ return true ;
904
+ }
905
+
906
+ // Check if content looks like JSON without actually validating
907
+ $ content = $ response ->getContent ();
908
+ if ($ content !== false && in_array ($ content [0 ], ['{ ' , '[ ' ], true )) {
909
+ return true ;
910
+ }
911
+
912
+ return false ;
908
913
}
909
914
910
915
/**
0 commit comments