Skip to content

Commit

Permalink
Replace with helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemanrubia committed Dec 15, 2024
1 parent 13dd08d commit e0ee392
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
10 changes: 4 additions & 6 deletions app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ var HotwireSpark = (function () {
function nameFromFilePath(path) {
return path.split("/").pop().split(".")[0];
}
function withoutAssetDigest(path) {
return path.replace(/-[a-z0-9]+\.(\w+)$/, ".$1");
}
function urlWithParams(urlString, params) {
const url = new URL(urlString, window.location.origin);
Object.entries(params).forEach(_ref => {
Expand Down Expand Up @@ -3635,16 +3638,11 @@ var HotwireSpark = (function () {
});
}
#findExistingLinkFor(link) {
return this.#cssLinks.find(newLink => {
return this.#withoutAssetDigest(link.href) === this.#withoutAssetDigest(newLink.href);
});
return this.#cssLinks.find(newLink => withoutAssetDigest(link.href) === withoutAssetDigest(newLink.href));
}
get #cssLinks() {
return Array.from(document.querySelectorAll("link[rel='stylesheet']"));
}
#withoutAssetDigest(url) {
return url.replace(/-[^-]+\.css.*$/, ".css");
}
#appendNewLink(link) {
document.head.append(link);
return link;
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.

6 changes: 5 additions & 1 deletion app/javascript/hotwire_spark/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ export function nameFromFilePath(path) {
return path.split("/").pop().split(".")[0]
}

export function withoutAssetDigest(path) {
return path.replace(/-[a-z0-9]+\.(\w+)$/, ".$1");
}

export function urlWithParams(urlString, params) {
const url = new URL(urlString, window.location.origin)
Object.entries(params).forEach(([key, value]) => {
Object.entries(params).forEach(([ key, value ]) => {
url.searchParams.set(key, value)
})
return url.toString()
Expand Down
8 changes: 2 additions & 6 deletions app/javascript/hotwire_spark/reloaders/css_reloader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { log } from "../logger.js"
import { cacheBustedUrl, reloadHtmlDocument } from "../helpers.js"
import { cacheBustedUrl, reloadHtmlDocument, withoutAssetDigest } from "../helpers.js"

export class CssReloader {
static async reload(...params) {
Expand Down Expand Up @@ -53,17 +53,13 @@ export class CssReloader {
}

#findExistingLinkFor(link) {
return this.#cssLinks.find(newLink => this.#withoutAssetDigest(link.href) === this.#withoutAssetDigest(newLink.href))
return this.#cssLinks.find(newLink => withoutAssetDigest(link.href) === withoutAssetDigest(newLink.href))
}

get #cssLinks() {
return Array.from(document.querySelectorAll("link[rel='stylesheet']"))
}

#withoutAssetDigest(url) {
return url.replace(/-[^-]+\.css.*$/, ".css")
}

#appendNewLink(link) {
document.head.append(link)
return link
Expand Down

0 comments on commit e0ee392

Please sign in to comment.