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

# Theme extensions

> Let a merchant place your app's widgets in their theme: the Apps page in the theme editor, app block slots, embeds and scripts, with working demos.

A theme is the storefront's React app. Your app reaches it in three ways, and
they differ in **who places your code**:

| Surface | Who places it | Where it renders | Demo below |
| - | - | - | - |
| **App embed** | The merchant, one toggle, every page | Head of every page | [Embed](#demo-an-app-embed) |
| **App block** | The merchant, per slot, from the theme editor's **Apps** page | Wherever the theme placed a slot | [Block](#demo-an-app-block) |
| **Script** | Your app, through the scripts API, no merchant action | Head, wherever your selector points | [Script](#demo-a-script) |

An app block is the surface most widgets want: the merchant chooses where it
goes, and it can be switched off per block without uninstalling.

## The Apps page in the theme editor

A theme earns an **Apps** page in the merchant's theme editor by declaring an
`appBlocks` field in its `salesive.form.json`:

```json theme={null}
{
  "page": "apps",
  "title": "App widgets",
  "sections": [
    {
      "id": "app-blocks",
      "title": "App widgets",
      "description": "Widgets from apps you installed, placed where shoppers will see them.",
      "fields": [
        { "id": "appBlocksProduct", "type": "appBlocks", "label": "Below the buy options on a product page" },
        { "id": "appBlocksCart", "type": "appBlocks", "label": "In the cart, under the checkout button" },
        { "id": "appBlocksOrderSuccess", "type": "appBlocks", "label": "After a finished order" }
      ]
    }
  ]
}
```

The merchant picks from the blocks their installed apps provide and drags them
into order. **The saved value is an array, not a boolean**:

```json theme={null}
[
  { "id": "b1", "block": "reviews/review-stars", "settings": { "size": "large" } },
  { "id": "b2", "block": "loyalty/points-badge" }
]
```

The matching default in `salesive.config.json` is `[]`, so a store with no apps
saves nothing and every slot stays empty.

> [!NOTE]
>
> A field with no default is `undefined` on stores that predate it. Always
> treat the value as "maybe an array": `Array.isArray(value) ? value : []`.

## What the storefront injects

The platform writes two globals the theme reads, and **only when the store has at
least one block installed**:

```js theme={null}
window.SALESIVE_APPS = { blocks: { "reviews/review-stars": { /* metadata */ } } };
window.SALESIVE_CONFIG.variables.appBlocksProduct = [ /* placements */ ];
```

It also loads `/app-blocks.js`, which walks the page for placeholders and mounts
each block into them. Your theme never calls into an app directly: it renders a
placeholder carrying the right attributes and the loader does the rest.

Because both globals are conditional, **a slot must be safe to render before
they exist**. Return `null` rather than an empty wrapper, or you will ship an
empty `<div>` with margins on every store that has no apps.

## Demo: an app block

This is the whole contract, dependency-free. It is the component the default
theme ships in `src/components/AppBlockSlot.jsx`:

```jsx theme={null}
const manifest = () => window.SALESIVE_APPS?.blocks || {};
const placedList = (name) => window.SALESIVE_CONFIG?.variables?.[name] || [];
const json = (v) => { try { return JSON.stringify(v && typeof v === "object" ? v : {}); } catch { return "{}"; } };

export default function AppBlockSlot({ name, context = {}, className = "" }) {
  const placed = (Array.isArray(placedList(name)) ? placedList(name) : [])
    .filter((item) => item && typeof item.block === "string" && manifest()[item.block]);

  if (!placed.length) return null;   // nothing placed, or the store has no apps

  return (
    <div className={`salesive-app-blocks ${className}`.trim()}>
      {placed.map((item, index) => (
        <div
          key={item.id || `${item.block}-${index}`}
          data-salesive-app-block={item.block}
          data-salesive-block-settings={json(item.settings)}
          data-salesive-block-context={json(context)}
        />
      ))}
    </div>
  );
}
```

Place it where the merchant's choice should appear:

```jsx theme={null}
{/* Product page, below the buy options */}
<AppBlockSlot name="appBlocksProduct" context={{ productId: item?._id }} className="mt-6" />

{/* Cart, under the checkout button */}
<AppBlockSlot name="appBlocksCart" className="mt-4" />

{/* After a finished order */}
<AppBlockSlot name="appBlocksOrderSuccess" context={{ orderId }} />
```

Passing `context` is how the block knows **which** product or order it is looking
at. A block mounted without context gets an empty object and must not assume one.

### The same thing with the SDK

If the theme already depends on `salesive-dev-tools`, its components do exactly
this and save you the file:

```jsx theme={null}
import { SalesiveAppBlocks } from "salesive-dev-tools/app-blocks";

<SalesiveAppBlocks value={config.appBlocksProduct} context={{ productId }} />
```

Use `hasAppBlock(key)` when the theme needs to reserve layout space or hide a
heading around a slot that might be empty. See
[storefront extensions](https://docs.salesive.com/apps/storefront-extensions.md) for the rest of the exports.

## Demo: an app embed

An embed needs no theme work at all: the merchant toggles it on, and the platform
puts your script in the head of every page.

```json theme={null}
{
  "extensions": {
    "embeds": [
      {
        "name": "Review widget",
        "scriptUrl": "https://yourapp.com/embed.js"
      }
    ]
  }
}
```

```js theme={null}
// https://yourapp.com/embed.js
(function () {
  var el = document.createElement("div");
  el.id = "yourapp-reviews";
  var host = document.querySelector("[data-yourapp-slot]") || document.body;
  host.appendChild(el);
  // The page does not give you a store id. Identify the store with your own
  // install token on your server, not with anything the storefront exposes.
  fetch("https://yourapp.com/api/reviews", { credentials: "omit" })
    .then(function (r) { return r.json(); })
    .then(function (data) { el.textContent = data.summary; });
})();
```

> [!NOTE]
>
> There is no store id in `window.SALESIVE_CONFIG` on a production storefront, so
> an embed cannot read one. Your embed calls your own server, and your server
> resolves which store the request is for from the install it recorded at OAuth
> time. Never trust a store id sent from the page.

> [!WARNING]
>
> An embed runs in the storefront page with the same reach as the theme's own
> code. It can read the DOM and any global the theme exposes. Do not scrape
> shopper input, and do not rely on an embed for anything that must be isolated.

## Demo: a script

A script is the app itself writing into the head through the API, with no
merchant step. Use it for analytics or a tag that must load before the theme.

```http theme={null}
POST /api/v1/scripts
Authorization: Bearer <app_ token>
```

```json theme={null}
{
  "name": "Heatmap",
  "code": "<script src=\"https://yourapp.com/heatmap.js\" defer></script>",
  "location": "head"
}
```

Needs `WRITE_SCRIPTS`. The merchant did not agree to this one individually, so
keep it to what the install consent described.

## Choosing a surface

> [accordion] The merchant should control placement
>
> Use an **app block**. It is the only surface where the merchant decides where
> the widget appears, and the only one they can move without editing code.

> [accordion] It runs on every page and the merchant turns it on
>
> Use an **app embed**. One toggle, no theme edits, and it survives a theme
> switch.

> [accordion] It must load unconditionally, before anything else
>
> Use a **script**. Reach for it last: it is invisible in the merchant's
> settings compared with an embed.

> [accordion] It should not run on the storefront at all
>
> Use the [app proxy](https://docs.salesive.com/apps/app-proxy.md), which serves your page under
> `/apps/<slug>` on the store's domain without your code entering the theme.

## Verifying your slot

1. Install your app on a development store.
2. Set an `appBlocks` field in the theme's form, and place your block in the
   editor's **Apps** page.
3. Open the storefront. The placeholder should carry
   `data-salesive-app-block="<your-slug>/<your-key>"`.
4. Uninstall the app and reload: the slot must disappear **and leave no wrapper
   behind**. A gap where the widget was is the most common bug here.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.