Skip to content

Commit

Permalink
fix: Avoids converting a gif into a function (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
leocwolter committed Sep 23, 2022
1 parent e27ea76 commit b76a50a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/lib/web-worker/worker-exec.ts
@@ -1,5 +1,5 @@
import { debug } from '../utils';
import { environments, partytownLibUrl, webWorkerCtx } from './worker-constants';
import { VERSION } from '../build-modules/version';
import { logWorker } from '../log';
import {
EventHandler,
InitializeScriptData,
Expand All @@ -10,12 +10,12 @@ import {
WebWorkerEnvironment,
WinId,
WorkerInstance,
WorkerMessageType,
WorkerMessageType
} from '../types';
import { getInstanceStateValue, setInstanceStateValue } from './worker-state';
import { debug } from '../utils';
import { environments, partytownLibUrl, webWorkerCtx } from './worker-constants';
import { getOrCreateNodeInstance } from './worker-constructors';
import { logWorker } from '../log';
import { VERSION } from '../build-modules/version';
import { getInstanceStateValue, setInstanceStateValue } from './worker-state';

export const initNextScriptsInWebWorker = async (initScript: InitializeScriptData) => {
let winId = initScript.$winId$;
Expand All @@ -27,6 +27,14 @@ export const initNextScriptsInWebWorker = async (initScript: InitializeScriptDat
let errorMsg = '';
let env = environments[winId];
let rsp: Response;
let javascriptContentTypes = ["text/jscript",
"text/javascript",
"text/x-javascript",
"application/javascript",
"application/x-javascript",
"text/ecmascript",
"text/x-ecmascript",
"application/ecmascript"]

if (scriptSrc) {
try {
Expand All @@ -40,10 +48,13 @@ export const initNextScriptsInWebWorker = async (initScript: InitializeScriptDat

rsp = await fetch(scriptSrc);
if (rsp.ok) {
scriptContent = await rsp.text();

env.$currentScriptId$ = instanceId;
run(env, scriptContent, scriptOrgSrc || scriptSrc);
let responseContentType = rsp.headers.get("content-type");
let shouldExecute = javascriptContentTypes.some(ct => responseContentType?.toLowerCase?.().includes?.(ct));
if (shouldExecute){
scriptContent = await rsp.text();
env.$currentScriptId$ = instanceId;
run(env, scriptContent, scriptOrgSrc || scriptSrc);
}
runStateLoadHandlers(instance!, StateProp.loadHandlers);
} else {
errorMsg = rsp.statusText;
Expand Down
1 change: 1 addition & 0 deletions tests/index.html
Expand Up @@ -120,6 +120,7 @@ <h2>Service Integration Tests</h2>
<li><a href="/tests/integrations/load-scripts-on-main-thread/">Load Scripts on Main Thread</a></li>
<li><a href="/tests/integrations/facebook-pixel/">Facebook Pixel</a></li>
<li><a href="/tests/integrations/gtm/">Google Tag Manager (GTM)</a></li>
<li><a href="/tests/integrations/javascript-request/">Javascript Request Execution</a></li>
<li><a href="/tests/integrations/hubspot/forms.html">Hubspot Forms</a></li>
<li><a href="/tests/integrations/intercom/">Intercom</a></li>
<li><a href="/tests/integrations/jquery/">jQuery</a></li>
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/javascript-request/execute.js
@@ -0,0 +1,4 @@
(function () {
const element = document.querySelector("#javascript-execution")
element.textContent = "Executed the js"
})()
60 changes: 60 additions & 0 deletions tests/integrations/javascript-request/index.html
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Partytown Test Page" />

<title>Javascript request execution 🎉</title>

<script>
partytown = {
logCalls: true,
logGetters: true,
logSetters: true,
logImageRequests: true,
logMainAccess: true,
logSendBeaconRequests: true,
logStackTraces: false,
logScriptExecution: true,
}
</script>
<script src="/~partytown/debug/partytown.js"></script>
</head>
<body>
<h1>Javascript request execution</h1>

<p>
<strong>Javascript ("create-element.js") execution</strong>
<span id="javascript-execution"></span>
</p>

<p>
<strong>Text ("some-text.txt") execution</strong>
<span id="text-execution"></span>
</p>

<!-- Some third party js -->
<script type="text/partytown">
(async function(){
const jsRequestElement = document.createElement("script")
jsRequestElement.setAttribute("src", "/tests/integrations/javascript-request/execute.js")
document.head.appendChild(jsRequestElement)

const textRequestElement = document.createElement("script")
textRequestElement.setAttribute("src", "/tests/integrations/javascript-request/some-text.txt")
document.head.appendChild(textRequestElement)

})()
</script>
<!-- End Some third party js -->

<script type="text/partytown">
(function () {
document.body.classList.add('completed')
})()
</script>

<p><a href="/tests/">All Tests</a></p>
</body>
</html>
19 changes: 19 additions & 0 deletions tests/integrations/javascript-request/javascript-request.spec.ts
@@ -0,0 +1,19 @@
import { expect, test } from '@playwright/test';

test('javascript request execution', async ({ page }) => {
await page.goto('/tests/integrations/javascript-request/');

await page.waitForSelector('.completed');

const javascriptExecutionElement = page.locator('#javascript-execution');
await expect(javascriptExecutionElement).toHaveText('Executed the js');
});

test('text request execution', async ({ page }) => {
await page.goto('/tests/integrations/javascript-request/');

await page.waitForSelector('.completed');

const textExecutionElement = page.locator('#text-execution');
await expect(textExecutionElement).toBeEmpty();
});
4 changes: 4 additions & 0 deletions tests/integrations/javascript-request/some-text.txt
@@ -0,0 +1,4 @@
(function () {
const element = document.querySelector("#text-execution")
element.textContent = "Executed the text"
})()

1 comment on commit b76a50a

@vercel
Copy link

@vercel vercel bot commented on b76a50a Sep 23, 2022

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.