diff --git a/.changeset/fix-trusted-script-url.md b/.changeset/fix-trusted-script-url.md new file mode 100644 index 00000000..f6fe5226 --- /dev/null +++ b/.changeset/fix-trusted-script-url.md @@ -0,0 +1,5 @@ +--- +'@qwik.dev/partytown': patch +--- + +fix: serialize TrustedScript and TrustedScriptURL values as strings for the main thread diff --git a/src/lib/sandbox/main-access-handler.ts b/src/lib/sandbox/main-access-handler.ts index 10fe25e7..496430c8 100644 --- a/src/lib/sandbox/main-access-handler.ts +++ b/src/lib/sandbox/main-access-handler.ts @@ -215,6 +215,8 @@ const applyToInstance = ( args[1] = trustedType('createHTML', args[1]); } else if (previous === 'write' || previous === 'writeln') { args = args.map((arg) => trustedType('createHTML', arg)); + } else if (previous === 'register') { + args[0] = trustedType('createScriptURL', args[0]); } } diff --git a/src/lib/web-worker/worker-serialization.ts b/src/lib/web-worker/worker-serialization.ts index 7d57e651..233fa226 100644 --- a/src/lib/web-worker/worker-serialization.ts +++ b/src/lib/web-worker/worker-serialization.ts @@ -66,7 +66,7 @@ export const serializeForMain = ( SerializedType.Event, serializeObjectForMain($winId$, $instanceId$, value, false, added), ]; - } else if (supportsTrustedHTML && value instanceof TrustedHTML) { + } else if (trustedTypes.some((T) => value instanceof T)) { // https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API return [SerializedType.Primitive, value.toString()]; } else if (value instanceof ArrayBuffer) { @@ -87,7 +87,9 @@ export const serializeForMain = ( } }; -const supportsTrustedHTML = typeof TrustedHTML === 'function'; +const trustedTypes = ['TrustedHTML', 'TrustedScript', 'TrustedScriptURL'] + .map((n) => (globalThis as any)[n]) + .filter((T) => typeof T === 'function'); const serializeObjectForMain = ( winId: WinId, @@ -371,4 +373,3 @@ const Attr = class { } }; -declare const TrustedHTML: any; diff --git a/tests/platform/trusted-types/index.html b/tests/platform/trusted-types/index.html index 2e34d288..9754c760 100644 --- a/tests/platform/trusted-types/index.html +++ b/tests/platform/trusted-types/index.html @@ -2,7 +2,7 @@
- +