From 20684521ebc94bccf76d43ea60d96a24eaed2011 Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:53:04 -0700 Subject: [PATCH] Keep curator alt text on product images when syncing The sync set every product image's alt to the Printify title, so alt text a curator wrote in Payload was lost at the next sync. Now an image's alt is filled with the title only when it is empty, on first import and on later syncs. Images are matched by URL. Title import is unchanged. Tested: unit tests (new image, curator alt kept, emptied alt refilled) and the same flow against an in-memory MongoDB replica set. Not tested: a live Printify account, or a store with a custom images field. Co-Authored-By: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Co-Authored-By: Garfunkel Botsworth <357926774+garfunkelbot@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 +++++++ src/sync.ts | 14 +++++++++++++- test/plugin.test.ts | 25 +++++++++++++++++++++++++ test/real-db.test.ts | 21 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ecb6ef9 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog + +## Unreleased + +- Syncing no longer overwrites product image alt text written in Payload. An image's alt is set to the product title only when it is empty, on first import and on later syncs. Images are matched by URL, so an image whose URL changes in Printify gets the title again. Titles are imported as before. +- Tested: unit tests for a new image getting the title, a curator's alt surviving a resync (also when the title changes), and an emptied alt being refilled. The same flow on a real in-memory MongoDB. Full suite (78 tests), typecheck and leak check pass. +- Not tested: against a live Printify account, or a store whose `images` rows are shaped differently from the plugin's own. diff --git a/src/sync.ts b/src/sync.ts index 5cd3caa..18e2511 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -27,6 +27,18 @@ export const slugify = (s: string) => .replace(/^-+|-+$/g, '') .slice(0, 80) || 'product' +type StoredImage = { url?: string | null; alt?: string | null } + +/** + * Put freshly mapped images onto the stored ones without losing alt text written in Payload. + * An image is matched by its URL. A stored alt that has text is kept; an empty or missing one + * is filled with the title the mapping gave it. + */ +export function mergeImageAlt(images: T[], stored: StoredImage[] | null | undefined): T[] { + const kept = new Map((stored ?? []).filter((s) => s.url && s.alt?.trim()).map((s) => [s.url, s.alt as string])) + return images.map((img) => (kept.has(img.url) ? { ...img, alt: kept.get(img.url)! } : img)) +} + /** The parts of a Printify product a store keeps (pure). Prices stay in cents. */ export function mapPrintifyProduct(p: PrintifyProduct) { const enabled = p.variants.filter((v) => v.is_enabled) @@ -108,7 +120,7 @@ export async function syncPrintifyProduct(payload: Payload, opts: ResolvedPrinti slug, generateSlug: false, description: m.description, - images: m.images, + images: mergeImageAlt(m.images, existing?.images as StoredImage[] | undefined), enableVariants: m.options.length > 0, variantTypes: typeIds, [`${price}Enabled`]: m.minPrice != null, diff --git a/test/plugin.test.ts b/test/plugin.test.ts index 7da7ecf..2934101 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -3,6 +3,7 @@ import type { Config, PayloadRequest } from 'payload' import { describe, expect, it } from 'vitest' import { mapPrintifyProduct, printifyPlugin, validPrintifySignature, type PrintifyProduct } from '../src' +import { mergeImageAlt } from '../src/sync' import { pushVariantPriceHook } from '../src/prices' import { pushVisibilityHook } from '../src/visibility' import type { PrintifyPluginOptions, ResolvedPrintifyOptions } from '../src/types' @@ -62,6 +63,30 @@ describe('mapping', () => { }) }) +describe('image alt text', () => { + const front = 'https://images-api.printify.com/mockup/front.jpg' + const back = 'https://images-api.printify.com/mockup/back.jpg' + const mapped = () => mapPrintifyProduct(tee()).images + + it('gives a new image the product title', () => { + expect(mergeImageAlt(mapped(), undefined).map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee']) + expect(mergeImageAlt(mapped(), [{ url: 'https://example.test/other.jpg', alt: 'Other' }]).map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee']) + }) + + it('keeps an alt a curator wrote through a resync, even when the title changes', () => { + const stored = [{ url: front, alt: 'A gecko on a black tee, facing left' }, { url: back, alt: 'Leopard Gecko Tee' }] + const out = mergeImageAlt(mapPrintifyProduct(tee({ title: 'Gecko Tee v2' })).images, stored) + expect(out.find((i) => i.url === front)?.alt).toBe('A gecko on a black tee, facing left') + expect(out.find((i) => i.url === back)?.alt).toBe('Leopard Gecko Tee') + }) + + it('refills an alt that was emptied', () => { + const out = mergeImageAlt(mapped(), [{ url: front, alt: '' }, { url: back, alt: ' ' }]) + expect(out.map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee']) + expect(mergeImageAlt(mapped(), [{ url: front, alt: null }]).map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee']) + }) +}) + describe('webhooks', () => { it('accepts only correctly signed bodies', () => { const body = JSON.stringify({ id: 'e1', type: 'product:updated' }) diff --git a/test/real-db.test.ts b/test/real-db.test.ts index 6587185..009ff7a 100644 --- a/test/real-db.test.ts +++ b/test/real-db.test.ts @@ -206,6 +206,27 @@ async function unknownOrder(s: Store, item: { product: string; variant: string } return doc } +describe('image alt text, on a real database', () => { + const withImage = (title: string): PrintifyProduct => ({ + ...product('pf-alt-1', 4001, title), + images: [{ src: 'https://images-api.printify.com/mockup/alt.jpg', variant_ids: [4001], position: 'front', is_default: true }], + }) + const altOf = async (id: string) => ((await one.payload.findByID({ collection: 'products', id, depth: 0, draft: true })) as Doc).images as Doc[] + const setAlt = async (id: string, alt: string) => + one.payload.update({ collection: 'products', id, data: { images: (await altOf(id)).map((i) => ({ ...i, alt })) } as never, draft: true }) + + it('fills alt, keeps a curator’s alt through resyncs, and refills it when emptied', async () => { + const { id } = await syncPrintifyProduct(one.payload, one.opts, null, withImage('Alt Mug')) + expect((await altOf(id))[0]!.alt).toBe('Alt Mug') + await setAlt(id, 'A white mug with a gecko') + await syncPrintifyProduct(one.payload, one.opts, null, withImage('Alt Mug 2')) + expect((await altOf(id))[0]!.alt).toBe('A white mug with a gecko') + await setAlt(id, '') + await syncPrintifyProduct(one.payload, one.opts, null, withImage('Alt Mug 2')) + expect((await altOf(id))[0]!.alt).toBe('Alt Mug 2') + }) +}) + let mongo: MongoMemoryReplSet let one: Store let item: { product: string; variant: string }