Module: Roda::RodaPlugins::TimestampPublic

Defined in:
lib/roda/plugins/timestamp_public.rb

Overview

The timestamp_public plugin adds a timestamp_path method for constructing timestamp paths, and a r.timestamp_public routing method to serve static files from a directory (using the public plugin). This plugin is useful when you want to modify the path to static files when the modify timestamp on the file changes, ensuring that requests for the static file will not be cached.

Note that while this plugin will not serve files outside of the public directory, for performance reasons it does not check the path of the file is inside the public directory when getting the modify timestamp. If the timestamp_path method is called with untrusted input, it is possible for an attacker to get the modify timestamp for any file on the file system.

Examples:

# Use public folder as location of files, and static as the path prefix
plugin :timestamp_public

# Use /path/to/app/static as location of files, and public as the path prefix
opts[:root] = '/path/to/app'
plugin :public, root: 'static', prefix: 'public'

# Assuming public is the location of files, and static is the path prefix
r.route do
  # Make GET /static/1238099123/images/foo.png look for public/images/foo.png 
  r.timestamp_public

  r.get "example" do
    # "/static/1238099123/images/foo.png"
    timestamp_path("images/foo.png")
  end
end

Defined Under Namespace

Modules: InstanceMethods, RequestMethods

Class Method Summary collapse

Class Method Details

.configure(app, opts = {}) ⇒ Object

Use options given to setup timestamped file serving. The following option is recognized by the plugin:

:prefix

The prefix for paths, before the timestamp segment

The options given are also passed to the public plugin.



44
45
46
47
# File 'lib/roda/plugins/timestamp_public.rb', line 44

def self.configure(app, opts={})
  app.plugin :public, opts
  app.opts[:timestamp_public_prefix] = (opts[:prefix] || app.opts[:timestamp_public_prefix] || "static").dup.freeze
end