Skip to content

Commit

Permalink
Merge pull request #23 from cloudflare/move-document-usage-into-effect
Browse files Browse the repository at this point in the history
Move reference to document into effect
  • Loading branch information
third774 authored Mar 22, 2021
2 parents a2400d8 + 4744aa4 commit 0a003d5
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import React, {
const scriptLocation =
"https://embed.videodelivery.net/embed/r4xu.fla9.latest.js";

let streamScript = document.querySelector<HTMLScriptElement>(
`script[src="${scriptLocation}"]`
);

function useStreamElement(
containerRef: RefObject<HTMLDivElement>,
streamRef: MutableRefObject<HTMLStreamElement | null>
Expand Down Expand Up @@ -53,8 +49,11 @@ declare global {
*/
function useStreamScript(ref: RefObject<HTMLStreamElement>) {
useEffect(() => {
if (streamScript === null) {
streamScript = document.createElement("script");
const existingScript = document.querySelector<HTMLScriptElement>(
`script[src="${scriptLocation}"]`
);
if (existingScript === null) {
const streamScript = document.createElement("script");
streamScript.setAttribute("data-cfasync", "false");
streamScript.setAttribute("defer", "true");
streamScript.setAttribute("type", "text/javascript");
Expand Down

0 comments on commit 0a003d5

Please sign in to comment.