From 3824b6d0484109d6f4726b14bed1b64ff82c35a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Mon, 22 Jul 2024 15:39:32 +0300 Subject: [PATCH 1/2] Improve nosto reload guard --- spec/nosto.reload.spec.tsx | 17 +++++++++++++++++ src/components/NostoProvider.tsx | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 spec/nosto.reload.spec.tsx diff --git a/spec/nosto.reload.spec.tsx b/spec/nosto.reload.spec.tsx new file mode 100644 index 0000000..807a168 --- /dev/null +++ b/spec/nosto.reload.spec.tsx @@ -0,0 +1,17 @@ +import React from "react" +import { render } from "@testing-library/react" +import { NostoProvider, NostoHome } from "../src/index" +import "@testing-library/jest-dom" + +test("verify Nosto is not loaded twice", async () => { + // @ts-expect-error dummy placeholder for Nosto iframe window scope + window.nosto = {} + + render( + + + + ) + + expect(document.querySelector("[nosto-client-script]")).not.toBeInTheDocument() +}) diff --git a/src/components/NostoProvider.tsx b/src/components/NostoProvider.tsx index c7dc762..325b4f1 100644 --- a/src/components/NostoProvider.tsx +++ b/src/components/NostoProvider.tsx @@ -86,7 +86,7 @@ export default function NostoProvider(props: NostoProviderProps) { window.nostojs(api => api.setAutoLoad(false)) } - if (!document.querySelectorAll("[nosto-client-script]").length && !shopifyMarkets) { + if (!window.nosto && !shopifyMarkets) { const script = document.createElement("script") script.type = "text/javascript" script.src = "//" + (host || "connect.nosto.com") + "/include/" + account From 72028c798c727ca2c4fc9fb1eeb38073f8856df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Mon, 22 Jul 2024 16:14:27 +0300 Subject: [PATCH 2/2] Review feedback --- src/components/NostoProvider.tsx | 3 ++- src/components/helpers.ts | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 src/components/helpers.ts diff --git a/src/components/NostoProvider.tsx b/src/components/NostoProvider.tsx index 325b4f1..e9a2347 100644 --- a/src/components/NostoProvider.tsx +++ b/src/components/NostoProvider.tsx @@ -1,6 +1,7 @@ import React, { useEffect, isValidElement } from "react" import { NostoContext, RecommendationComponent } from "../context" import { NostoClient } from "../types" +import { isNostoLoaded } from "./helpers" /** * @group Components @@ -86,7 +87,7 @@ export default function NostoProvider(props: NostoProviderProps) { window.nostojs(api => api.setAutoLoad(false)) } - if (!window.nosto && !shopifyMarkets) { + if (!isNostoLoaded() && !shopifyMarkets) { const script = document.createElement("script") script.type = "text/javascript" script.src = "//" + (host || "connect.nosto.com") + "/include/" + account diff --git a/src/components/helpers.ts b/src/components/helpers.ts new file mode 100644 index 0000000..f3924ff --- /dev/null +++ b/src/components/helpers.ts @@ -0,0 +1,3 @@ +export function isNostoLoaded() { + return typeof window.nosto !== "undefined" +} \ No newline at end of file