Skip to content

Commit 99acf3e

Browse files
Do not build View watcher until the first updated? check
Currently initialization of every Rails::Engine leads to the creation of a new View watcher when the engine prepends its paths. This contributes to the time it takes to perform the first cold request on a lazy loaded application. This change delays the initialization of the View watcher until the first `updated?` check is performed. Co-Authored-By: Gannon McGibbon <[email protected]>
1 parent 2fa3294 commit 99acf3e

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

actionview/lib/action_view/cache_expiry.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ def initialize(watcher:, &block)
1010
@watcher = nil
1111
@previous_change = false
1212

13-
rebuild_watcher
14-
1513
ActionView::PathRegistry.file_system_resolver_hooks << method(:rebuild_watcher)
1614
end
1715

1816
def updated?
17+
build_watcher unless @watcher
1918
@previous_change || @watcher.updated?
2019
end
2120

2221
def execute
22+
return unless @watcher
23+
2324
watcher = nil
2425
@mutex.synchronize do
2526
@previous_change = false
@@ -33,7 +34,7 @@ def reload!
3334
ActionView::LookupContext::DetailsKey.clear
3435
end
3536

36-
def rebuild_watcher
37+
def build_watcher
3738
@mutex.synchronize do
3839
old_watcher = @watcher
3940

@@ -51,6 +52,11 @@ def rebuild_watcher
5152
end
5253
end
5354

55+
def rebuild_watcher
56+
return unless @watcher
57+
build_watcher
58+
end
59+
5460
def dirs_to_watch
5561
all_view_paths.uniq.sort
5662
end

actionview/lib/action_view/railtie.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class Railtie < Rails::Engine # :nodoc:
116116
view_reloader = ActionView::CacheExpiry::ViewReloader.new(watcher: app.config.file_watcher)
117117

118118
app.reloaders << view_reloader
119-
view_reloader.execute
120119
app.reloader.to_run do
121120
require_unload_lock!
122121
view_reloader.execute

0 commit comments

Comments
 (0)