Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to specify a scope for text/partytown scripts #624

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/lib/main/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function snippet(
doc.querySelector(config!.sandboxParent || 'body')!.appendChild(sandbox);
}

function fallback(i?: number, script?: HTMLScriptElement) {
function fallback() {
gioboa marked this conversation as resolved.
Show resolved Hide resolved
// no support or timeout reached
// basically "undo" all of the text/partytown scripts
// so they act as normal scripts
Expand All @@ -114,16 +114,19 @@ export function snippet(
});
}

for (i = 0; i < scripts!.length; i++) {
script = doc.createElement('script');
script.innerHTML = scripts![i].innerHTML;
// We don't need to set a `nonce` on sandbox script since it is loaded via
// the `src` attribute. However, we do need to set a `nonce` on the current
// script because it contains an inline script. This action ensures that the
// script can still be executed even when inline scripts are blocked
// (assuming `unsafe-inline` is disabled and `nonce-*` is used instead).
script.nonce = config!.nonce;
doc.head.appendChild(script);
for (let i = 0; i < scripts!.length; i++) {
const script = scripts![i];
if (script.getAttribute('ptScope') !== 'worker') {
const fallbackScript = doc.createElement('script');
fallbackScript.innerHTML = script.innerHTML;
// We don't need to set a `nonce` on sandbox script since it is loaded via
// the `src` attribute. However, we do need to set a `nonce` on the current
// script because it contains an inline script. This action ensures that the
// script can still be executed even when inline scripts are blocked
// (assuming `unsafe-inline` is disabled and `nonce-*` is used instead).
fallbackScript.nonce = config!.nonce;
doc.head.appendChild(fallbackScript);
}
}

if (sandbox) {
Expand Down