SolidJS 1.x hook to load external scripts -- built on top of
@dschz/load-script.
This package wraps loadScript in createResource, which SolidJS 2.0 removed. In 2.0, async values are part of the reactive graph: a computation can return a promise, a <Loading> boundary shows its fallback until the promise resolves, and an <Errored> boundary catches a rejection. So use @dschz/load-script directly:
npm install @dschz/load-scriptimport { loadScript } from "@dschz/load-script";
import { createMemo, Errored, Loading, Show } from "solid-js";
const CustomComponent = () => {
// Resolves to the <script> element once it has loaded (cached by `src`).
const script = createMemo(() => loadScript("https://example.com/widget.js", { async: true }));
return (
<Errored fallback={<p>Failed to load</p>}>
<Loading fallback={<p>Loading Script...</p>}>
<Show when={script()}>Script is ready!</Show>
</Loading>
</Errored>
);
};SolidJS 1.x (createScript) |
SolidJS 2.0 (loadScript + primitives) |
|---|---|
createScript(src, options) |
createMemo(() => loadScript(src, options)) |
script.loading |
<Loading fallback={…}> around the reads |
script.error |
<Errored fallback={…}> around the reads |
script() once loaded |
script() inside the boundaries |
loadScript rejects on the server, so in a server-rendered app load the script on the client only (for example, a component rendered client-side).
This package stays available for SolidJS 1.x (1.6 or newer) and is not updated for 2.0.
- 📑 Fully typed with TypeScript
- ✏️ Built on top of
@dschz/load-scriptinheriting all the same features. - 📆 Declarative async tracking via Solid's
createResource
npm install solid-js@1 @dschz/load-script @dschz/solid-create-script
pnpm add solid-js@1 @dschz/load-script @dschz/solid-create-script
yarn add solid-js@1 @dschz/load-script @dschz/solid-create-script
bun add solid-js@1 @dschz/load-script @dschz/solid-create-scriptThese are peer dependencies, so you must install:
solid-js1.6 or newer, below 2.0@dschz/load-script
Loads an external script dynamically and returns a Resource<HTMLScriptElement>.
| Name | Type | Description |
|---|---|---|
src |
string |
Script URL (required) |
options |
LoadScriptOptions |
loadScript options (e.g. async, type) |
container |
HTMLElement |
HTML element to append <script /> to (default: document.head) |
import { Switch, Match } from "solid-js";
import { createScript } from "@dschz/solid-create-script";
const CustomComponent = () => {
const script = createScript("https://example.com/widget.js", { async: true });
return (
<Switch>
<Match when={script.loading}>Loading Script...</Match>
<Match when={script.error}>Failed to load</Match>
<Match when={script()}>Script is ready!</Match>
</Switch>
);
};- Scripts are cached by
srcunlessinnerHTMLortextContentis used - Scripts are not automatically removed on cleanup/unmount
- Designed to be simple and safe to use inside SolidJS components (in SSR and non-SSR environments)
Feel free to open issues or submit pull requests. PRs are welcome!