Skip to content

Commit

Permalink
Reload stimulus controllers from fresh document to avoid caching issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemanrubia committed Dec 4, 2024
1 parent c9dbe1e commit 61c7f3c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
13 changes: 5 additions & 8 deletions app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2996,14 +2996,11 @@
}

class StimulusReloader {
static async reload() {
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
params[_key] = arguments[_key];
}
return new StimulusReloader(...params).reload();
static async reload(filePattern) {
const document = await reloadHtmlDocument();
return new StimulusReloader(document, filePattern).reload();
}
constructor() {
let document = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.document;
constructor(document) {
let filePattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : /./;
this.document = document;
this.filePattern = filePattern;
Expand Down Expand Up @@ -3161,7 +3158,7 @@
CssReloader.reload(new RegExp(path));
}
#reloadStimulus(path) {
StimulusReloader.reload(window.document, new RegExp(path));
StimulusReloader.reload(new RegExp(path));
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions app/javascript/hotwire_spark/reloaders/stimulus_reloader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Application } from "@hotwired/stimulus"
import { log } from "../logger.js"
import { cacheBustedUrl } from "../helpers.js"
import { cacheBustedUrl, reloadHtmlDocument } from "../helpers.js"

export class StimulusReloader {
static async reload(...params) {
return new StimulusReloader(...params).reload()
static async reload(filePattern) {
const document = await reloadHtmlDocument()
return new StimulusReloader(document, filePattern).reload()
}

constructor(document = window.document, filePattern = /./) {
constructor(document, filePattern = /./) {
this.document = document
this.filePattern = filePattern
this.application = window.Stimulus || Application.start()
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/hotwire_spark/reloading_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export class ReloadingMonitor {
}

#reloadStimulus(path) {
StimulusReloader.reload(window.document, new RegExp(path))
StimulusReloader.reload(new RegExp(path))
}
}

0 comments on commit 61c7f3c

Please sign in to comment.