Skip to content

Commit

Permalink
feat: loadScriptsOnMainThread string[] -> (string | RegExp)[] (#520)
Browse files Browse the repository at this point in the history
* loadScriptsOnMainThread string[] -> (string | RegExp)[]

* alter documentation

* regex-test-script.js test

* Update tests/integrations/load-scripts-on-main-thread/index.html

Co-authored-by: Giorgio Boa <35845425+gioboa@users.noreply.github.com>

* update documentation

---------

Co-authored-by: Giorgio Boa <35845425+gioboa@users.noreply.github.com>
  • Loading branch information
dymoo and gioboa authored Dec 26, 2023
1 parent 363bc6c commit 1ce9abc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", /regex-matched-script\.js/]`.|
| `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). |

Expand Down
2 changes: 1 addition & 1 deletion src/integration/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface PartytownConfig {
get?: GetHook;
globalFns?: string[];
lib?: string;
loadScriptsOnMainThread?: string[];
loadScriptsOnMainThread?: (string | RegExp)[];
logCalls?: boolean;
logGetters?: boolean;
logImageRequests?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ export interface PartytownConfig {
* This array can be used to filter which script are executed via
* Partytown and which you would like to execute on the main thread.
*
* @example loadScriptsOnMainThread:['https://test.com/analytics.js', 'inline-script-id']
* @example loadScriptsOnMainThread:['https://test.com/analytics.js', 'inline-script-id', /regex-matched-script\.js/]
* // Loads the `https://test.com/analytics.js` script on the main thread
*/
loadScriptsOnMainThread?: string[];
loadScriptsOnMainThread?: (string | RegExp)[];
get?: GetHook;
set?: SetHook;
apply?: ApplyHook;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/web-worker/worker-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 23 additions & 1 deletion tests/integrations/load-scripts-on-main-thread/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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/
],
};
</script>
Expand All @@ -29,6 +30,15 @@
document.head.appendChild(scriptElement);
})()
</script>
<script type='text/partytown'>
(() => {
const scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = "./regex-test-script.js";
scriptElement.id = "regexTestScript";
document.head.appendChild(scriptElement);
})()
</script>

<style>
body {
Expand Down Expand Up @@ -91,6 +101,18 @@ <h1>Load scripts on main thread 🎉</h1>
})()
</script>
</li>
<li>
<strong>Script Type:</strong>
<code id='regexTestScriptType'></code>
<script type='text/partytown'>
(() => {
const scriptElement = document.getElementById('regexTestScript');
const codeElement = document.getElementById('regexTestScriptType');

codeElement.innerText = scriptElement.type;
})()
</script>
</li>
<li>
<strong>Script Source:</strong>
<code id='testScriptSource'></code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ test('integration window accessor', async ({ page }) => {
const scriptElement = page.locator('#testScript');
await expect(scriptElement).toHaveAttribute('type', 'text/javascript')

const regexScriptElement = page.locator('#regexTestScript');
await expect(regexScriptElement).toHaveAttribute('type', 'text/javascript')

await page.waitForSelector('.testInlineScript');
const testInlineScript = page.locator('#testInlineScript');
await expect(testInlineScript).toHaveText('12');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
document.body.classList.add('completed');
})()

1 comment on commit 1ce9abc

@vercel
Copy link

@vercel vercel bot commented on 1ce9abc Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.