Artifact state
A published artifact cannot reach the network: it is served with connect-src 'none' from an opaque
origin. That is deliberate — a model wrote the HTML. But a page still needs somewhere to keep its
own data (a graph's zoom level, a quiz's answers, a leaderboard), so state exists, and it is written
by the viewer chrome on the page's behalf.
This is the same idea as window.storage in Claude's artifacts, with one addition: an optional
rev for compare-and-set.
From the page
The chrome injects artifactkit into the frame. Nothing else is required — no script tag, no origin,
no token.
// Personal: only the person viewing the artifact sees this.
await artifactkit.storage.set('quiz:state', JSON.stringify(state));
// Shared: everyone who can see the artifact. Needs the artifact to be shared.
await artifactkit.storage.set('leaderboard:alice', JSON.stringify({ score: 12 }), { shared: true });
// Read / list / delete
const row = await artifactkit.storage.get('quiz:state'); // { value, rev, updatedAt } | null
const keys = await artifactkit.storage.list('quiz:'); // [{ key, rev, bytes, updatedAt }]
await artifactkit.storage.delete('quiz:state');
// Optional: never clobber someone else's write.
await artifactkit.storage.set('doc', next, { expectRev: row.rev }); // throws { code: 'conflict' }
await artifactkit.storage.set('claim', 'me', { expectRev: null }); // create only
Two scopes, never merged for the same key:
| scope | who sees it | requires |
|---|---|---|
personal:<principal> (default) | the person viewing | nothing |
shared | everyone who can see the artifact | the artifact has a share link |
Limits
| limit | value | enforced |
|---|---|---|
| one value | 256 KB | server, before the write |
| one artifact, all scopes | 20 MB | server, against the sum of stored bytes |
| key length | 256 characters | server, and in the bridge |
| keys per scope | 1000 | documented; listing is cheap but unbounded growth is not |
A rewrite replaces its own bytes, so editing the same key cannot creep up on the cap.
How it travels
page --postMessage--> chrome (trusted, holds the session) --HTTP--> /api/artifacts/{id}/state/{key}
page <--postMessage-- chrome <--JSON-- store (memory | KV | Postgres)
- The page never learns a URL or a credential.
- The bridge accepts a reply only from
window.parentand only one echoing the random token it generated at install, so a sibling frame or a stale page instance cannot answer for the chrome. targetOrigin: '*'is unavoidable (an opaque origin cannot be named), which is exactly why the receiver checksevent.sourceinstead. That check is the security boundary.
Consistency
| adapter | last write wins | compare-and-set | listing | usage |
|---|---|---|---|---|
| memory | yes | yes | consistent | exact |
| Postgres | yes | yes (conditional UPDATE) | consistent | exact |
| Cloudflare KV | yes | no — throws unsupported | eventually consistent | walks the keys |
A page that needs expectRev must be deployed against Postgres; the KV adapter refuses rather than
pretending, because a silent last-write-wins is exactly the bug the caller was trying to avoid.
Embedding it
// chrome side
import { createStorageHost } from '@artifactkitdev/core/browser/host';
const host = createStorageHost({
frame: () => iframe.contentWindow,
call: async (request) => api(request), // your fetch, your session
});
window.addEventListener('message', host.onMessage);
// page side — usually inlined into the artifact by the publisher
import { installStorageBridge } from '@artifactkitdev/core/browser/bridge';
const { storage } = installStorageBridge();
globalThis.artifactkit = { storage };
Related capabilities
- Events.
artifact.state.changedis published to theEventSink(never with the value). The activity cursor and webhook delivery are implemented; your host must wire the event log and delivery service and run the webhook drain. - Comments on state are not implemented. Threads still bind to rendered text, not to a key.