Skip to content

Commit a06ffa2

Browse files
committed
fix(site,ci): resolve Sanity CDN image URLs from production dataset and cap sync batch size
1 parent b8db67d commit a06ffa2

2 files changed

Lines changed: 41 additions & 21 deletions

File tree

‎.github/workflows/nightly-dataset-sync.yml‎

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,49 @@ jobs:
9898
}
9999
100100
(async () => {
101-
// Pass 1: Create stubs so all internal document references resolve
102-
const stubs = sanitized.map((d) => ({ createOrReplace: { _id: d._id, _type: d._type } }));
103-
for (let i = 0; i < stubs.length; i += 250) {
104-
await mutateBatch(stubs.slice(i, i + 250));
101+
// Helper to chunk mutations by both count and byte size (< 1.5MB, well below Sanity's 4MB limit)
102+
async function sendChunked(items, label) {
103+
let batch = [];
104+
let batchBytes = 0;
105+
let sent = 0;
106+
const MAX_ITEMS = 50;
107+
const MAX_BYTES = 1500000;
108+
109+
for (const item of items) {
110+
const mutation = { createOrReplace: item };
111+
const itemBytes = Buffer.byteLength(JSON.stringify(mutation), "utf8");
112+
if (batch.length > 0 && (batch.length >= MAX_ITEMS || batchBytes + itemBytes > MAX_BYTES)) {
113+
await mutateBatch(batch);
114+
sent += batch.length;
115+
if (sent % 500 < batch.length) {
116+
console.log(`[${label}] Imported ${sent} / ${items.length}...`);
117+
}
118+
batch = [];
119+
batchBytes = 0;
120+
}
121+
batch.push(mutation);
122+
batchBytes += itemBytes;
123+
}
124+
if (batch.length > 0) {
125+
await mutateBatch(batch);
126+
sent += batch.length;
127+
}
128+
console.log(`[${label}] Completed (${sent} / ${items.length}).`);
105129
}
106-
console.log("Pass 1 (document stubs) complete.");
130+
131+
// Pass 1: Create stubs so all internal document references resolve
132+
const stubs = sanitized.map((d) => ({ _id: d._id, _type: d._type }));
133+
await sendChunked(stubs, "Pass 1: Stubs");
107134
108135
// Pass 2: Populate full document bodies
109-
for (let i = 0; i < sanitized.length; i += 100) {
110-
const batch = sanitized.slice(i, i + 100).map((d) => ({ createOrReplace: d }));
111-
await mutateBatch(batch);
112-
if ((i / 100) % 5 === 0) {
113-
console.log(`Imported ${Math.min(i + 100, sanitized.length)} / ${sanitized.length} documents...`);
114-
}
115-
}
136+
await sendChunked(sanitized, "Pass 2: Content Docs");
116137
117-
// Pass 3 (best-effort): Import asset metadata documents
138+
// Pass 3 (best-effort): Import asset metadata documents so Studio Media Library resolves in dev
118139
const assetDocs = allDocs
119140
.filter((d) => d._type === "sanity.imageAsset" || d._type === "sanity.fileAsset")
120141
.map(({ _rev, ...rest }) => rest);
121142
try {
122-
for (let i = 0; i < assetDocs.length; i += 250) {
123-
const batch = assetDocs.slice(i, i + 250).map((d) => ({ createOrReplace: d }));
124-
await mutateBatch(batch);
125-
}
126-
console.log(`Imported ${assetDocs.length} asset metadata documents.`);
143+
await sendChunked(assetDocs, "Pass 3: Asset Docs");
127144
} catch (assetErr) {
128145
console.warn("Note: Asset metadata docs skipped (served via CDN):", assetErr.message);
129146
}

‎apps/site/src/lib/sanity/image.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ type Builder = ReturnType<typeof createImageUrlBuilder>;
99

1010
const builders = new Map<string, Builder>();
1111

12-
function builderFor({ projectId, dataset }: SanityConfig): Builder {
13-
const key = `${projectId}:${dataset}`;
12+
function builderFor({ projectId }: SanityConfig): Builder {
13+
// Image binary files synced from `production` to `dev` remain stored under
14+
// the `production` dataset path in Sanity's CDN (`images/<projectId>/production/...`).
15+
const imageDataset = "production";
16+
const key = `${projectId}:${imageDataset}`;
1417
let b = builders.get(key);
1518
if (!b) {
16-
b = createImageUrlBuilder({ projectId, dataset });
19+
b = createImageUrlBuilder({ projectId, dataset: imageDataset });
1720
builders.set(key, b);
1821
}
1922
return b;

0 commit comments

Comments
 (0)