From 3062a9695e14830d3505d17c07f67a5c0189a201 Mon Sep 17 00:00:00 2001 From: Dylan Moore <60218243+dymoo@users.noreply.github.com> Date: Tue, 26 Dec 2023 16:18:40 +0000 Subject: [PATCH 1/5] loadScriptsOnMainThread string[] -> (string | RegExp)[] --- src/integration/api.md | 2 +- src/lib/types.ts | 2 +- src/lib/web-worker/worker-script.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/integration/api.md b/src/integration/api.md index e52099cd..4aed4fd5 100644 --- a/src/integration/api.md +++ b/src/integration/api.md @@ -24,7 +24,7 @@ export interface PartytownConfig { get?: GetHook; globalFns?: string[]; lib?: string; - loadScriptsOnMainThread?: string[]; + loadScriptsOnMainThread?: (string | RegExp)[]; logCalls?: boolean; logGetters?: boolean; logImageRequests?: boolean; diff --git a/src/lib/types.ts b/src/lib/types.ts index 97c6e946..13c0c60d 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -446,7 +446,7 @@ export interface PartytownConfig { * @example loadScriptsOnMainThread:['https://test.com/analytics.js', 'inline-script-id'] * // Loads the `https://test.com/analytics.js` script on the main thread */ - loadScriptsOnMainThread?: string[]; + loadScriptsOnMainThread?: (string | RegExp)[]; get?: GetHook; set?: SetHook; apply?: ApplyHook; diff --git a/src/lib/web-worker/worker-script.ts b/src/lib/web-worker/worker-script.ts index 6e1544ed..730f72dd 100644 --- a/src/lib/web-worker/worker-script.ts +++ b/src/lib/web-worker/worker-script.ts @@ -28,7 +28,7 @@ export const patchHTMLScriptElement = (WorkerHTMLScriptElement: any, env: WebWor if (this.type && config.loadScriptsOnMainThread) { const shouldExecuteScriptViaMainThread = config.loadScriptsOnMainThread.some( - (scriptUrl) => scriptUrl === url + (scriptUrl) => new RegExp(scriptUrl).test(url) ); if (shouldExecuteScriptViaMainThread) { From 4a09e08ff87fd1c2d092d37e3be1ce24a959a736 Mon Sep 17 00:00:00 2001 From: Dylan Moore <60218243+dymoo@users.noreply.github.com> Date: Tue, 26 Dec 2023 17:02:51 +0000 Subject: [PATCH 2/5] alter documentation --- docs/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index 3e8afe59..a83624aa 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -9,7 +9,7 @@ Partytown does not require a config for it to work, however a config can be set | `debug` | When `true`, Partytown scripts are not inlined and not minified. See the [Debugging](/debugging) docs on how to enable more logging. | | `forward` | An array of strings representing function calls on the main thread to forward to the web worker. See [Forwarding Events and Triggers](/forwarding-events) for more info. | | `lib` | Path where the Partytown library can be found your server. Note that the path must both start and end with a `/` character, and the files must be hosted from the same origin as the webpage. Default is `/~partytown/` | -| `loadScriptsOnMainThread` | An array of strings used to filter out which script are executed via Partytown and the main thread. An example is as follows: `loadScriptsOnMainThread: ["https://test.com/analytics.js", "inline-script-id"]`.| +| `loadScriptsOnMainThread` | An array of strings or regular expressions (RegExp) used to filter out which script are executed via Partytown and the main thread. An example is as follows: `loadScriptsOnMainThread: ["https://test.com/analytics.js", "inline-script-id"]`.| | `resolveUrl` | Hook that is called to resolve URLs which can be used to modify URLs. The hook uses the API: `resolveUrl(url: URL, location: URL, method: string)`. See the [Proxying Requests](/proxying-requests) for more information. | | `nonce` | The nonce property may be set on script elements created by Partytown. This should be set only when dealing with content security policies and when the use of `unsafe-inline` is disabled (using `nonce-*` instead). | From 97681d7b9601bdfc17046086312b5d6e24b185a6 Mon Sep 17 00:00:00 2001 From: Dylan Moore <60218243+dymoo@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:34:58 +0000 Subject: [PATCH 3/5] regex-test-script.js test --- .../load-scripts-on-main-thread/index.html | 24 ++++++++++++++++++- .../load-scripts-on-main-thread.spec.ts | 3 +++ .../regex-test-script.js | 3 +++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/integrations/load-scripts-on-main-thread/regex-test-script.js diff --git a/tests/integrations/load-scripts-on-main-thread/index.html b/tests/integrations/load-scripts-on-main-thread/index.html index eecbc698..28711d5f 100644 --- a/tests/integrations/load-scripts-on-main-thread/index.html +++ b/tests/integrations/load-scripts-on-main-thread/index.html @@ -15,7 +15,8 @@ logScriptExecution: true, loadScriptsOnMainThread: [ `http://${location.host}/tests/integrations/load-scripts-on-main-thread/test-script.js`, - `inline-test-script` + `inline-test-script`, + /regex-test-script\.js/ ], }; @@ -29,6 +30,15 @@ document.head.appendChild(scriptElement); })() +