@@ -796,63 +796,57 @@ public function modifyResponse(Request $request, Response $response)
796796 $ this ->addClockworkHeaders ($ response );
797797 }
798798
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+
799818 if ($ response ->isRedirection ()) {
800819 try {
801820 $ this ->stackData ();
802821 } catch (Exception $ e ) {
803822 $ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
804823 }
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 ();
812824
813- if (is_string ($ content )) {
814- $ content = json_decode ($ content , true );
815- }
825+ return $ response ;
826+ }
816827
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+ }
825834
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 )
839842 ) {
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 {
847843 try {
848844 $ this ->injectDebugbar ($ response );
849845 } catch (Exception $ e ) {
850846 $ app ['log ' ]->error ('Debugbar exception: ' . $ e ->getMessage ());
851847 }
852848 }
853849
854-
855-
856850 return $ response ;
857851 }
858852
@@ -889,9 +883,10 @@ protected function isDebugbarRequest()
889883
890884 /**
891885 * @param \Symfony\Component\HttpFoundation\Request $request
886+ * @param \Symfony\Component\HttpFoundation\Response $response
892887 * @return bool
893888 */
894- protected function isJsonRequest (Request $ request )
889+ protected function isJsonRequest (Request $ request, Response $ response )
895890 {
896891 // If XmlHttpRequest, Live or HTMX, return true
897892 if (
@@ -904,7 +899,17 @@ protected function isJsonRequest(Request $request)
904899
905900 // Check if the request wants Json
906901 $ 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 ;
908913 }
909914
910915 /**
0 commit comments