1.2.8 โ€ข Published 5 months ago

@dschz/solid-create-script v1.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@dschz/solid-create-script

License npm Bundle Size JSR CI

SolidJS hook to load external scripts -- built on top of @dschz/load-script.

โœ… Features

  • ๐Ÿ“‘ Fully typed with TypeScript
  • โœ๏ธ Built on top of @dschz/load-script inheriting all the same features.
  • ๐Ÿ“† Declarative async tracking via Solid's createResource

๐Ÿ“ฆ Installation

npm install solid-js @dschz/load-script @dschz/solid-create-script
pnpm install solid-js @dschz/load-script @dschz/solid-create-script
yarn install solid-js @dschz/load-script @dschz/solid-create-script
bun install solid-js @dschz/load-script @dschz/solid-create-script

These are peer dependencies, so you must install:

  • solid-js
  • @dschz/load-script

๐Ÿง  API

createScript(src, options?, container?)

Loads an external script dynamically and returns a Resource<HTMLScriptElement>.

Parameters:

NameTypeDescription
srcstringScript URL (required)
optionsLoadScriptOptionsloadScript options (e.g. async, type)
containerHTMLElementHTML element to append <script /> to (default: document.head)

๐Ÿงช Example

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>
  );
};

๐Ÿ“ Notes

  • Scripts are cached by src unless innerHTML or textContent is 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)

๐Ÿ’ฌ Feedback & Contributions

Feel free to open issues or submit pull requests. PRs are welcome!