@@ -15,11 +15,11 @@ def call!(env)
1515 @app . call ( env )
1616 end
1717 else
18- request = Rack ::Request . new ( env )
18+ request = ActionDispatch ::Request . new ( env )
1919 status , headers , body = @app . call ( env )
2020
2121 if html? ( headers ) && ( status == 500 || status == 422 || ( status . to_s =~ /20./ && request . get? ) )
22- return inject_rails_live_reload ( status , headers , body )
22+ return inject_rails_live_reload ( request , status , headers , body )
2323 end
2424
2525 [ status , headers , body ]
@@ -28,25 +28,26 @@ def call!(env)
2828
2929 private
3030
31- def inject_rails_live_reload ( status , headers , body )
31+ def inject_rails_live_reload ( request , status , headers , body )
3232 response = Rack ::Response . new ( [ ] , status , headers )
33-
33+
34+ nonce = request &.content_security_policy_nonce
3435 if String === body
35- response . write make_new_response ( body )
36+ response . write make_new_response ( body , nonce )
3637 else
37- body . each { |fragment | response . write make_new_response ( fragment ) }
38+ body . each { |fragment | response . write make_new_response ( fragment , nonce ) }
3839 end
3940 body . close if body . respond_to? ( :close )
4041 response . finish
4142 end
4243
43- def make_new_response ( body )
44+ def make_new_response ( body , nonce )
4445 index = body . rindex ( /<\/ body>/i ) || body . rindex ( /<\/ html>/i )
4546 return body if index . nil?
4647
4748 body . insert ( index , <<~HTML . html_safe )
4849 < script defer type ="text/javascript " src ="#{ RailsLiveReload . config . url } /script"> </ script>
49- < script id ="rails-live-reload-options " type ="application/json ">
50+ < script id ="rails-live-reload-options " type ="application/json " nonce =" #{ nonce } " >
5051 #{ {
5152 files : CurrentRequest . current . data . to_a ,
5253 time : Time . now . to_i ,
@@ -57,7 +58,7 @@ def make_new_response(body)
5758 end
5859
5960 def html? ( headers )
60- headers [ "Content-Type " ] . to_s . include? ( "text/html" )
61+ headers [ "content-type " ] . to_s . include? ( "text/html" )
6162 end
6263 end
6364 end
0 commit comments