Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Change rack session options only when asset is found #421

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/sprockets/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ def call(env)

msg = "Served asset #{env['PATH_INFO']} -"

# 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 = unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))

Expand All @@ -45,6 +40,8 @@ def call(env)

# Look up the asset.
asset = find_asset(path, :bundle => !body_only?(env))
# Do not change session options when pass the request to other routes.
skip_session(env) unless asset.nil?

# `find_asset` returns nil if the asset doesn't exist
if asset.nil?
Expand Down Expand Up @@ -243,5 +240,12 @@ def unescape(str)
def etag(asset)
%("#{asset.digest}")
end

# Mark session as "skipped" so no `Set-Cookie` header is set
def skip_session(env)
env['rack.session.options'] ||= {}
env['rack.session.options'][:defer] = true
env['rack.session.options'][:skip] = true
end
end
end
13 changes: 13 additions & 0 deletions test/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def app
last_response.headers['ETag']
end

test "serve source with session skipped" do
rack_env = { 'PATH_INFO' => "application.js" }
@env.call(rack_env)
assert_equal true, rack_env['rack.session.options'][:skip]
assert_equal true, rack_env['rack.session.options'][:defer]
end

test "updated file updates the last modified header" do
time = Time.now
path = fixture_path "server/app/javascripts/foo.js"
Expand Down Expand Up @@ -165,6 +172,12 @@ def app
assert_equal "pass", last_response.headers['X-Cascade']
end

test "missing source does not touch env" do
rack_env = { 'PATH_INFO' => "none.js" }
@env.call(rack_env)
assert_equal nil, rack_env['rack.session.options']
end

test "re-throw JS exceptions in the browser" do
get "/assets/missing_require.js"
assert_equal 200, last_response.status
Expand Down