Skip to content

Commit

Permalink
perf: avoid multiple script injection
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Dec 13, 2023
1 parent 23c4a3d commit 12e0748
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/cypress/src/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(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<string>(fileName).then((source) => {
window.eval(source);
}),
);
});
});
}

function readArgosCypressVersion() {
Expand Down
4 changes: 4 additions & 0 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 4 additions & 0 deletions packages/puppeteer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 12e0748

Please sign in to comment.