-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: consolidate functionality used for adding third party scripts
- Loading branch information
1 parent
ef1b759
commit 8e91f79
Showing
5 changed files
with
42 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,8 @@ | ||
type TAssetType = { | ||
src: string | ||
type: "script" | "link" | ||
} | ||
import { TAssetType } from "@/lib/helpers/helper.scripts" | ||
|
||
export const assets: TAssetType[] = [ | ||
{ | ||
src: "https://play.pubhub.dk/1.3.0/js/player-kernel.min.js", | ||
type: "script", | ||
}, | ||
] | ||
|
||
export const appendAsset = ({ src, type }: TAssetType) => { | ||
if (type === "script") { | ||
const scriptElement = document.createElement("script") | ||
scriptElement.src = src | ||
scriptElement.defer = true | ||
scriptElement.async = false | ||
scriptElement.type = "module" | ||
document.head.appendChild(scriptElement) | ||
} | ||
} | ||
|
||
export const removeAsset = ({ src, type }: TAssetType) => { | ||
if (type === "script") { | ||
const scriptElement = document.querySelector(`script[src="${src}"]`) | ||
scriptElement?.remove() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export type TAssetType = { | ||
src: string | ||
type: "script" | "link" | ||
} | ||
|
||
export const appendAsset = ({ src, type }: TAssetType) => { | ||
if (type === "script") { | ||
const scriptElement = document.createElement("script") | ||
scriptElement.src = src | ||
scriptElement.defer = true | ||
scriptElement.async = false | ||
scriptElement.type = "module" | ||
document.head.appendChild(scriptElement) | ||
} | ||
|
||
if (type === "link") { | ||
const linkElement = document.createElement("link") | ||
linkElement.href = src | ||
linkElement.rel = "stylesheet" | ||
document.head.appendChild(linkElement) | ||
} | ||
} | ||
|
||
export const removeAsset = ({ src, type }: TAssetType) => { | ||
if (type === "script") { | ||
const scriptElement = document.querySelector(`script[src="${src}"]`) | ||
scriptElement?.remove() | ||
} | ||
|
||
if (type === "link") { | ||
const linkElement = document.querySelector(`link[href="${src}"]`) | ||
linkElement?.remove() | ||
} | ||
} |