Skip to content

Commit

Permalink
Fix error when no scripts in custom html
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-orlik committed Jul 4, 2024
1 parent b158a24 commit f67a3ff
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/postponeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ so, for a simple `console.log('hello')` script that needs scripts
*/




export function postponeScript(idsOfScriptsToAwait: string[], script_content: string) {
return `{const loaded = ${JSON.stringify(
Object.fromEntries(idsOfScriptsToAwait.map(id => [id, false]))
)};
export function postponeScript(
idsOfScriptsToAwait: string[],
script_content: string
) {
return `{const loaded = ${JSON.stringify(
Object.fromEntries(idsOfScriptsToAwait.map(id => [id, false]))
)};
let called = false;
const call_if_ready = () => {
Expand All @@ -76,11 +76,13 @@ export function postponeScript(idsOfScriptsToAwait: string[], script_content: st
}
};
${idsOfScriptsToAwait.map(
id => `document.addEventListener(
${idsOfScriptsToAwait
.map(
id => `document.addEventListener(
"loaded-${id}",
()=>{ loaded["${id}"] = true;
call_if_ready()
}).join(";")`
)}}`;
})`
)
.join(';')}}`
}

0 comments on commit f67a3ff

Please # to comment.