Module: Sprockets::Server
- Included in:
- Base
- Defined in:
- lib/sprockets/server.rb
Overview
‘Server` is a concern mixed into `Environment` and `Index` that provides a Rack compatible `call` interface and url generation helpers.
Instance Method Summary collapse
-
#call(env) ⇒ Object
‘call` implements the Rack 1.x specification which accepts an `env` Hash and returns a three item tuple with the status code, headers, and body.
-
#path(logical_path, fingerprint = true, prefix = nil) ⇒ Object
Deprecated: ‘path` is a url helper that looks up an asset given a `logical_path` and returns a path String.
-
#url(env, logical_path, fingerprint = true, prefix = nil) ⇒ Object
Deprecated: Similar to ‘path`, `url` returns a full url given a Rack `env` Hash and a `logical_path`.
Instance Method Details
#call(env) ⇒ Object
‘call` implements the Rack 1.x specification which accepts an `env` Hash and returns a three item tuple with the status code, headers, and body.
Mapping your environment at a url prefix will serve all assets in the path.
map "/assets" do
run Sprockets::Environment.new
end
A request for ‘“/assets/foo/bar.js”` will search your environment for `“foo/bar.js”`.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sprockets/server.rb', line 22 def call(env) start_time = Time.now.to_f time_elapsed = lambda { ((Time.now.to_f - start_time) * 1000).to_i } msg = "Served asset #{env['PATH_INFO']} -" # URLs containing a `".."` are rejected for security reasons. if forbidden_request?(env) return forbidden_response end # Mark session as "skipped" so no `Set-Cookie` header is set env['rack.session.options'] ||= {} env['rack.session.options'][:defer] = true env['rack.session.options'][:skip] = true # Extract the path from everything after the leading slash path = env['PATH_INFO'].to_s.sub(/^\//, '') # Look up the asset. asset = find_asset(path) asset.to_a if asset # `find_asset` returns nil if the asset doesn't exist if asset.nil? logger.info "#{msg} 404 Not Found (#{time_elapsed.call}ms)" # Return a 404 Not Found not_found_response # Check request headers `HTTP_IF_MODIFIED_SINCE` and # `HTTP_IF_NONE_MATCH` against the assets mtime and digest elsif not_modified?(asset, env) || etag_match?(asset, env) logger.info "#{msg} 304 Not Modified (#{time_elapsed.call}ms)" # Return a 304 Not Modified not_modified_response(asset, env) else logger.info "#{msg} 200 OK (#{time_elapsed.call}ms)" # Return a 200 with the asset contents ok_response(asset, env) end rescue Exception => e logger.error "Error compiling asset #{path}:" logger.error "#{e.class.name}: #{e.}" case content_type_of(path) when "application/javascript" # Re-throw JavaScript asset exceptions to the browser logger.info "#{msg} 500 Internal Server Error\n\n" return javascript_exception_response(e) when "text/css" # Display CSS asset exceptions in the browser logger.info "#{msg} 500 Internal Server Error\n\n" return css_exception_response(e) else raise end end |
#path(logical_path, fingerprint = true, prefix = nil) ⇒ Object
Deprecated: ‘path` is a url helper that looks up an asset given a `logical_path` and returns a path String. By default, the asset’s digest fingerprint is spliced into the filename.
/assets/application-3676d55f84497cbeadfc614c1b1b62fc.js
A third ‘prefix` argument can be pass along to be prepended to the string.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sprockets/server.rb', line 92 def path(logical_path, fingerprint = true, prefix = nil) logger.warn "Sprockets::Environment#path is deprecated\n#{caller[0..2].join("\n")}" if fingerprint && asset = find_asset(logical_path.to_s.sub(/^\//, '')) url = asset.digest_path else url = logical_path end url = File.join(prefix, url) if prefix url = "/#{url}" unless url =~ /^\// url end |
#url(env, logical_path, fingerprint = true, prefix = nil) ⇒ Object
Deprecated: Similar to ‘path`, `url` returns a full url given a Rack `env` Hash and a `logical_path`.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sprockets/server.rb', line 108 def url(env, logical_path, fingerprint = true, prefix = nil) logger.warn "Sprockets::Environment#url is deprecated\n#{caller[0..2].join("\n")}" req = Rack::Request.new(env) url = req.scheme + "://" url << req.host if req.scheme == "https" && req.port != 443 || req.scheme == "http" && req.port != 80 url << ":#{req.port}" end url << path(logical_path, fingerprint, prefix) url end |