hotwire-livereload 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 588323a498a0f2aa7b4ebb53c417b0f937b3d9df5e06af8ec4039e26f35de0c9
4
- data.tar.gz: 15c0688010ad47a244cbcf4c1bb543ce199f5ba9f73b822228b9d5aafe71c5b0
3
+ metadata.gz: 44376128ac37b1619d34c7ee8dee64eb1901804cc170dd0bd5c2114973e98225
4
+ data.tar.gz: 9053700099d671c33b11fa0483100ec5f921016a7e2f1315113072c5c1bba587
5
5
  SHA512:
6
- metadata.gz: c4389aeae81c93d5f9ec646748e587a084bebf0993173bce97bf3629f81dd1c8763d31635729218b0e2631b5529a16e2d63403c696301f24c9a788d84aa6c5fc
7
- data.tar.gz: 429fb41ac85243a1dd9aa0b7f5461b189e10f06f98e5547af125f65996026b4ea44ea1ceceb8c0dbe07ce297091438490d257b51b0e7f9319e0e7e7a80bbf6f2
6
+ metadata.gz: 5677a61e971bfa24bb8c8ea76970420e370e03003f07970632bf07b22c187cc6dfc3289963509c8597f7b12c08e112d8db56f308fa85008023c3160a9f700846
7
+ data.tar.gz: 4e91a4a01f3e5b8d54e18b2344351c17bef3e1ac09568adc3b1802dc6a8f95eaa4abc6c972e93c8edd7eafa61a944e2b390e339697b1bc802cc7a8e85176b6ff
data/README.md CHANGED
@@ -34,6 +34,8 @@ The gem detects if you use [`jsbundling-rails`](https://github.com/rails/jsbundl
34
34
 
35
35
  ## Configuration
36
36
 
37
+ ### Listen paths
38
+
37
39
  You can watch for changes in additional folders by adding them to `listen_paths`:
38
40
  ```ruby
39
41
  # config/environments/development.rb
@@ -58,6 +60,8 @@ Rails.application.configure do
58
60
  end
59
61
  ```
60
62
 
63
+ ### Force reload
64
+
61
65
  If you don't have `data-turbo-track="reload"` attribute on your JS and CSS bundles you might need to setup force reloading. This will trigger full browser reloading for JS and CSS files only:
62
66
  ```ruby
63
67
  # config/environments/development.rb
@@ -69,6 +73,8 @@ Rails.application.configure do
69
73
  end
70
74
  ```
71
75
 
76
+ ### Reload method
77
+
72
78
  Instead of a direct ActionCable websocket connection, you can reuse the existing TurboStream websocket connection and send updates using standard turbo-streams:
73
79
  ```ruby
74
80
  # config/environments/development.rb
@@ -90,6 +96,22 @@ In that case you need to place `hotwire_livereload_tags` helper in your layout *
90
96
  </head>
91
97
  ```
92
98
 
99
+ ### Listen options
100
+
101
+ [Listen gem](https://github.com/guard/listen), which is used for file system monitoring, accepts [options](https://github.com/guard/listen?tab=readme-ov-file#options) like enabling a fallback mechanism called "polling" to detect file changes.
102
+
103
+ By default, Listen uses a more efficient mechanism called "native" which relies on the operating system's file system events to detect changes. However, in some cases, such as when working with network-mounted file systems or in certain virtualized environments, the native mechanism may not work reliably. In such cases, enabling force_polling ensures that file changes are still detected, albeit with a slightly higher resource usage.
104
+
105
+ You may use listen_options to pass these options like:
106
+ ```ruby
107
+ # config/environments/development.rb
108
+
109
+ Rails.application.configure do
110
+ # ...
111
+ config.hotwire_livereload.listen_options[:force_polling] = true
112
+ end
113
+ ```
114
+
93
115
  ## Disable livereload
94
116
 
95
117
  To temporarily disable livereload use:
@@ -81,6 +81,32 @@
81
81
 
82
82
  // app/javascript/lib/hotwire-livereload-received.js
83
83
  var import_debounce = __toESM(require_debounce());
84
+
85
+ // app/javascript/lib/hotwire-livereload-scroll-position.js
86
+ var KEY = "hotwire-livereload-scrollPosition";
87
+ function read() {
88
+ const value = localStorage.getItem(KEY);
89
+ if (!value)
90
+ return;
91
+ return parseInt(value);
92
+ }
93
+ function save() {
94
+ const pos = window.scrollY;
95
+ localStorage.setItem(KEY, pos.toString());
96
+ }
97
+ function remove() {
98
+ localStorage.removeItem(KEY);
99
+ }
100
+ function restore() {
101
+ const value = read();
102
+ if (value) {
103
+ console.log("[Hotwire::Livereload] Restoring scroll position to", value);
104
+ window.scrollTo(0, value);
105
+ }
106
+ }
107
+ var hotwire_livereload_scroll_position_default = { read, save, restore, remove };
108
+
109
+ // app/javascript/lib/hotwire-livereload-received.js
84
110
  var hotwire_livereload_received_default = (0, import_debounce.default)(({ force_reload }) => {
85
111
  const onErrorPage = document.title === "Action Controller: Exception caught";
86
112
  if (onErrorPage || force_reload) {
@@ -88,6 +114,8 @@
88
114
  document.location.reload();
89
115
  } else {
90
116
  console.log("[Hotwire::Livereload] Files changed. Reloading..");
117
+ hotwire_livereload_scroll_position_default.save();
118
+ Turbo.cache.clear();
91
119
  Turbo.visit(window.location.href, { action: "replace" });
92
120
  }
93
121
  }, 300);
@@ -453,7 +453,7 @@
453
453
  this.subscribe(subscription);
454
454
  return subscription;
455
455
  };
456
- Subscriptions2.prototype.remove = function remove(subscription) {
456
+ Subscriptions2.prototype.remove = function remove2(subscription) {
457
457
  this.forget(subscription);
458
458
  if (!this.findAll(subscription.identifier).length) {
459
459
  this.sendCommand(subscription, "unsubscribe");
@@ -607,7 +607,7 @@
607
607
  // node_modules/debounce/index.js
608
608
  var require_debounce = __commonJS({
609
609
  "node_modules/debounce/index.js"(exports, module) {
610
- function debounce3(func, wait, immediate) {
610
+ function debounce2(func, wait, immediate) {
611
611
  var timeout, args, context, timestamp, result;
612
612
  if (null == wait)
613
613
  wait = 100;
@@ -653,8 +653,8 @@
653
653
  };
654
654
  return debounced;
655
655
  }
656
- debounce3.debounce = debounce3;
657
- module.exports = debounce3;
656
+ debounce2.debounce = debounce2;
657
+ module.exports = debounce2;
658
658
  }
659
659
  });
660
660
 
@@ -663,41 +663,46 @@
663
663
 
664
664
  // app/javascript/lib/hotwire-livereload-received.js
665
665
  var import_debounce = __toESM(require_debounce());
666
- var hotwire_livereload_received_default = (0, import_debounce.default)(({ force_reload }) => {
667
- const onErrorPage = document.title === "Action Controller: Exception caught";
668
- if (onErrorPage || force_reload) {
669
- console.log("[Hotwire::Livereload] Files changed. Force reloading..");
670
- document.location.reload();
671
- } else {
672
- console.log("[Hotwire::Livereload] Files changed. Reloading..");
673
- Turbo.visit(window.location.href, { action: "replace" });
674
- }
675
- }, 300);
676
666
 
677
667
  // app/javascript/lib/hotwire-livereload-scroll-position.js
678
668
  var KEY = "hotwire-livereload-scrollPosition";
679
669
  function read() {
680
670
  const value = localStorage.getItem(KEY);
681
671
  if (!value)
682
- return 0;
672
+ return;
683
673
  return parseInt(value);
684
674
  }
685
675
  function save() {
686
676
  const pos = window.scrollY;
687
677
  localStorage.setItem(KEY, pos.toString());
688
678
  }
689
- function reset() {
690
- localStorage.setItem(KEY, "0");
679
+ function remove() {
680
+ localStorage.removeItem(KEY);
691
681
  }
692
682
  function restore() {
693
683
  const value = read();
694
- console.log("[Hotwire::Livereload] Restoring scroll position to", value);
695
- window.scrollTo(0, value);
684
+ if (value) {
685
+ console.log("[Hotwire::Livereload] Restoring scroll position to", value);
686
+ window.scrollTo(0, value);
687
+ }
696
688
  }
697
- var hotwire_livereload_scroll_position_default = { read, save, restore, reset };
689
+ var hotwire_livereload_scroll_position_default = { read, save, restore, remove };
690
+
691
+ // app/javascript/lib/hotwire-livereload-received.js
692
+ var hotwire_livereload_received_default = (0, import_debounce.default)(({ force_reload }) => {
693
+ const onErrorPage = document.title === "Action Controller: Exception caught";
694
+ if (onErrorPage || force_reload) {
695
+ console.log("[Hotwire::Livereload] Files changed. Force reloading..");
696
+ document.location.reload();
697
+ } else {
698
+ console.log("[Hotwire::Livereload] Files changed. Reloading..");
699
+ hotwire_livereload_scroll_position_default.save();
700
+ Turbo.cache.clear();
701
+ Turbo.visit(window.location.href, { action: "replace" });
702
+ }
703
+ }, 300);
698
704
 
699
705
  // app/javascript/hotwire-livereload.js
700
- var import_debounce2 = __toESM(require_debounce());
701
706
  var consumer = (0, import_actioncable.createConsumer)();
702
707
  consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
703
708
  received: hotwire_livereload_received_default,
@@ -708,19 +713,8 @@
708
713
  console.log("[Hotwire::Livereload] Websocket disconnected");
709
714
  }
710
715
  });
711
- var debouncedScroll = (0, import_debounce2.default)(() => {
712
- if (window.scrollY !== 0)
713
- return hotwire_livereload_scroll_position_default.save();
714
- setTimeout(() => {
715
- if (window.scrollY !== 0)
716
- return;
717
- hotwire_livereload_scroll_position_default.save();
718
- }, 1e3);
719
- }, 100);
720
- window.addEventListener("scroll", debouncedScroll);
721
- document.addEventListener("turbo:click", hotwire_livereload_scroll_position_default.reset);
722
- document.addEventListener("turbo:before-visit", hotwire_livereload_scroll_position_default.restore);
723
- document.addEventListener("turbo:load", hotwire_livereload_scroll_position_default.restore);
724
- document.addEventListener("DOMContentLoaded", hotwire_livereload_scroll_position_default.restore);
725
- document.addEventListener("turbo:frame-load", hotwire_livereload_scroll_position_default.restore);
716
+ document.addEventListener("turbo:load", () => {
717
+ hotwire_livereload_scroll_position_default.restore();
718
+ hotwire_livereload_scroll_position_default.remove();
719
+ });
726
720
  })();
@@ -1,7 +1,6 @@
1
1
  import { createConsumer } from "@rails/actioncable"
2
2
  import received from "./lib/hotwire-livereload-received"
3
3
  import scrollPosition from "./lib/hotwire-livereload-scroll-position"
4
- import debounce from "debounce"
5
4
 
6
5
  const consumer = createConsumer()
7
6
  consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
@@ -16,21 +15,8 @@ consumer.subscriptions.create("Hotwire::Livereload::ReloadChannel", {
16
15
  },
17
16
  })
18
17
 
19
- const debouncedScroll = debounce(() => {
20
- if (window.scrollY !== 0) return scrollPosition.save();
21
-
22
- // On a second update, the page mysteriously jumps to the top and sends a scroll event.
23
- // So we wait a bit and if the page is still is at the top, it was likely on purpose.
24
- setTimeout(() => {
25
- if (window.scrollY !== 0) return;
26
- scrollPosition.save();
27
- }, 1000);
28
- }, 100)
29
- window.addEventListener("scroll", debouncedScroll)
30
-
31
- document.addEventListener("turbo:click", scrollPosition.reset)
32
- document.addEventListener("turbo:before-visit", scrollPosition.restore)
33
- document.addEventListener("turbo:load", scrollPosition.restore)
34
- document.addEventListener("DOMContentLoaded", scrollPosition.restore)
35
- document.addEventListener("turbo:frame-load", scrollPosition.restore)
18
+ document.addEventListener("turbo:load", () => {
19
+ scrollPosition.restore()
20
+ scrollPosition.remove()
21
+ })
36
22
 
@@ -1,4 +1,5 @@
1
1
  import debounce from "debounce"
2
+ import scrollPosition from "./hotwire-livereload-scroll-position"
2
3
 
3
4
  export default debounce(({force_reload}) => {
4
5
  const onErrorPage = document.title === "Action Controller: Exception caught"
@@ -8,6 +9,8 @@ export default debounce(({force_reload}) => {
8
9
  document.location.reload()
9
10
  } else {
10
11
  console.log("[Hotwire::Livereload] Files changed. Reloading..")
12
+ scrollPosition.save()
13
+ Turbo.cache.clear()
11
14
  Turbo.visit(window.location.href, { action: 'replace' })
12
15
  }
13
16
  }, 300)
@@ -2,7 +2,7 @@ const KEY = "hotwire-livereload-scrollPosition"
2
2
 
3
3
  export function read() {
4
4
  const value = localStorage.getItem(KEY)
5
- if (!value) return 0;
5
+ if (!value) return
6
6
  return parseInt(value)
7
7
  }
8
8
 
@@ -11,14 +11,17 @@ export function save() {
11
11
  localStorage.setItem(KEY, pos.toString())
12
12
  }
13
13
 
14
- export function reset() {
15
- localStorage.setItem(KEY, "0");
14
+ export function remove() {
15
+ localStorage.removeItem(KEY)
16
16
  }
17
17
 
18
18
  export function restore() {
19
19
  const value = read()
20
- console.log("[Hotwire::Livereload] Restoring scroll position to", value)
21
- window.scrollTo(0, value)
20
+ if (value) {
21
+ console.log("[Hotwire::Livereload] Restoring scroll position to", value)
22
+ window.scrollTo(0, value)
23
+ }
24
+
22
25
  }
23
26
 
24
- export default { read, save, restore, reset }
27
+ export default { read, save, restore, remove }
@@ -15,6 +15,7 @@ module Hotwire
15
15
  #{root}/app/channels
16
16
  #{root}/app/helpers
17
17
  ]
18
+ config.hotwire_livereload.listen_options ||= {}
18
19
 
19
20
  initializer "hotwire_livereload.assets" do
20
21
  if Rails.application.config.respond_to?(:assets)
@@ -63,7 +64,7 @@ module Hotwire
63
64
  listen_paths = options.listen_paths.map(&:to_s).uniq
64
65
  force_reload_paths = options.force_reload_paths.map(&:to_s).uniq.join("|")
65
66
 
66
- @listener = Listen.to(*listen_paths) do |modified, added, removed|
67
+ @listener = Listen.to(*listen_paths, **config.hotwire_livereload.listen_options) do |modified, added, removed|
67
68
  unless File.exist?(DISABLE_FILE)
68
69
  changed = [modified, removed, added].flatten.uniq
69
70
  next unless changed.any?
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Livereload
3
- VERSION = "1.3.0"
3
+ VERSION = "1.3.2"
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.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Platonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-11 00:00:00.000000000 Z
11
+ date: 2024-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties