From aa14cbd9c7883ee8c9d8760fcbbce1d334c696c4 Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Thu, 2 Dec 2021 16:33:17 -1000 Subject: [PATCH] fix: inject Prismic Toolbar only once --- src/PrismicToolbar.tsx | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/PrismicToolbar.tsx b/src/PrismicToolbar.tsx index 4ae8ffe..8a14ff7 100644 --- a/src/PrismicToolbar.tsx +++ b/src/PrismicToolbar.tsx @@ -26,19 +26,25 @@ export const PrismicToolbar = ({ repositoryName, type = "new", }: PrismicToolbarProps): null => { + const src = `https://static.cdn.prismic.io/prismic.js?repo=${repositoryName}${ + type === "new" ? "&type=new" : "" + }`; + React.useEffect(() => { - const script = document.createElement("script"); - script.src = `https://static.cdn.prismic.io/prismic.js?repositoryName=${repositoryName}${ - type === "new" ? "&type=new" : "" - }`; - script.defer = true; - - // Used to distinguish the toolbar element from other elements. - script.dataset.prismicToolbar = ""; - script.dataset.repositoryName = repositoryName; - script.dataset.type = type; - - document.body.appendChild(script); + const existingScript = document.querySelector(`script[src="${src}"]`); + + if (!existingScript) { + const script = document.createElement("script"); + script.src = src; + script.defer = true; + + // Used to distinguish the toolbar element from other elements. + script.dataset.prismicToolbar = ""; + script.dataset.repositoryName = repositoryName; + script.dataset.type = type; + + document.body.appendChild(script); + } }, [repositoryName, type]); return null;