Skip to content

Commit e903746

Browse files
committed
hopefully fix issue with stylesheet stuff crashing page behavior
1 parent ebfba57 commit e903746

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

assets/js/app.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import "./styles.js"
2-
import "phoenix_html"
3-
import { Socket, LongPoll } from "phoenix"
4-
import topbar from "topbar"
5-
import Hooks from "./_hooks"
6-
import { LiveSocket } from "phoenix_live_view"
7-
import { replaceIcons } from "./_hooks/BanchanWeb.Components.Icon.hooks"
8-
9-
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
10-
const orderSeed = document.querySelector("[data-order-seed]")?.dataset.orderSeed
1+
import "phoenix_html";
2+
import { Socket, LongPoll } from "phoenix";
3+
import topbar from "topbar";
4+
import Hooks from "./_hooks";
5+
import { LiveSocket } from "phoenix_live_view";
6+
import { replaceIcons } from "./_hooks/BanchanWeb.Components.Icon.hooks";
7+
8+
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
9+
const orderSeed = document.querySelector("[data-order-seed]")?.dataset.orderSeed;
1110
const liveSocket = new LiveSocket('/live', Socket, {
1211
params: {
1312
_csrf_token: csrfToken,
1413
order_seed: orderSeed == null ? null : parseInt(orderSeed)
1514
},
1615
hooks: Hooks
17-
})
16+
});
1817

1918
// liveSocket.socket.onError((_error, transport, establishedConnections) => {
2019
// liveSocket.socket.connect();
@@ -30,7 +29,7 @@ const liveSocket = new LiveSocket('/live', Socket, {
3029
// things feel faster.
3130
//
3231
// See: https://fly.io/phoenix-files/make-your-liveview-feel-faster/#solution
33-
topbar.config({ barColors: { 0: "#29d" }, shadowColor: "rgba(0, 0, 0, .3)" })
32+
topbar.config({ barColors: { 0: "#29d" }, shadowColor: "rgba(0, 0, 0, .3)" });
3433

3534
let topBarScheduled = undefined;
3635
window.addEventListener("phx:page-loading-start", () => {
@@ -45,13 +44,15 @@ window.addEventListener("phx:page-loading-stop", () => {
4544
});
4645

4746
// connect if there are any LiveViews on the page
48-
liveSocket.connect()
49-
50-
replaceIcons();
47+
liveSocket.connect();
5148

5249
// expose liveSocket on window for web console debug logs and latency simulation:
5350
// >> liveSocket.enableDebug()
5451
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
5552
// >> liveSocket.disableLatencySim()
56-
window.liveSocket = liveSocket
53+
window.liveSocket = liveSocket;
54+
55+
replaceIcons();
5756

57+
// We do this later because problems with this shouldn't crash the entire page.
58+
import('./styles.js').then(() => { }, console.error);

assets/js/styles.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ let styleText = "";
22

33
for (let i = 0; i < document.styleSheets.length; i++) {
44
const styleSheet = document.styleSheets[i];
5+
if (new URL(styleSheet.href).hostname !== window.location.hostname) {
6+
// We can't access cross-origin stylesheets, which are often injected by
7+
// extensions.
8+
continue;
9+
}
510
for (let j = 0; j < styleSheet.cssRules.length; j++) {
611
const cssRule = styleSheet.cssRules[j];
712
styleText += "\n"

0 commit comments

Comments
 (0)