# @solid-primitives/script-loader

> Primitive to load scripts dynamically

Latest version **3.0.0** (published 2026-11-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @solid-primitives/script-loader
pnpm add @solid-primitives/script-loader
yarn add @solid-primitives/script-loader
bun add @solid-primitives/script-loader
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2026-11-17 |
| First published | 2021-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1568 |
| Author | Alex Lohr <alex.lohr@logmein.com> |
| Maintainers | davedbase, lexlohr, thetarnav. |
| Keywords | script, jsonp, solid, primitives |

## Links

- npm: https://www.npmjs.com/package/@solid-primitives/script-loader
- Repository: https://github.com/solidjs-community/solid-primitives
- Homepage: https://primitives.solidjs.community/package/script-loader
- npm.io page: https://npm.io/package/@solid-primitives/script-loader

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.0.0 (latest) — 2026-11-17
- 3.0.0-next.4 (next) — 2026-10-14
- 3.0.0-next.2 — 2026-08-12
- 3.0.0-next.1 — 2026-07-18
- 3.0.0-next.0 — 2026-06-26
- 2.3.2 — 2025-06-29
- 2.3.1 — 2025-04-27
- 2.3.0 — 2025-01-22
- 2.2.0 — 2024-06-12
- 2.1.2 — 2024-03-05
- 2.1.1 — 2024-01-16
- 2.1.0 — 2024-01-15
- 2.0.2 — 2023-03-23
- 2.0.1 — 2023-02-28
- 2.0.0 — 2023-02-01
- … 10 more at https://npm.io/package/@solid-primitives/script-loader/versions

## README

<p>
  <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Script%20Loader&version=2.0" alt="Solid Primitives Script Loader">
</p>

# @solid-primitives/script-loader

[![size](https://img.shields.io/badge/size-545_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/script-loader)
[![size](https://img.shields.io/npm/v/@solid-primitives/script-loader?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/script-loader)
[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)
[![tested with vitest](https://img.shields.io/badge/tested_with-vitest-6E9F18?style=for-the-badge&logo=vitest)](https://vitest.dev)

Creates a primitive to load scripts dynamically, either for external services or jsonp requests

## Installation

```
npm install @solid-primitives/script-loader
# or
yarn add @solid-primitives/script-loader
# or
pnpm add @solid-primitives/script-loader
```

## How to use it

### createScriptLoader

Appends a script (by URL or inline source) to `document.head` and removes it when the owner is disposed.

```tsx repl
import { render } from "@solidjs/web";
import { createSignal } from "solid-js";
import { createScriptLoader } from "@solid-primitives/script-loader";

function Demo() {
  const [status, setStatus] = createSignal("loading...");
  createScriptLoader({
    src: "https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.3/dist/confetti.browser.min.js",
    onLoad: () => setStatus("loaded"),
    onError: () => setStatus("failed to load"),
  });
  return (
    <>
      <p>canvas-confetti: {status()}</p>
      <button disabled={status() !== "loaded"} onClick={() => (window as any).confetti()}>
        Confetti!
      </button>
    </>
  );
}

render(() => <Demo />, document.getElementById("app")!);
```

createScriptLoader expects a props object with a `src` property. All the other props will be spread to the script element.

The `src` prop is required and will be used to set the `src` or `textContent` attribute. It can be a string or an accessor.

```ts
import { createScriptLoader } from "@solid-primitives/script-loader";

// For example, to use recaptcha:
createScriptLoader({
  src: "https://www.google.com/recaptcha/enterprise.js?render=my_token",
  async onLoad() {
    await grecaptcha.enterprise.ready();
    const token = await grecaptcha.enterprise.execute("my_token", { action: "login" });
    // do your stuff...
  },
});

// or pinterest embeds:
createScriptLoader({
  src: '!function(a,b,c){var d,e,f;d="PIN_"+~~((new Date).getTime()/864e5),...',
  onLoad() {
    window?.PinUtils?.build();
  },
});
```

## Definition

```ts
function createScriptLoader(props: ScriptProps): HTMLScriptElement | undefined; // script element with be undefined only on the server

type ScriptProps = Omit<ComponentProps<"script">, "src" | "textContent"> & {
  /** URL or source of the script to load. */
  src: string | Accessor<string>;
};
```

## Changelog

See [CHANGELOG.md](./CHANGELOG.md)

---
_Source: https://npm.io/package/@solid-primitives/script-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
