Class: ScriptRelocator::Rack
- Inherits:
-
Object
- Object
- ScriptRelocator::Rack
- Defined in:
- lib/script_relocator.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Rack
constructor
A new instance of Rack.
Constructor Details
#initialize(app) ⇒ Rack
Returns a new instance of Rack.
6 7 8 |
# File 'lib/script_relocator.rb', line 6 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/script_relocator.rb', line 10 def call(env) status, headers, response = @app.call(env) if headers['Content-Type'] !~ %r{^text/html} return status, headers, response end response_body = '' response.each { |part| response_body << part } doc = Nokogiri::HTML(response_body) scripts = doc.css('script') return status, headers, response if scripts.empty? scripts.each do |s| s['data-turbolinks-eval'] = 'false' if s.parent.name == 'head' s.remove doc.at('body') << s end transformed_body = doc.to_html if headers.key?('Content-Length') && headers['Content-Length'].to_i != transformed_body.length headers['Content-Length'] = transformed_body.length.to_s end return status, headers, [transformed_body] end |