Skip to content

Assets are still compiled when config.assets.compile set false (including in production) #2886

Closed
wants to merge 6 commits into from
View
22 actionpack/lib/sprockets/assets.rake
@@ -21,6 +21,16 @@ namespace :assets do
target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
manifest = {}
manifest_path = config.assets.manifest || target
+ case config.assets.undigested_as
+ when NilClass
+ undigested_as = :duplicate
+ when :none, false
+ undigested_as = false
+ when :duplicate, :symlink, :hardlink
+ undigested_as = config.assets.undigested_as
+ else
+ raise "config.assets.undigested_as was '#{config.assets.undigested_as.inspect}'; must be one of: :duplicate, :symlink, :hardlink, :none, false"
+ end
config.assets.precompile.each do |path|
env.each_logical_path do |logical_path|
@@ -38,6 +48,18 @@ namespace :assets do
mkdir_p filename.dirname
asset.write_to(filename)
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
+
+ if config.assets.digest && undigested_as
+ undigested = target.join(logical_path)
+ case undigested_as
+ when :duplicate
+ asset.write_to(undigested)
+ when :symlink
+ symlink(filename, undigested, :force => true)
+ when :hardlink
+ link(filename, undigested, :force => true)
+ end
+ end
end
end
end
View
2 actionpack/lib/sprockets/railtie.rb
@@ -50,7 +50,7 @@ class Railtie < ::Rails::Railtie
# are compiled, and so that other Railties have an opportunity to
# register compressors.
config.after_initialize do |app|
- next unless app.assets
+ next unless app.assets && app.config.assets.compile
config = app.config
config.assets.paths.each { |path| app.assets.append_path(path) }
View
1 railties/lib/rails/application/configuration.rb
@@ -47,6 +47,7 @@ def initialize(*)
@assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/" ]
@assets.js_compressor = nil
@assets.css_compressor = nil
+ @assets.undigested_as = :duplicate
end
def compiled_asset_path
View
4 railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
@@ -20,6 +20,10 @@
# Generate digests for assets URLs
config.assets.digest = true
+ # Method to use to additionally create assets without digests
+ # Valid values are :duplicate, :none, :symlink, :hardlink
+ # config.assets.undigested_as = :duplicate
+
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
View
12 railties/test/application/assets_test.rb
@@ -137,6 +137,7 @@ def app
test "assets do not require any assets group gem when manifest file is present" do
app_file "app/assets/javascripts/application.js", "alert();"
+ app_file "config/initializers/serve_static_assets.rb", "Rails.application.config.serve_static_assets = true"
ENV["RAILS_ENV"] = "production"
capture(:stdout) do
@@ -243,6 +244,17 @@ class ::PostsController < ActionController::Base ; end
assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
end
+ test "assets routes are not drawn when compilation is disabled" do
+ app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
+ app_file "config/initializers/compile.rb", "Rails.application.config.assets.compile = false"
+
+ ENV["RAILS_ENV"] = "production"
+ require "#{app_path}/config/environment"
+
+ get "/assets/demo.js"
+ assert_equal 404, last_response.status
+ end
+
test "does not stream session cookies back" do
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
Something went wrong with that request. Please try again.