mirror of
https://github.com/discourse/discourse.git
synced 2025-02-15 16:42:46 +08:00
![David Taylor](/assets/img/avatar_default.png)
This lets us use all our normal JS tooling like prettier, esline and babel on the splash screen JS. At runtime the JS file is read and inlined into the HTML. This commit also switches us to use a CSP hash rather than a nonce for the splash screen.
67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
// This script is inlined in `_discourse_splash.html.erb
|
|
const DELAY_TARGET = 2000;
|
|
const POLLING_INTERVAL = 50;
|
|
|
|
const splashSvgTemplate = document.querySelector(".splash-svg-template");
|
|
const splashTemplateClone = splashSvgTemplate.content.cloneNode(true);
|
|
const svgElement = splashTemplateClone.querySelector("svg");
|
|
|
|
const svgString = new XMLSerializer().serializeToString(svgElement);
|
|
const encodedSvg = btoa(svgString);
|
|
|
|
const splashWrapper = document.querySelector("#d-splash");
|
|
const splashImage =
|
|
splashWrapper && splashWrapper.querySelector(".preloader-image");
|
|
|
|
if (splashImage) {
|
|
splashImage.src = `data:image/svg+xml;base64,${encodedSvg}`;
|
|
|
|
const connectStart = performance.timing.connectStart || 0;
|
|
const targetTime = connectStart + DELAY_TARGET;
|
|
|
|
let splashInterval;
|
|
let discourseReady;
|
|
|
|
const swapSplash = () => {
|
|
splashWrapper &&
|
|
splashWrapper.style.setProperty("--animation-state", "running");
|
|
svgElement && svgElement.style.setProperty("--animation-state", "running");
|
|
|
|
const newSvgString = new XMLSerializer().serializeToString(svgElement);
|
|
const newEncodedSvg = btoa(newSvgString);
|
|
|
|
splashImage.src = `data:image/svg+xml;base64,${newEncodedSvg}`;
|
|
|
|
performance.mark("discourse-splash-visible");
|
|
|
|
clearSplashInterval();
|
|
};
|
|
|
|
const clearSplashInterval = () => {
|
|
clearInterval(splashInterval);
|
|
splashInterval = null;
|
|
};
|
|
|
|
(() => {
|
|
splashInterval = setInterval(() => {
|
|
if (discourseReady) {
|
|
clearSplashInterval();
|
|
}
|
|
|
|
if (Date.now() > targetTime) {
|
|
swapSplash();
|
|
}
|
|
}, POLLING_INTERVAL);
|
|
})();
|
|
|
|
document.addEventListener(
|
|
"discourse-ready",
|
|
() => {
|
|
discourseReady = true;
|
|
splashWrapper && splashWrapper.remove();
|
|
performance.mark("discourse-splash-removed");
|
|
},
|
|
{ once: true }
|
|
);
|
|
}
|