Load css async

This commit is contained in:
tobspr 2020-09-28 12:24:52 +02:00
parent 0d52793c27
commit 507121b886
2 changed files with 247 additions and 228 deletions

View File

@ -54,19 +54,19 @@ function gulptasksHTML($, gulp, buildFolder) {
document.head.appendChild(css);
// Append async css
const asyncCss = document.createElement("link");
asyncCss.rel = "stylesheet";
asyncCss.type = "text/css";
asyncCss.media = "none";
asyncCss.setAttribute("onload", "this.media='all'");
asyncCss.href = cachebust("async-resources.css");
if (integrity) {
asyncCss.setAttribute(
"integrity",
computeIntegrityHash(path.join(buildFolder, "async-resources.css"))
);
}
document.head.appendChild(asyncCss);
// const asyncCss = document.createElement("link");
// asyncCss.rel = "stylesheet";
// asyncCss.type = "text/css";
// asyncCss.media = "none";
// asyncCss.setAttribute("onload", "this.media='all'");
// asyncCss.href = cachebust("async-resources.css");
// if (integrity) {
// asyncCss.setAttribute(
// "integrity",
// computeIntegrityHash(path.join(buildFolder, "async-resources.css"))
// );
// }
// document.head.appendChild(asyncCss);
if (app) {
// Append cordova link

View File

@ -8,6 +8,7 @@ import { Signal } from "./signal";
import { SOUNDS, MUSIC } from "../platform/sound";
import { AtlasDefinition, atlasFiles } from "./atlas_definitions";
import { initBuildingCodesAfterResourcesLoaded } from "../game/meta_building_registry";
import { cachebust } from "./cachebust";
const logger = createLogger("background_loader");
@ -110,6 +111,7 @@ export class BackgroundResourcesLoader {
essentialBareGameSounds,
essentialBareGameAtlases
)
.then(() => this.internalPreloadCss("async-resources.scss"))
.catch(err => {
logger.warn("⏰ Failed to load essentials for bare game:", err);
})
@ -136,6 +138,23 @@ export class BackgroundResourcesLoader {
});
}
internalPreloadCss(name) {
return new Promise((resolve, reject) => {
console.log("TODO");
const link = document.createElement("link");
link.onload = resolve;
link.onerror = reject;
link.setAttribute("rel", "stylesheet");
link.setAttribute("media", "all");
link.setAttribute("type", "text/css");
link.setAttribute("href", cachebust("async-resources.css"));
document.head.appendChild(link);
});
}
/**
* @param {Array<string>} sprites
* @param {Array<string>} sounds