Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7862269
refresh-styles.stimulus.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
refresh-styles.stimulus.ts
View Options
import
{
Controller
}
from
"stimulus"
;
function
make_new_link
()
{
const
new_link
=
document
.
createElement
(
"link"
);
new_link
.
rel
=
"stylesheet"
;
new_link
.
href
=
`/dist/main.css?
${
Math
.
random
()
}
+
${
Math
.
random
()
}
`
;
new_link
.
type
=
"text/css"
;
return
new_link
;
}
function
getStyles
()
{
return
Array
.
from
(
document
.
querySelectorAll
(
"head link"
)).
filter
(
(
e
:
HTMLLinkElement
)
=>
new
URL
(
e
.
href
).
pathname
==
"/dist/main.css"
);
}
function
cleanup_css
()
{
getStyles
()
.
slice
(
0
,
-
1
)
.
forEach
((
style
)
=>
{
style
.
parentElement
.
removeChild
(
style
);
});
}
export
default
class
RefreshStyles
extends
Controller
{
socket
:
WebSocket
;
async
connect
()
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const
{
port
}
=
await
fetch
(
"/dist/notifier.json"
).
then
((
r
)
=>
r
.
json
());
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
this
.
socket
=
new
WebSocket
(
`ws://localhost:
${
port
as
number
}
`
);
this
.
socket
.
onmessage
=
async
(
message
)
=>
{
if
(
message
.
data
===
"css"
)
{
const
new_link
=
make_new_link
();
new_link
.
onload
=
cleanup_css
;
document
.
querySelector
(
"head"
).
appendChild
(
new_link
);
}
};
document
.
documentElement
.
addEventListener
(
"turbo:morph"
,
cleanup_css
);
}
async
disconnect
()
{
this
.
socket
.
close
();
document
.
documentElement
.
removeEventListener
(
"turbo:morph"
,
cleanup_css
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, Aug 13, 20:22 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
870859
Default Alt Text
refresh-styles.stimulus.ts (1 KB)
Attached To
Mode
rPHOTOAPP photo-app-demo
Attached
Detach File
Event Timeline
Log In to Comment