@solid-primitives/script-loader
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -6,3 +6,3 @@ import { Accessor } from 'solid-js'; | ||
* | ||
* @param string URL of the script to load. | ||
* @param string URL or source of the script to load. | ||
* @param type Type value to put in the script attribute. | ||
@@ -9,0 +9,0 @@ * @param function Callback to trigger onLoad. |
@@ -5,11 +5,16 @@ // src/index.ts | ||
const script = document.createElement("script"); | ||
script.src = typeof opts.src === "function" ? opts.src() : opts.src; | ||
opts.type && (script.type = opts.type); | ||
opts.onload && script.addEventListener("load", opts.onload); | ||
opts.onerror && script.addEventListener("error", opts.onerror); | ||
document.head.appendChild(script); | ||
const remove = () => document.head.removeChild(script); | ||
createEffect(() => { | ||
script.src = typeof opts.src === "function" ? opts.src() : opts.src; | ||
}); | ||
const remove = () => document.head.contains(script) && document.head.removeChild(script); | ||
const load = () => { | ||
const src = typeof opts.src === "string" ? opts.src : opts.src(); | ||
const prop = /^(https?:|\w[\.\w-_%]+|)\//.test(src) ? "src" : "textContent"; | ||
if (script[prop] !== src) { | ||
script[prop] = src; | ||
document.head.appendChild(script); | ||
} | ||
}; | ||
load(); | ||
createEffect(load, false); | ||
onCleanup(remove); | ||
@@ -16,0 +21,0 @@ return [script, remove]; |
{ | ||
"name": "@solid-primitives/script-loader", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Primitive to load scripts dynamically", | ||
@@ -52,3 +52,3 @@ "author": "Alex Lohr <alex.lohr@logmein.com>", | ||
"prettier": "^2.0.5", | ||
"solid-register": "0.1.1", | ||
"solid-register": "0.1.5", | ||
"solid-testing-library": "^0.2.0", | ||
@@ -61,3 +61,3 @@ "tslib": "^2.3.1", | ||
"peerDependencies": { | ||
"solid-js": "1.3.0" | ||
"solid-js": "^1.3.1" | ||
}, | ||
@@ -70,2 +70,2 @@ "jest": { | ||
} | ||
} | ||
} |
@@ -22,3 +22,3 @@ # @solid-primitives/script-loader | ||
const [script: HTMLScriptElement, remove: () => void] = createScriptLoader({ | ||
url: string | Accessor<string>, | ||
src: string | Accessor<string>, // url or js source | ||
type?: string, | ||
@@ -31,3 +31,3 @@ onload?: () => void, | ||
createScriptLoader({ | ||
url: 'https://www.google.com/recaptcha/enterprise.js?render=my_token' | ||
src: 'https://www.google.com/recaptcha/enterprise.js?render=my_token' | ||
onload: async () => { | ||
@@ -39,2 +39,9 @@ await grecaptcha.enterprise.ready(); | ||
}); | ||
// or pinterest embeds: | ||
const pinterestEmbedScript = '!function(a,b,c){var d,e,f;d="PIN_"+~~((new Date).getTime()/864e5),...'; | ||
createScriptLoader({ | ||
src: pinterestEmbedScript, | ||
onload: () => window?.PinUtils?.build() | ||
}); | ||
``` | ||
@@ -63,2 +70,6 @@ | ||
1.0.4 | ||
Support JS source inclusion. | ||
</details> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8688
143
72