> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abtestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dev libraries

> Attach a curated JavaScript library to a variant. It loads once per page, on the pages that need it, cached at the CDN.

If two variants both need a carousel library, you do not want two copies
of Splide on the page. Dev libraries fix that. Attach a library to a
variant from a picker; the runtime injects the script and the CSS
exactly once per page, on the pages a variant actually applies to.

<Frame caption="Developer libraries tab, registered libraries with load-time telemetry.">
  <img src="https://mintcdn.com/abtestly/pnpJFDTFa50Ks6SL/images/dev-libraries.png?fit=max&auto=format&n=pnpJFDTFa50Ks6SL&q=85&s=2db928b64d16aaa75cfd83aa24374f65" alt="Developer libraries" width="2400" height="1141" data-path="images/dev-libraries.png" />
</Frame>

## What is on offer

Curated for now. The catalog is worker-authoritative and each entry is
pinned to a specific version with an SRI hash. Adding a library takes
a small PR to the catalog.

Today, examples:

* `splide@4.1.4`, Splide carousel.
* `glightbox@3.3.0`, GLightbox lightbox.

The current catalog is visible from the Dev libraries picker in the
variation editor.

## Attach one

1. In the variation editor for a variant, open the **Libraries** picker.
2. Pick a library. It gets added to the variant's dependency list.
3. Save. The library's starter code is appended to your variant's JS if
   the starter is more than a stub.

You can attach up to **three** libraries per variant.

## Use it in variant JS

Inside your variant's code, the library is available via a scoped
accessor on the apply context:

```js theme={null}
abtestly.onApply(ABTESTLY_EXP_ID, async (ctx) => {
  const Splide = await ctx.libs.get('splide');
  new Splide('.hero-carousel').mount();
});
```

* `ctx.libs.get(key)` returns the library's default export.
* The Promise resolves once the library has finished loading on the
  page.
* If you did not declare the library on the variant, the call throws
  `UndeclaredLibraryDependencyError`. Add it to the picker first.

## What the runtime actually does

On the first page a variant needing library X applies to, the runtime
injects a `<script>` (or `<link>`) with the pinned URL and SRI hash and
waits for the load. On the second page, and for any other variant on
the same page that also needs X, the library is already loaded, the
picker's second call resolves synchronously.

Libraries are hosted on jsDelivr today; the target endgame is
`cdn.abtestly.com/lib/` which lets us pin, warm the cache, and skip a
third-party origin.

## Diagnostic telemetry

The picker sends a tiny telemetry beacon per library load, start,
resolve, and any load errors. That is what powers the load-time
column in the Libraries tab under **Site → Developer libraries**. No
per-visitor data goes to that dataset.

## Approve, deprecate, delete

Under **Site → Developer libraries**, workspace admins can:

* **Approve**, mark a library as endorsed for the site.
* **Deprecate**, mark it as "please move off", without breaking
  existing variants.
* **Delete**, refused (`409 in_use`) while any variant still
  references it. Detach first, then delete.

## When *not* to reach for a library

Everything on the catalog is a proper JavaScript library. If your
variant needs one line of jQuery-style behavior, write it inline.
adding a library has a real byte cost that the picker cannot hide.
