Render stored URLs
You upload images, your users edit them with the cloud image editor, and you store the result in your database. Later you need to render that image somewhere else: a 400×400 thumbnail in a list, or one entry of an srcset. The stored URL already carries the user's edits, so all you need to add is sizing on top.
Why not string concatenation
The obvious approach breaks in non-obvious ways:
// ❌ Looks fine, breaks in production
const thumb = `${stored}-/preview/400x400/`| Stored value | Result |
|---|---|
…/uuid/ | ✅ works, by luck |
…/uuid (no trailing slash) | ❌ …/uuid-/preview/400x400/, a broken path |
…/uuid/-/crop/640x480/photo.jpg | ❌ ops land after the filename, so they're ignored |
…/uuid/?token=exp=… | ❌ ops land after the query string, so it breaks |
Regex patching has the same problem from the other direction:
// ❌ Drops the user's edits along with the old sizing
const thumb = stored.replace(/-\/.*$/, '-/preview/400x400/')parseCdnUrl decomposes any CDN URL flavor into a plain object; serializeCdnUrl puts it back together. Everything between them is ordinary array and object manipulation.
What's in your database?
Depending on how you saved the editor output, you have one of three shapes. All three end in the same place: an operations array you can extend.
A full CDN URL
import { parseCdnUrl, serializeCdnUrl } from '@uploadcare/cdn-url'
import { preview } from '@uploadcare/cdn-url/ops'
const stored = 'https://ucarecdn.com/:uuid/-/crop/640x480/130,80/photo.jpg'
const parsed = parseCdnUrl(stored)
const thumb = serializeCdnUrl({
...parsed,
operations: [...parsed.operations, preview(400, 400)]
})
// → https://ucarecdn.com/:uuid/-/crop/640x480/130,80/-/preview/400x400/photo.jpgThe user's crop stays. The filename stays at the end, where the CDN expects it.
Under a size budget, the same append needs no operation model at all — the string level works on the raw chain:
import {
joinModifiers,
modifiers,
normalizeModifiers,
tinyBuild,
tinyParse
} from '@uploadcare/cdn-url/tiny'
const parts = tinyParse(stored)
const thumb = tinyBuild({
...parts,
modifiers: joinModifiers(parts.modifiers, modifiers('preview/400x400'))
})
// → https://ucarecdn.com/:uuid/-/crop/640x480/130,80/-/preview/400x400/photo.jpgIt assumes the stored value is a file URL: a group element or a proxy URL parses into fields that look right and serialize back wrong. Everything below stays with the operation model, which knows the difference.
A uuid only
Nothing to parse. Build it from scratch:
import { serializeCdnUrl } from '@uploadcare/cdn-url'
import { preview } from '@uploadcare/cdn-url/ops'
const thumb = serializeCdnUrl({
cdnBase: 'https://ucarecdn.com',
uuid: row.uuid,
operations: [preview(400, 400)]
})Or, string level, with no parsing to skip in the first place:
tinyBuild({
cdnBase: 'https://ucarecdn.com',
uuid: row.uuid,
modifiers: modifiers('preview/400x400')
})A uuid + modifiers string
Some integrations store the modifiers string (the uploader calls it cdnUrlModifiers, e.g. '-/crop/640x480/130,80/') in its own column (row.modifiers below). parseOperations turns it back into an array:
import { parseOperations, serializeCdnUrl } from '@uploadcare/cdn-url'
import { preview } from '@uploadcare/cdn-url/ops'
const thumb = serializeCdnUrl({
cdnBase: 'https://ucarecdn.com',
uuid: row.uuid,
operations: [...parseOperations(row.modifiers), preview(400, 400)]
})This is the shape the string level fits best, since the stored column is already a chain string. normalizeModifiers accepts it in any of the forms integrations save it in — with or without the leading -, with or without edge slashes:
tinyBuild({
cdnBase: 'https://ucarecdn.com',
uuid: row.uuid,
modifiers: joinModifiers(
normalizeModifiers(row.modifiers),
modifiers('preview/400x400')
)
})Append vs replace
Operations form a sequential pipeline, and order matters. Appending preview after the stored crop resizes the cropped result, which is almost always what you want. Appending it before would crop the resized image instead.
For non-repeatable operations (quality, format, …) the CDN applies the last occurrence when the same one appears twice; sizing operations genuinely stack as pipeline steps. Appending a second quality works, but the URL carries dead weight, and a second resize after a preview chain can produce surprising sizes. When the stored URL may already contain the operation you're adding, replace instead of append:
const withoutSizing = parsed.operations.filter(
(op) => op.name !== 'preview' && op.name !== 'resize'
)
const thumb = serializeCdnUrl({
...parsed,
operations: [...withoutSizing, preview(400, 400)]
})To catch accidental duplicates during development, run the chain through validateOperations; duplicates surface as duplicate-operation warnings.
Rebasing onto another domain
Stored URLs often point at the legacy ucarecdn.com while your project now serves from a prefixed or custom domain, one you've already configured in your project settings; the library only writes the string. The cdnBase is just a field:
const rebased = serializeCdnUrl({
...parseCdnUrl(stored),
cdnBase: 'https://cdn.example.com' // your CNAME, or https://<prefix>.ucarecd.net
})Everything else survives untouched: the uuid, the user's edits, the filename. The CDN base covers where that value comes from — prefixedCdnBase derives your project's from its public key — and why a rebase invalidates a signed URL.
A taste of srcset
Generating width variants is a map:
const widths = [320, 640, 1280]
const srcset = widths
.map((w) => {
const url = serializeCdnUrl({
...parsed,
operations: [...parsed.operations, resize({ width: w })]
})
return `${url} ${w}w`
})
.join(', ')See Responsive images for the full treatment, or skip the manual work entirely with <uc-img> adaptive delivery, which generates the variants for you.
Signed URLs
If your project uses secure delivery, stored URLs may carry ?token=…. Parsing preserves the token, but appending operations changes the path the signature was computed for, so the CDN rejects the modified URL with the old token. Re-sign it before serving.
Defensive parsing
Database rows lie. parseCdnUrl throws a TypeError on anything that isn't a CDN URL, in both the development and production bundles, since this is structural rather than validation:
function tryParse(stored: string) {
try {
return parseCdnUrl(stored)
} catch {
return null // log it, render a placeholder, fix the row
}
}If your table can also contain group URLs, narrow by kind before touching uuid:
const parsed = parseCdnUrl(stored)
switch (parsed.kind) {
case 'file':
return renderThumb(parsed)
case 'group':
return renderGallery(parsed.group)
default:
return renderFallback()
}Putting it together
A thumbnail grid from a list of rows, handling all three storage shapes:
import {
parseCdnUrl,
parseOperations,
serializeCdnUrl
} from '@uploadcare/cdn-url'
import { preview } from '@uploadcare/cdn-url/ops'
const THUMB = [preview(400, 400)]
function thumbUrl(row: ImageRow): string | null {
try {
if (row.cdnUrl) {
const parsed = parseCdnUrl(row.cdnUrl)
if (parsed.kind !== 'file') return null
return serializeCdnUrl({
...parsed,
operations: [...parsed.operations, ...THUMB]
})
}
return serializeCdnUrl({
cdnBase: 'https://ucarecdn.com',
uuid: row.uuid,
operations: [...parseOperations(row.modifiers ?? ''), ...THUMB]
})
} catch {
return null
}
}{
rows.map((row) => {
const src = thumbUrl(row)
return src ? (
<img key={row.id} src={src} width={400} height={400} alt={row.alt} />
) : null
})
}