Repository navigation
fix: resolve bare media ID in MediaValue.src to file URL - #701
Conversation
🦋 Changeset detectedLatest commit: b7e2652 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
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 |
|
All contributors have signed the CLA ✍️ ✅ |
fd1acb6 to
621b82a
Compare
|
I have read the CLA Document and I hereby sign the CLA |
|
/review |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/blocks
@emdash-cms/cloudflare
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
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 mediasrcvalues into/_emdash/api/media/file/<key>URLs and backfillidwhen missing. - Add a unit test covering the bare-
src→ file-URL behavior via the loader. - Add a patch changeset for the
emdashpackage.
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.
| 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}`; | ||
| } |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Good catch. Will only rewrite src for bare storage keys, leaving URLs/absolute paths as-is. Also backfilling id from the internal media prefix.
| 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}`; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good point. Moving local media normalization into a shared helper used by both mapRowToData() and mapRevisionData() for consistent MediaValue output.
| 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" } }), | ||
| ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
LGTM! |
|
Can you check the failing test |
|
@ascorbic Sorry for the delay. I’ve pushed an update addressing this now. |
What does this PR do?
When local media is saved without storageKey, the loader returned the bare ULID in
srcinstead 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
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runpnpm locale:extracthas been run (if applicable)AI-generated code disclosure
Screenshots / test output