LoginSignup
11
12

More than 5 years have passed since last update.

Railsの再読み込みの仕組みが意外とざっくりしていた

Posted at

修正されたファイルがピンポイントで再読み込みされると思っていたのですが、autoload_paths以下のファイルやconfig/routes.rb等が修正された場合は全て再読み込みになるようです。非常にざっくりとした流れは以下の通り。

まずリクエストを受けるとreload_dependencies?autoload_paths以下のファイル等が更新されているかチェック。

railties/lib/rails/application/default_middleware_stack.rb
def reload_dependencies?
  config.reload_classes_only_on_change != true || app.reloaders.map(&:updated?).any?
end

更新されているとこちらのコールバックが呼ばれます。

railties/lib/rails/application/finisher.rb
callback = lambda do
  ActiveSupport::DescendantsTracker.clear
  ActiveSupport::Dependencies.clear
end

ActiveSupport::Dependencies.clear以下自動読み込みされた定数がremove_constで全て削除され、その後リクエストを処理する過程で改めてconst_missingからファイルが再読み込みされます。

11
12
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
11
12