Class: Rack::LiveReload
- Inherits:
-
Object
- Object
- Rack::LiveReload
- Defined in:
- lib/adsf/live/rack_livereload.rb
Constant Summary collapse
- LIVERELOAD_JS_PATH =
'/__rack/livereload.js'- VENDORED_JS_PATH =
"#{__dir__}/../../../vendor/livereload.js"- HEAD_TAG_REGEX =
/<head( [^<]+)?>/.freeze
- LIVERELOAD_PORT =
35_729- LIVERELOAD_SCHEME =
'ws'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ LiveReload
constructor
A new instance of LiveReload.
Constructor Details
#initialize(app, options = {}) ⇒ LiveReload
Returns a new instance of LiveReload.
11 12 13 14 |
# File 'lib/adsf/live/rack_livereload.rb', line 11 def initialize(app, = {}) @app = app = end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/adsf/live/rack_livereload.rb', line 16 def call(env) return deliver_file(VENDORED_JS_PATH) if env['PATH_INFO']&.end_with? LIVERELOAD_JS_PATH status, headers, body = result = @app.call(env) if env['REQUEST_METHOD'] != 'GET' || headers['content-type'] !~ %r{text/html} || headers['transfer-encoding'] == 'chunked' || headers['content-disposition'] =~ /^inline/ return result end body.close if body.respond_to?(:close) new_body = [] livereload_added = false @env = env body.each do |line| if !livereload_added && line =~ HEAD_TAG_REGEX new_body << line.sub(HEAD_TAG_REGEX) { |match| %(#{match}#{template}) } livereload_added = true else new_body << line end end headers['content-length'] = new_body.sum(&:bytesize).to_s headers['x-rack-livereload'] = '1' if livereload_added [status, headers, new_body] end |