I've been working on an embeddable checkout SDK for a while now, and one thing about how it ships ended up deciding more of its design than any single feature did, so I want to write it down while it's still fresh. The short version is that the SDK is two halves that deploy on their own, and the version mix between them never settles.
How the two halves ship
The SDK has a small host library, which merchants install from npm (or load from the CDN with a pinned file and an integrity hash), and a payment component, which wraps Stripe's elements as a custom element. The host doesn't bundle the component. When it starts, it fetches a manifest from the CDN that says where the current component bundle lives, and loads that:
// inside the host library, roughly
const manifest = await fetch(`${CDN}/manifest.json`).then((r) => r.json());
await loadScript(manifest["payment-component"].url);
The manifest URL has no version in it, which is the whole point (we can fix the component without asking every merchant to upgrade), and which also means that every host ever published loads the newest component. It's a deliberate trust decision too, since an integrity hash on the host file can't cover a component the host fetches later, so the page ends up trusting the CDN. Merchants upgrade the host whenever they get around to it, and some never do.
So a page in production can run any old host against the newest component, and after a component rollback, a new host can run against an older component. I knew all of this in the abstract, and still kept designing changes as if the mix were something that lasts an afternoon during a rollout, when in practice the SDK is in that state all the time.
What finally made it concrete was a Sentry event from a real incident (more on that below), which showed a host running six minor versions behind the component it had loaded. Once we had that in front of us, "we'll ship the host first" stopped being an answer to anything.
The rule we ended up with is that every change has to be safe with either half deployed alone, in any order, and with either half rolled back. That one rule shaped three designs that looked unrelated at first.
Removing a component that an old host doesn't know how to remove
The public API had unmount() and destroy() calls, and the docs said they remove the wallet button, which they didn't. The host only removed components that exposed the SDK's own teardown() method (a custom one, not the DOM's remove()), and only the card component had it.
The obvious fix was to add teardown() to the wallet component too, since the host already calls it and it'd just start working. The problem is that "the host already calls it" includes every old host, and old hosts also never cleared the container they had mounted into, so after an unmount, a later re-render could put the button right back where it was. Shipping the component alone would have changed public behavior under hosts that weren't ready for it.
There was a second problem underneath. With preact-custom-element (4.6, on Preact 10), removing the element from the DOM runs render(null) synchronously in disconnectedCallback, and Preact 10 runs the hook cleanups synchronously too, so by the time element.remove() returns, the whole tree is gone. If a payment was in flight, its cleanups had already reset the merchant callbacks, and the charge finished with no-op callbacks and followed the default success redirect, even when the merchant's own callback would have stopped it. An earlier attempt at the fix had been reverted for exactly that reason. The fix we kept makes the charge request the commit point, so an attempt torn down before it is abandoned with nothing charged, and an attempt that already sent it keeps snapshots of the callbacks and state it needs to finish.
What held up was giving the wallet component a new method name that only the new host calls:
// new host
unmount() {
for (const component of this.components) {
const el = component.element;
if (typeof el.release === "function") {
el.release(); // wallet, new component only
} else if (typeof el.teardown === "function") {
el.teardown(); // card, as before
} else if (!customElements.get(el.localName)) {
el.remove(); // bundle never loaded, so there's no payment to protect
}
component.parent = null; // so a later render doesn't re-attach it
}
}
The method name is the capability check. We talked about a separate boolean flag for it, but that would have been a second contract next to the method, which already says everything. Laid out as a matrix, the four combinations look like this:
| Host | Component |
unmount() on the wallet button |
|---|---|---|
| old | old | button stays (as before) |
| old | new | button stays, the old host doesn't know release
|
| new | old | button stays, the old component has neither method |
| new | new | button removed, a payment past its charge request still completes |
Nothing in that table depends on which side ships first, and a rollback of either side lands in one of the "button stays" rows, so the button never disappears under a merchant who didn't upgrade. The button only goes away once both sides are new, so the host release is the public change, and that's what goes in the release notes. One thing the table hides is that "old host" is a range and not a version, so the oldest host still showing up in Sentry is the one worth loading against a new component build before calling a row safe.
Telemetry that an old host would have forwarded
The incident was one handled Sentry event with a generic "unexpected error" title. The catch block that produced it threw the original error away, so the plan was to add the error name and message to the event payload, and to strip those fields in the host before they reached merchant listeners, because raw error text can carry buyer data and anything merchants receive becomes part of the public event contract. That looked like five lines of work.
The host forwards every component event to the merchant's on() listeners, including wildcard ones. A new host with an allowlist would strip the new fields, but an old host forwards whatever it gets, and every old host loads the new component. So "ship the host first" didn't help at all. It only protected the pages that had already upgraded, and the event we were debugging came from a page that hadn't.
The design moved to a separate DOM event with a name old hosts never listen for, dispatched on the component element itself, which the new host records as a Sentry breadcrumb and never forwards:
// component side, dispatched on the custom element itself
element.dispatchEvent(
new CustomEvent("example-telemetry", {
detail: { kind: "breadcrumb", errorType: "submit_error", context: { step } },
}),
);
// new host
element.addEventListener("example-telemetry", (event) => {
const { kind } = event.detail ?? {};
if (kind !== "breadcrumb") return; // unknown kinds are ignored
Sentry.addBreadcrumb(toBreadcrumb(event.detail)); // scrubbed, capped, never re-emitted
});
It doesn't bubble (that's the default for CustomEvent), but that isn't what keeps it away from merchants, since a capture listener higher up the page would still see it. The protection is that old hosts don't know the name, so they never pick it up and forward it, and a new host with an old component just gets nothing. New kinds stay additive because a host ignores kinds it doesn't know. We also looked at a new event name on the public bus (wildcard listeners would get it) and at a flag the host sets on the element (it works, but every producer has to check it and telemetry still goes through the public channel).
What goes into the breadcrumb follows the same "send nothing you can't vouch for" idea. Stripe.js error messages can echo the value you passed in, something like ... You specified: <value>, and that value can be a buyer's name or address. Cutting the message at "You specified" looked like enough, until a look through the live Stripe.js bundle turned up roughly 284 places that throw an integration error, with other wordings like You used: "..." and Invalid payment method type '...'. So Stripe text is now sent only for a couple of known wordings, and only the part with no caller value in it, and anything else sends no message at all. When Stripe rewords an error, the cost is some lost diagnostics and no buyer data leaves the page.
The page that produced the incident also loaded a pinned host behind an integrity hash, so the success criterion we first wrote ("the next event shows the breadcrumb") could never have passed there :|
Scripts that every copy of the SDK loads
The third design started from a plain bug that has nothing to do with versions. Two SDK instances on one page is more common than it sounds (a single-page app that creates one per route is enough), and each instance had its own script loader, so each one appended Stripe.js and our Stripe helper script again, and every run of the helper replaced the shared function on window that hands out Stripe instances, dropping the cache the earlier run had built. The version mix came in with the fix, because the copies on one page don't share a module and can be different versions, so the record of what's loaded can't live in a module variable. It lives on the script tags themselves:
<script
src="https://cdn.example.com/payment-helper.<version>.js"
data-loader-key="payment-helper"
data-loader-state="loading"
data-loader-deadline="<ms timestamp>"
></script>
Before appending, a copy that knows about these attributes looks for a tag with that key. A loaded tag resolves, a loading tag is waited on, and a loading tag well past its deadline (or with none) has no owner and gets replaced. A tag that fails or times out is removed by whoever appended it, so the next caller starts over, and waiters never append anything themselves, and settle when that tag or any later tag for the key loads, or when their own timeout runs out. The key is the manifest key and not the URL, because the URL carries a version and the key doesn't, so a key match still works when two copies resolved different builds. Whichever copy appends first serves the page. The component bundles already behave like that, since customElements.define throws on a second definition of the same name, and each bundle checks customElements.get before defining, so we didn't invent a new rule. Anything that lives in the shared page can be faked by other content on that page, so markers like these should only ever decide whether a script needs loading, and never anything that could run code or read payment data, so the worst a faked one can do is stop a script from loading.
Old hosts don't read these tags, so a page with an old host and a new one can still run the helper twice, and that's why the helper itself only fills in what's missing on the shared global:
// own properties only, so a polluted Object.prototype can't count as "already there"
const shared = Object.hasOwn(window, "ExampleSDK") ? window.ExampleSDK : (window.ExampleSDK = {});
for (const [name, fn] of Object.entries(helpers)) {
if (!Object.hasOwn(shared, name) || typeof shared[name] !== "function") {
shared[name] = fn;
}
}
A second run keeps the first run's functions and their cache and only adds the helpers an older run didn't have. The other side of that is that a newer component may run against an older helper's functions, so the component has to cope with a helper that isn't there, and a shared function's signature can only change by adding a function with a new name, never in place. The data-loader-* attributes are a contract between versions on one page now too, so they get the same treatment.
Summary
All of this has a price, since every public behavior change now gets a matrix like the one above before it gets code, the names pile up (the wallet's release next to the card's teardown is one example), and the visible part of a fix usually waits for the host release, which pinned pages only get when someone bumps the pin.
When a library loads its newest half at runtime under a half that customers pin, the mix of versions is the permanent state and release order protects nothing, so each change has to be designed so that either half alone does what the old version did. For us that meant using a new method name as the capability check instead of reusing an old one, sending new data on a channel the old side never hears instead of adding fields to a shared one, letting the first copy on a page own shared state that later copies only add to, and falling back to the old behavior whenever one side can't tell what the other supports.
Top comments (0)