LoginSignup
7
6

More than 5 years have passed since last update.

Ruby on Rails 4 - compressorを指定しても、minifyされない時の対処法

Last updated at Posted at 2016-01-31

キャッシュクリア

bundle exec rake tmp:cache:clear

とか

rake tmp:cache:clear

でキャッシュをクリアする。

public/assets配下のファイルを削除

public/assets/*.jsを削除するか、config/environments/production.rbconfig.assets.js_compressorを変更する。

今回は
public/assetsにファイルがある場合は、削除する。
の方針で、対処してみましたが、Rails 4ではpublic/assetsは作成されないようなので、特に何もしてません。

rm public/assets/*.js

キャッシュクリアのみでちゃんとminifyされました。

設定ファイル

開発環境の設定ファイル(config/environments/development.rb)で確認してみました。
設定は下記のようになっています。

config/environments/development.rb
  # From production
  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  config.assets.css_compressor = :sass

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = false

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = false

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

config.assets.digestについて

config.assets.digestの設定がなんなのか最初良くわからなかったのですが、
これをconfig.assets.digest = trueとしておくと、アセットファイル名にハッシュ値(MD5)が付加されるようです。

例)application.css ⇒ application-908e25f4bf641868d8683022a5b62f54.css

ファイル名にハッシュ値がつくということは、内容が少しでも変わる度に別のファイル名で保存されるということです。
以前にプリコンパイルされた古いアセットファイルや古いキャッシュが残ってしまう可能性があるので(ここは未確認)、毎回、キャッシュクリア(rake tmp:cache:clear)して、プリコンパイル(rake assets:precompile)した方が良いかと思います。

#!/bin/bash
#
# キャッシュクリアしてプリコンパイルする
#

rake tmp:cache:clear
rake assets:precompile

みたいなスクリプトを組んでおくのも良いかもしれませんね。

参考URL

Rails 4にアップグレードするとassets:precompile しても minify されない

アセットパイプライン | Rails ガイド

5分でわかる!? アセットパイプライン(Assets Pipeline)

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6