|
6 | 6 |
|
7 | 7 | > Lightweight utility for dynamically loading external scripts into the browser — framework-agnostic, caching-safe, and CSP-friendly. |
8 | 8 |
|
| 9 | +## ✅ Features |
| 10 | + |
| 11 | +- 📆 Small and framework-agnostic |
| 12 | +- 📑 Fully typed with TypeScript for autocompletion and safety |
| 13 | +- 🚫 Prevents duplicate script injection via internal cache |
| 14 | + |
9 | 15 | ## 📦 Installation |
10 | 16 |
|
11 | 17 | ```bash |
@@ -40,44 +46,12 @@ Loads an external script dynamically and returns a `Promise<HTMLScriptElement>`. |
40 | 46 | | `options` | `LoadScriptOptions` | `loadScript` options (e.g. `async`, `type`) | |
41 | 47 | | `container` | `HTMLElement` | HTML element to append `<script />` to (default: `document.head`) | |
42 | 48 |
|
43 | | -## ✅ Features |
44 | | - |
45 | | -- 📑 Fully typed with TypeScript for autocompletion and safety |
46 | | -- 🚫 Prevents duplicate script injection via internal cache |
47 | | -- ⚙️ Supports `data-*`, `nonce`, `fetchPriority`, `crossOrigin`, and other modern attributes |
48 | | -- 📆 Small and framework-agnostic |
49 | | -- 🔒 CSP-friendly via `nonce` support |
50 | | - |
51 | | ---- |
52 | | - |
53 | | -## 🧪 Example: With SolidJS |
54 | | - |
55 | | -```ts |
56 | | -import { onMount } from "solid-js"; |
57 | | -import { loadScript } from "@dschz/load-script"; |
58 | | - |
59 | | -const WidgetLoader = () => { |
60 | | - let containerRef!: HTMLElement; |
61 | | - |
62 | | - onMount(async () => { |
63 | | - await loadScript("https://example.com/widget.js", { |
64 | | - type: "text/javascript", |
65 | | - async: true, |
66 | | - }, containerRef); |
67 | | - }); |
68 | | - |
69 | | - return <div ref={containerRef} />; |
70 | | -}; |
71 | | -``` |
72 | | - |
73 | 49 | ## 📝 Notes |
74 | 50 |
|
75 | | -- Scripts are cached by `src` unless `innerHTML` or `textContent` is used |
| 51 | +- Scripts are cached by `src`. If `innerHTML` or `textContent` is set, the script will not be cached. |
76 | 52 | - A nil (`undefined`/`null`) container value will append the script to `document.head`. |
77 | 53 | - Cleanup is not automatic — script elements remain in the DOM |
78 | 54 |
|
79 | | ---- |
80 | | - |
81 | 55 | ## 💬 Feedback & Contributions |
82 | 56 |
|
83 | 57 | Feel free to open [issues](https://github.com/dsnchz/load-script/issues) or submit pull requests. PRs are welcome! |
0 commit comments