Skip to content

fix: resolve bare media ID in MediaValue.src to file URL - #701

Merged
ascorbic merged 7 commits into
emdash-cms:mainfrom
lsngmin:fix/media-value-src-url
May 1, 2026
Merged

ascorbic merged 7 commits into
emdash-cms:mainfrom
lsngmin:fix/media-value-src-url

Conversation

@lsngmin

@lsngmin lsngmin commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

When local media is saved without storageKey, the loader returned the bare ULID in src instead of a usable URL.

Users can still get a valid URL — the loader now rewrites bare IDs to /_emdash/api/media/file/<id> at read time.

Closes #696

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code

Screenshots / test output

@changeset-bot

changeset-bot Bot commented Apr 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b7e2652

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@lsngmin
lsngmin force-pushed the fix/media-value-src-url branch from fd1acb6 to 621b82a Compare April 21, 2026 12:39
@lsngmin

lsngmin commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Apr 21, 2026
@ascorbic

Copy link
Copy Markdown
Member

/review

@pkg-pr-new

pkg-pr-new Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@701

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@701

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@701

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@701

emdash

npm i https://pkg.pr.new/emdash@701

create-emdash

npm i https://pkg.pr.new/create-emdash@701

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@701

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@701

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@701

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@701

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@701

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@701

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@701

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@701

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@701

commit: b7e2652

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a loader/runtime data-shape bug where local MediaValue records stored without a storageKey could surface a bare ULID in src instead of a usable media URL, by rewriting that value at read time in the core loader.

Changes:

  • Add read-time rewriting in mapRowToData() to convert local media src values into /_emdash/api/media/file/<key> URLs and backfill id when missing.
  • Add a unit test covering the bare-src → file-URL behavior via the loader.
  • Add a patch changeset for the emdash package.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/core/src/loader.ts Rewrites parsed local-media JSON values so src becomes a file URL and id is backfilled.
packages/core/tests/unit/loader-media-src.test.ts New unit test verifying the loader rewrites a bare local media ID in src into a file URL.
.changeset/brave-chefs-wait.md Declares a patch release note for the loader media src fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/core/src/loader.ts Outdated
Comment on lines +151 to +155
const parsed = JSON.parse(value);
if (parsed?.provider === "local" && parsed?.src) {
parsed.id = parsed.id || parsed.src;
parsed.src = `/_emdash/api/media/file/${parsed.src}`;
}

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rewrite triggers for any JSON object with provider === "local" and a truthy src, even when src is already a usable URL/path. This can produce invalid URLs like /_emdash/api/media/file//_emdash/api/media/file/... and can also set id to a URL when id is empty. Consider tightening the condition to only rewrite when src is a bare key/id (e.g., a string that does not start with "/" or "http"), and keep existing src values that are already URLs untouched (optionally extracting id from an existing internal prefix when id is missing).

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Will only rewrite src for bare storage keys, leaving URLs/absolute paths as-is. Also backfilling id from the internal media prefix.

Comment thread packages/core/src/loader.ts Outdated
Comment on lines +151 to +155
const parsed = JSON.parse(value);
if (parsed?.provider === "local" && parsed?.src) {
parsed.id = parsed.id || parsed.src;
parsed.src = `/_emdash/api/media/file/${parsed.src}`;
}

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This normalization only happens in mapRowToData (content-table rows). In preview mode loadEntry builds data from mapRevisionData(parsed), so revision-backed reads can still surface the legacy bare src value. Consider sharing the same local-media src fix between mapRowToData and mapRevisionData so normal and preview reads return consistent MediaValue shapes.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Moving local media normalization into a shared helper used by both mapRowToData() and mapRevisionData() for consistent MediaValue output.

Comment on lines +28 to +40
it("resolves bare media ID in src to a file URL", async () => {
await handleContentCreate(db, "post", {
data: {
title: "Test",
hero: { provider: "local", id: "", src: "01KPD97MWB5DVHBHK69TW55KY3" },
},
status: "published",
});

const loader = emdashLoader();
const result = await runWithContext({ editMode: false, db }, () =>
loader.loadCollection!({ filter: { type: "post" } }),
);

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test covers the loadCollection path, but the same parsing logic is also used by loadEntry (and preview mode uses a different path via revision data). To prevent regressions, consider adding assertions/tests for loader.loadEntry and for a revision-backed loadEntry where the stored media value contains a bare src ID.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Adding coverage for loadEntry() and revision-backed loadEntry() with a bare local media src, plus a case verifying existing URLs aren't rewritten again.

@ask-bonk

ask-bonk Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@ascorbic

Copy link
Copy Markdown
Member

Can you check the failing test

@lsngmin

lsngmin commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

@ascorbic Sorry for the delay. I’ve pushed an update addressing this now.

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ascorbic
ascorbic merged commit a2d3658 into emdash-cms:main May 1, 2026
29 checks passed
@emdashbot emdashbot Bot mentioned this pull request May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MediaValue.src contains bare media ID instead of URL

3 participants