Skip to content

Commit

Permalink
Put codeToRunBeforeInit into begin of <head> tag to ensure it is ru…
Browse files Browse the repository at this point in the history
…n before any user code [Publish]
  • Loading branch information
andrey-skl committed Feb 16, 2024
1 parent 56581a5 commit e3e60de
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/websandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@ class Websandbox {
}

_prepareFrameContent(options: SandboxOptions) {
let frameContent = options.frameContent
?.replace('<head>', `<head>\n<script>${CompiledFrameScript}</script>`) ?? '';
let frameContent = options.frameContent ?? '';

if (options.codeToRunBeforeInit) {
frameContent = frameContent
.replace('</head>', `<script>${options.codeToRunBeforeInit}</script>\n</head>`);
}

frameContent = frameContent
.replace('<head>', `<head>\n<script>${CompiledFrameScript}</script>`) ?? '';

if (options.initialStyles) {
frameContent = frameContent
Expand All @@ -127,10 +134,6 @@ class Websandbox {
.replace('<head>', `<head>\n<base href="${options.baseUrl}"/>`);
}

if (options.codeToRunBeforeInit) {
frameContent = frameContent
.replace('</head>', `<script>${options.codeToRunBeforeInit}</script>\n</head>`);
}
return frameContent;
}

Expand Down

0 comments on commit e3e60de

Please sign in to comment.