hotwire-livereload 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44376128ac37b1619d34c7ee8dee64eb1901804cc170dd0bd5c2114973e98225
4
- data.tar.gz: 9053700099d671c33b11fa0483100ec5f921016a7e2f1315113072c5c1bba587
3
+ metadata.gz: 76c9e1c7f17f98e067a076d8dfc624d03aa1a7a64784da9fdde0b59f211bebe5
4
+ data.tar.gz: 3fa02e7e5a7592c61be7db173ff945f9d2f6da2e29ba4a777072702619b54473
5
5
  SHA512:
6
- metadata.gz: 5677a61e971bfa24bb8c8ea76970420e370e03003f07970632bf07b22c187cc6dfc3289963509c8597f7b12c08e112d8db56f308fa85008023c3160a9f700846
7
- data.tar.gz: 4e91a4a01f3e5b8d54e18b2344351c17bef3e1ac09568adc3b1802dc6a8f95eaa4abc6c972e93c8edd7eafa61a944e2b390e339697b1bc802cc7a8e85176b6ff
6
+ metadata.gz: 760b3adbb7ef2bc0231f8e90f7ec3b5bfabbc2c01965518d1c69ab9885b0974cbdf0551d1f44e72a498917c55f790947fbad61ec1267973ac139ff85704c2a2f
7
+ data.tar.gz: 449e13ba3578d4fcc8e41e4939abecd29ee1a9673cdeaa7a0da24090c31de249868ae7c6541ed37c181cc4a4a23a40c6ee04f83b6922b65be3d9b0d92131d44c
data/README.md CHANGED
@@ -20,7 +20,7 @@ Run installer:
20
20
  rails livereload:install
21
21
  ```
22
22
 
23
- Folders listened by default:
23
+ Folders watched by default:
24
24
  - `app/views`
25
25
  - `app/helpers`
26
26
  - `app/javascript`
@@ -32,6 +32,12 @@ Folders listened by default:
32
32
 
33
33
  The gem detects if you use [`jsbundling-rails`](https://github.com/rails/jsbundling-rails) or [`cssbundling-rails`](https://github.com/rails/cssbundling-rails) and watches for changes in their output folder `app/assets/builds` automatically.
34
34
 
35
+ In your layout, make sure you don't `turbo-track` your JS/CSS in development:
36
+ ```diff
37
+ + <%= stylesheet_link_tag "application", "data-turbo-track": Rails.env.production? ? "reload" : "" %>
38
+ - <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
39
+ ```
40
+
35
41
  ## Configuration
36
42
 
37
43
  ### Listen paths
@@ -46,6 +52,16 @@ Rails.application.configure do
46
52
  end
47
53
  ```
48
54
 
55
+ You can skip one or few default listen paths:
56
+ ```ruby
57
+ # config/environments/development.rb
58
+
59
+ Rails.application.configure do
60
+ # ...
61
+ config.hotwire_livereload.skip_listen_paths << Rails.root.join("app/views")
62
+ end
63
+ ```
64
+
49
65
  You can disable default listen paths and fully override them:
50
66
  ```ruby
51
67
  # config/environments/development.rb
@@ -112,6 +128,21 @@ Rails.application.configure do
112
128
  end
113
129
  ```
114
130
 
131
+ ### Listen debounce delay
132
+
133
+ If your app uses TailwindCSS or similar that compiles your CSS from looking at your templates, you can end up in a situation, where updating a template triggers twice for changes; once for the template and once for the rebuilt CSS. This can lead to unreliable reloads, ie. the reload happening before the CSS is built.
134
+
135
+ To avoid this, you can add a debounce delay to the file watcher:
136
+
137
+ ```ruby
138
+ # config/environments/development.rb
139
+
140
+ Rails.application.configure do
141
+ # ...
142
+ config.hotwire_livereload.debounce_delay_ms = 300 # in milliseconds
143
+ end
144
+ ```
145
+
115
146
  ## Disable livereload
116
147
 
117
148
  To temporarily disable livereload use:
@@ -8,6 +8,7 @@ module Hotwire
8
8
  isolate_namespace Hotwire::Livereload
9
9
  config.hotwire_livereload = ActiveSupport::OrderedOptions.new
10
10
  config.hotwire_livereload.listen_paths ||= []
11
+ config.hotwire_livereload.skip_listen_paths ||= []
11
12
  config.hotwire_livereload.force_reload_paths ||= []
12
13
  config.hotwire_livereload.reload_method = :action_cable
13
14
  config.hotwire_livereload.disable_default_listeners = false
@@ -16,6 +17,7 @@ module Hotwire
16
17
  #{root}/app/helpers
17
18
  ]
18
19
  config.hotwire_livereload.listen_options ||= {}
20
+ config.hotwire_livereload.debounce_delay_ms = 0
19
21
 
20
22
  initializer "hotwire_livereload.assets" do
21
23
  if Rails.application.config.respond_to?(:assets)
@@ -31,6 +33,7 @@ module Hotwire
31
33
 
32
34
  initializer "hotwire_livereload.set_configs" do |app|
33
35
  options = app.config.hotwire_livereload
36
+ skip_listen_paths = options.skip_listen_paths.map(&:to_s).uniq
34
37
 
35
38
  unless options.disable_default_listeners
36
39
  default_listen_paths = %w[
@@ -55,11 +58,20 @@ module Hotwire
55
58
  .uniq
56
59
  .map { |p| Rails.root.join(p) }
57
60
  .select { |p| Dir.exist?(p) }
61
+ .reject { |p| skip_listen_paths.include?(p.to_s) }
58
62
  end
59
63
  end
60
64
 
61
65
  config.after_initialize do |app|
62
66
  if Rails.env.development? && Hotwire::Livereload.server_process?
67
+ @trigger_reload = (Hotwire::Livereload.debounce(config.hotwire_livereload.debounce_delay_ms) do |options|
68
+ if config.hotwire_livereload.reload_method == :turbo_stream
69
+ Hotwire::Livereload.turbo_stream(options)
70
+ else
71
+ Hotwire::Livereload.action_cable(options)
72
+ end
73
+ end)
74
+
63
75
  options = app.config.hotwire_livereload
64
76
  listen_paths = options.listen_paths.map(&:to_s).uniq
65
77
  force_reload_paths = options.force_reload_paths.map(&:to_s).uniq.join("|")
@@ -74,11 +86,7 @@ module Hotwire
74
86
  end
75
87
 
76
88
  options = {changed: changed, force_reload: force_reload}
77
- if config.hotwire_livereload.reload_method == :turbo_stream
78
- Hotwire::Livereload.turbo_stream(options)
79
- else
80
- Hotwire::Livereload.action_cable(options)
81
- end
89
+ @trigger_reload.call(options)
82
90
  end
83
91
  end
84
92
  @listener.start
@@ -111,5 +119,27 @@ module Hotwire
111
119
 
112
120
  puma_process || rails_server
113
121
  end
122
+
123
+ def self.debounce(wait_ms, &block)
124
+ if wait_ms.zero?
125
+ return ->(*args) { yield(*args) }
126
+ end
127
+
128
+ mutex = Mutex.new
129
+ timer_thread = nil
130
+ seconds = wait_ms.to_f / 1000.0
131
+
132
+ lambda do |*args|
133
+ mutex.synchronize do
134
+ # Cancel previous timer
135
+ timer_thread&.kill
136
+
137
+ timer_thread = Thread.new do
138
+ sleep(seconds)
139
+ yield(*args)
140
+ end
141
+ end
142
+ end
143
+ end
114
144
  end
115
145
  end
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Livereload
3
- VERSION = "1.3.2"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwire-livereload
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Platonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-28 00:00:00.000000000 Z
11
+ date: 2024-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties