Skip to content

Commit

Permalink
Fix caching problem when loading CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemanrubia committed Dec 5, 2024
1 parent 16a9d02 commit 8360939
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
8 changes: 4 additions & 4 deletions app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,9 @@ var HotwireSpark = (function () {
});
}
async function reloadHtmlDocument() {
let currentUrl = urlWithParams(window.location.href, {
let currentUrl = cacheBustedUrl(urlWithParams(window.location.href, {
hotwire_spark: "true"
});
}));
const response = await fetch(currentUrl);
const fetchedHTML = await response.text();
const parser = new DOMParser();
Expand Down Expand Up @@ -3626,7 +3626,7 @@ var HotwireSpark = (function () {
return new Promise(resolve => {
const href = link.getAttribute("href");
const newLink = this.#findNewLinkFor(link);
newLink.href = cacheBustedUrl(newLink.href);
newLink.setAttribute("href", cacheBustedUrl(newLink.getAttribute("href")));
newLink.onload = () => {
log(`\t${href}`);
resolve();
Expand All @@ -3640,7 +3640,7 @@ var HotwireSpark = (function () {
});
}
#withoutAssetDigest(url) {
return url.replace(/-[^-]+\.css$/, ".css");
return url.replace(/-[^-]+\.css.*$/, ".css");
}
}

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.

2 changes: 1 addition & 1 deletion app/javascript/hotwire_spark/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function cacheBustedUrl(urlString) {
}

export async function reloadHtmlDocument() {
let currentUrl = urlWithParams(window.location.href, { hotwire_spark: "true" })
let currentUrl = cacheBustedUrl(urlWithParams(window.location.href, { hotwire_spark: "true" }))
const response = await fetch(currentUrl)
const fetchedHTML = await response.text()
const parser = new DOMParser()
Expand Down
5 changes: 2 additions & 3 deletions app/javascript/hotwire_spark/reloaders/css_reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export class CssReloader {
return new Promise(resolve => {
const href = link.getAttribute("href")
const newLink = this.#findNewLinkFor(link)

newLink.href = cacheBustedUrl(newLink.href)
newLink.setAttribute("href", cacheBustedUrl(newLink.getAttribute("href")))
newLink.onload = () => {
log(`\t${href}`)
resolve()
Expand All @@ -62,6 +61,6 @@ export class CssReloader {
}

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

0 comments on commit 8360939

Please sign in to comment.