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:

scopewho sees itrequires
personal:<principal> (default)the person viewingnothing
sharedeveryone who can see the artifactthe artifact has a share link

Limits

limitvalueenforced
one value256 KBserver, before the write
one artifact, all scopes20 MBserver, against the sum of stored bytes
key length256 charactersserver, and in the bridge
keys per scope1000documented; 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)

Consistency

adapterlast write winscompare-and-setlistingusage
memoryyesyesconsistentexact
Postgresyesyes (conditional UPDATE)consistentexact
Cloudflare KVyesno — throws unsupportedeventually consistentwalks 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 };