Skip to content

Commit

Permalink
Support text/javascript as JavaScript MIME type
Browse files Browse the repository at this point in the history
Next Rack release (probably 3.1) will include this change:

rack/rack@1bd0f15
sinatra#1857 (comment)
  • Loading branch information
dentarg committed Mar 5, 2023
1 parent e93842c commit f800370
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,13 @@ def mime_type(type, value = nil)
# mime_types :js # => ['application/javascript', 'text/javascript']
def mime_types(type)
type = mime_type type
type =~ %r{^application/(xml|javascript)$} ? [type, "text/#{$1}"] : [type]
if type =~ %r{^application/(xml|javascript)$}
[type, "text/#{$1}"]
elsif type =~ %r{^text/(xml|javascript)$}
[type, "application/#{$1}"]
else
[type]
end
end

# Define a before filter; runs before all requests within the same
Expand Down
5 changes: 4 additions & 1 deletion sinatra-contrib/spec/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def results_in(obj)

it "accepts shorthands for :content_type" do
mock_app { get('/') { json({}, :content_type => :js) } }
expect(get('/')["Content-Type"]).to eq("application/javascript;charset=utf-8")
# Changed to "text/javascript" in Rack >3.0
# https://github.com/sinatra/sinatra/pull/1857#issuecomment-1445062212
expect(get('/')["Content-Type"])
.to eq("application/javascript;charset=utf-8").or eq("text/javascript;charset=utf-8")
end

it 'calls generate on :encoder if available' do
Expand Down
4 changes: 3 additions & 1 deletion test/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,13 @@ def status_app(code, &block)
assert_equal content_type(:foo), 'text/foo;charset=utf-8'
assert_equal content_type(:xml), 'application/xml;charset=utf-8'
assert_equal content_type(:xhtml), 'application/xhtml+xml;charset=utf-8'
assert_equal content_type(:js), 'application/javascript;charset=utf-8'
assert_equal content_type(:json), 'application/json'
assert_equal content_type(:bar), 'application/bar'
assert_equal content_type(:png), 'image/png'
assert_equal content_type(:baz), 'application/baz;charset=utf-8'
# Changed to "text/javascript" in Rack >3.0
# https://github.com/sinatra/sinatra/pull/1857#issuecomment-1445062212
assert_match %r{^application|text/javascript;charset=utf-8$}, content_type(:js)
tests_ran = true
"done"
end
Expand Down

0 comments on commit f800370

Please sign in to comment.