Skip to content

Commit

Permalink
Move getConfigurationProperty to config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
codergeek121 committed Jan 3, 2025
1 parent 5620b01 commit 00e840e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 46 deletions.
74 changes: 37 additions & 37 deletions app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
var HotwireSpark = (function () {
'use strict';

function assetNameFromPath(path) {
return path.split("/").pop().split(".")[0];
}
function pathWithoutAssetDigest(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 => {
let [key, value] = _ref;
url.searchParams.set(key, value);
});
return url.toString();
}
function cacheBustedUrl(urlString) {
return urlWithParams(urlString, {
reload: Date.now()
});
}
async function reloadHtmlDocument() {
let currentUrl = cacheBustedUrl(urlWithParams(window.location.href, {
hotwire_spark: "true"
}));
const response = await fetch(currentUrl, {
headers: {
"Accept": "text/html"
}
});
if (!response.ok) {
throw new Error(`${response.status} when fetching ${currentUrl}`);
}
const fetchedHTML = await response.text();
const parser = new DOMParser();
return parser.parseFromString(fetchedHTML, "text/html");
}
function getConfigurationProperty(name) {
return document.querySelector(`meta[name="hotwire-spark:${name}"]`)?.content;
}

var config = {
loggingEnabled: getConfigurationProperty("logging") ?? false,
htmlReloadMethod: getConfigurationProperty("html-reload-method"),
Expand Down Expand Up @@ -559,6 +523,42 @@ var HotwireSpark = (function () {

var consumer = createConsumer(config.cableServerPath);

function assetNameFromPath(path) {
return path.split("/").pop().split(".")[0];
}
function pathWithoutAssetDigest(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 => {
let [key, value] = _ref;
url.searchParams.set(key, value);
});
return url.toString();
}
function cacheBustedUrl(urlString) {
return urlWithParams(urlString, {
reload: Date.now()
});
}
async function reloadHtmlDocument() {
let currentUrl = cacheBustedUrl(urlWithParams(window.location.href, {
hotwire_spark: "true"
}));
const response = await fetch(currentUrl, {
headers: {
"Accept": "text/html"
}
});
if (!response.ok) {
throw new Error(`${response.status} when fetching ${currentUrl}`);
}
const fetchedHTML = await response.text();
const parser = new DOMParser();
return parser.parseFromString(fetchedHTML, "text/html");
}

// base IIFE to define idiomorph
var Idiomorph = (function () {

Expand Down Expand Up @@ -1636,7 +1636,7 @@ var HotwireSpark = (function () {
});

const HotwireSpark$1 = {
config: config
config
};

return HotwireSpark$1;
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.

4 changes: 3 additions & 1 deletion app/javascript/hotwire/spark/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getConfigurationProperty } from "./helpers.js";
function getConfigurationProperty(name) {
return document.querySelector(`meta[name="hotwire-spark:${name}"]`)?.content
}

export default {
loggingEnabled: getConfigurationProperty("logging") ?? false,
Expand Down
5 changes: 0 additions & 5 deletions app/javascript/hotwire/spark/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,3 @@ export async function reloadHtmlDocument() {
const parser = new DOMParser()
return parser.parseFromString(fetchedHTML, "text/html")
}

export function getConfigurationProperty(name) {
return document.querySelector(`meta[name="hotwire-spark:${name}"]`)?.content
}

2 changes: 1 addition & 1 deletion app/javascript/hotwire/spark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import config from "./config.js"
import "./channels/monitoring_channel.js"

const HotwireSpark = {
config: config,
config
}

export default HotwireSpark
1 change: 1 addition & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ class Application < Rails::Application
# config.eager_load_paths << Rails.root.join("extras")

config.asset_host = ->(asset, request) { "#{request.host}:#{request.port}" }
config.hotwire.spark.cable_server_path = "/test/hotwire-spark"
end
end

0 comments on commit 00e840e

Please sign in to comment.