Module: Sinatra::JsSupport
- Defined in:
- lib/sinatra/support/jssupport.rb
Overview
Adds a route for JavaScript files.
Usage
require 'sinatra/support/jssupport'
class Main < Sinatra::Base
register Sinatra::JsSupport
serve_js '/js', from: './app/js'
end
You’ll be able to access files via /js
:
# This will serve /app/js/jquery.js. (or .coffee)
$ curl "http://localhost:4567/js/jquery.js"
CoffeeScript support
This plugin supports CoffeeScript. To use it, simply add a CoffeeScript file in the JS file path.
# Will first try app/js/application.coffee,
# then move onto app/js/application.js if it's not found.
$ curl "http://localhost:4567/js/application.js"
To use CoffeeScript, install the coffee_script
gem. If you’re using Bundler, that is:
# Gemfile
gem "coffee-script", require: "coffee_script"
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.registered(app) ⇒ Object
34 35 36 |
# File 'lib/sinatra/support/jssupport.rb', line 34 def self.registered(app) app.set :js_max_age, app.development? ? 0 : 86400*30 end |
Instance Method Details
#serve_js(url_prefix, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sinatra/support/jssupport.rb', line 38 def serve_js(url_prefix, ={}) path = File.join(url_prefix, '*.js') prefix = [:from] get path do |name| fname = Dir[File.join(prefix, "#{name}.{js,coffee}")].first or pass content_type :js last_modified File.mtime(fname) cache_control :public, :must_revalidate, :max_age => settings.js_max_age if fname =~ /\.coffee$/ coffee File.read(fname) else send_file fname end end end |