Class: Phrender::RackMiddleware
- Inherits:
-
Object
- Object
- Phrender::RackMiddleware
- Defined in:
- lib/phrender/rack_middleware.rb
Direct Known Subclasses
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ RackMiddleware
constructor
A new instance of RackMiddleware.
Constructor Details
#initialize(app, opts = {}) ⇒ RackMiddleware
Returns a new instance of RackMiddleware.
6 7 8 9 10 11 12 |
# File 'lib/phrender/rack_middleware.rb', line 6 def initialize(app, opts = {}) @app = app @index_file = opts[:index_file] @javascript_paths = opts[:javascript_files] @raw_javascript = opts[:javascript].join(';') @phantom = Phrender::PhantomJSEngine.new(opts) end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/phrender/rack_middleware.rb', line 14 def call(env) @req = Rack::Request.new(env) # Check if the next middleware can handle the request status, headers, body = @app.call(@req.env) # If it can't, or if it's just the index file delivered via aliasing for # pushstate, do phrender stuff. if status == 404 || headers['Push-State-Redirect'] # If it's phantom making the request, then the phrender index file has # a request that the upstream server can't resolve, so catch it, instead # of recursively invoking the index if @req.user_agent && @req.user_agent.match(/PhantomJS/) [ 500, { 'Content-Type' => 'text/html' }, [ 'Server Error: HTML file contains recursive lookup' ] ] else # Render the page body = render(@req.url) [ 200, { 'Content-Type' => 'text/html' }, [ body ] ] end else [ status, headers, body ] end end |