From 12e074816fe14e24e0ecdfd673b2908e060713ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Wed, 13 Dec 2023 21:35:02 +0400 Subject: [PATCH] perf: avoid multiple script injection --- packages/cypress/src/support.ts | 17 +++++++++-------- packages/playwright/src/index.ts | 4 ++++ packages/puppeteer/src/index.ts | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/cypress/src/support.ts b/packages/cypress/src/support.ts index b2bf2353..a484b797 100644 --- a/packages/cypress/src/support.ts +++ b/packages/cypress/src/support.ts @@ -30,15 +30,16 @@ declare global { } function injectArgos() { - const fileName = - typeof require.resolve === "function" - ? require.resolve("@argos-ci/browser/global.js") - : "node_modules/@argos-ci/browser/dist/global.js"; - cy.readFile(fileName).then((source) => - cy.window({ log: false }).then((window) => { + cy.window({ log: false }).then((window) => { + if (typeof (window as any).__ARGOS__ !== "undefined") return; + const fileName = + typeof require.resolve === "function" + ? require.resolve("@argos-ci/browser/global.js") + : "node_modules/@argos-ci/browser/dist/global.js"; + return cy.readFile(fileName).then((source) => { window.eval(source); - }), - ); + }); + }); } function readArgosCypressVersion() { diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts index 22b5ffcc..cddcf11a 100644 --- a/packages/playwright/src/index.ts +++ b/packages/playwright/src/index.ts @@ -46,6 +46,10 @@ export type ArgosScreenshotOptions = { * Inject Argos script into the page. */ async function injectArgos(page: Page) { + const injected = await page.evaluate( + () => typeof (window as any).__ARGOS__ !== "undefined", + ); + if (injected) return; const fileName = require.resolve("@argos-ci/browser/global.js"); const content = await readFile(fileName, "utf-8"); await page.addScriptTag({ content }); diff --git a/packages/puppeteer/src/index.ts b/packages/puppeteer/src/index.ts index c7e690d5..c659d62a 100644 --- a/packages/puppeteer/src/index.ts +++ b/packages/puppeteer/src/index.ts @@ -17,6 +17,10 @@ const require = createRequire(import.meta.url); * Inject Argos script into the page. */ async function injectArgos(page: Page) { + const injected = await page.evaluate( + () => typeof (window as any).__ARGOS__ !== "undefined", + ); + if (injected) return; const fileName = require.resolve("@argos-ci/browser/global.js"); const content = await readFile(fileName, "utf-8"); await page.addScriptTag({ content });