Skip to content

fix(core): honor storage publicUrl for site settings media and <Media> - #4284

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
devondragon:fix/public-url-site-settings-and-embeds
Nov 4, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
devondragon:fix/public-url-site-settings-and-embeds

Conversation

@devondragon

Copy link
Copy Markdown
Contributor

What does this PR do?

With publicUrl set on R2 or S3 storage, the site logo, favicon, and default social image from getSiteSettings() / getSiteSetting() still point at /_emdash/api/media/file/{key}, so every template that renders settings.logo.url proxies those bytes through the Worker. This PR applies publicUrl to those URLs at read time.

It also fixes <Media> (emdash/ui) for media library items. It looked up the local provider through getMediaProvider(), which creates it with an empty context, so rendering threw Local media provider requires database connection. <Media> now handles local media the same way <Image>, <Gallery>, and <Video> do, and so picks up publicUrl as well.

I reported #508 and run EmDash on Cloudflare with R2. #675 fixed the render components; this covers the remaining read-time surfaces. It replaces the server-side half of #729, which has gone stale with conflicts. That PR's first commit merged as #675.

Why the API and admin URLs stay internal

#729 also changed the media REST API, the plugin ctx.media.upload() result, the local provider's previewUrl, and the admin settings API to return CDN URLs. This PR leaves them on the internal route because those URLs get stored:

  • The admin copies item.url into content and settings (mediaItemToImageAttrs in PortableTextEditor.tsx, the logo/favicon pickers in GeneralSettings.tsx).
  • Site transfer rewrites only /_emdash/api/media/file/ paths (transfer/format/media-refs.ts), so stored CDN URLs would not be carried across a transfer.
  • Admin thumbnails resize and cache-bust only internal URLs (getMediaThumbnailUrl / getMediaPreviewUrl), so CDN URLs would load full-size originals in the media grid.

How it works

  • Middleware sets a publicMediaUrl resolver on the request context in the same place it builds locals.emdash.getPublicMediaUrl, which getSiteSettings() reads because it has no Astro.locals. The redirect middleware sets siteSettings on the parent context the same way.
  • getSiteSettings() / getSiteSetting() rewrite the three media URLs on a per-request copy through buildRenderMediaUrl. The cached settings snapshot, the object cache entry, and getSiteSettingsWithDb() (admin API, sitemap, robots) keep the internal route.
  • Without publicUrl, output is unchanged: the R2 and S3 adapters return the internal route.
  • The rewrite runs no queries, so logged-out query counts are unchanged.

Not in scope: when publicUrl is set, <Image> with stored dimensions still sends the absolute CDN URL through Astro's /_image endpoint, which fails on Workers (reported in a comment on #729). That needs its own change.

Related: #508 (closed automatically when #675 merged), #675, #729

Type of change

  • Bug fix
  • Feature (requires an accepted feature plan or RFC)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Your connection to this change

  • I use EmDash and ran into the problem this PR addresses
  • I maintain a tool, dependency, or service that EmDash uses, and this change concerns that integration
  • A Maintainer approved this specific change before I opened the PR

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Not applicable: no admin UI changes.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to their merged design PR: not applicable, bug fix.
  • I have included screenshots below if this PR changes the UI. Not applicable: no UI changes.

AI assistance disclosure

  • This PR includes AI-generated code or text — model/tool: Claude Code with Claude Opus 5.5 (implementation and PR text); Claude Opus 5.5 and Claude Fable 5.1 (code review passes)

Screenshots / test output

Not applicable (no UI changes).

Manual check on a local site from templates/starter-cloudflare (astro dev, local D1 and R2), with publicUrl pointed at a separate origin that proxies to the media route:

  • On this branch, logged-out and logged-in pages render the logo, favicon, og:image, and <Media> from the publicUrl origin, and the browser loads them from there. <Media> with a bare media id stays on /_emdash/api/media/file/. The media and settings APIs still return the internal route.
  • On main, the same pages render all three settings images from /_emdash/api/media/file/, and a page containing <Media> for a library item returns an empty body.

New tests, each confirmed to fail with the source change reverted:

  • tests/unit/settings/settings.test.ts: logo, favicon, and default OG image use the storage public URL through getSiteSettings() and getSiteSetting(), and the cached snapshot stays on the internal route.
  • tests/unit/astro/middleware-prerender.test.ts: the resolver reaches page renders on the anonymous, authenticated, and scoped-db paths.
  • tests/repro/image-render.render.test.ts: <Media> with the local provider registered as in production (previously threw) renders the public URL for a storage key and the internal route for a bare id.
pnpm build, pnpm lint:quick (0 diagnostics), pnpm typecheck: pass
packages/core vitest:        Test Files 833 passed | 2 skipped (835)   Tests 9991 passed | 11 skipped
packages/core test:repro:    Test Files 25 passed (25)                  Tests 142 passed (142)

@changeset-bot

changeset-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7244167

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

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 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 added review/needs-review No maintainer or bot review yet area/core area/docs labels Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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

@emdashbot emdashbot Bot 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.

The approach is sound. Public URLs are resolved at render time, only when public templates read settings through getSiteSettings(), getSiteSetting() and <Media>. Stored URLs, the REST and admin APIs, and getSiteSettingsWithDb() stay on the internal media route. Local <Media> values are also handled directly, so no database-dependent provider is built with an empty context. Both reviewers approved and found nothing needing changes. They checked request-context propagation through the anonymous, authenticated and scoped-db paths, settings cache isolation, adapter fallbacks when publicUrl is unset, the tests, the changeset and the docs. No extra database queries are added. The review was static; tests, builds and lint were not run.


Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.

@devondragon

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 Oct 19, 2026
@devondragon

Copy link
Copy Markdown
Contributor Author

Recheck

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

This PR is waiting on its author because it has merge conflicts with main, and it has had no activity for 14 days. It will be closed in 7 days unless there is activity: push an update or leave a comment to keep it open.

@github-actions github-actions Bot added stale and removed stale labels Nov 3, 2026
Sites with an R2 custom domain or S3 CDN still served the logo, favicon,
and default social image from getSiteSettings() through the Worker media
route, because the settings reader has no Astro.locals and so never saw
the storage adapter. Middleware now puts the public media URL resolver on
the request context, and the public settings readers rewrite those URLs on
a per-request copy. The cached snapshot, admin API, and stored values keep
the internal route, because the admin persists media URLs into content and
site transfer only rewrites internal paths.

<Media> resolved local media through getMediaProvider(), which builds the
local provider without a database and threw on every render. It now
handles local media like <Image>, <Gallery>, and <Video>, which also makes
it honor publicUrl.
@devondragon
devondragon force-pushed the fix/public-url-site-settings-and-embeds branch from 0e91d75 to 3d35b89 Compare November 3, 2026 15:50
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Nov 4, 2026

@emdashbot emdashbot Bot 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.

The approach is sound. Public media URLs are resolved at render time, so stored URLs, the REST and admin APIs, and getSiteSettingsWithDb() keep the internal media route. Local <Media> values are handled directly, so no database-dependent provider is built with an empty context. Both reviewers approved and found nothing needing changes. They checked request-context propagation across the anonymous, authenticated and scoped-db paths, settings cache isolation, adapter fallbacks when publicUrl is unset, the tests, the changeset and the docs. No extra database queries are added. The review was static; tests, builds and lint were not run.


Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Nov 4, 2026

@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

@emdashbot emdashbot Bot added review/needs-review No maintainer or bot review yet and removed review/needs-review No maintainer or bot review yet labels Nov 4, 2026
@ascorbic
ascorbic enabled auto-merge (squash) November 4, 2026 15:07
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Nov 4, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@4284

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@4284

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@4284

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@4284

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@4284

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@4284

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@4284

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@4284

upgrade-emdash

npm i https://pkg.pr.new/upgrade-emdash@4284

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@4284

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-ai-search

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-search@4284

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@4284

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 3d35b89

@ascorbic
ascorbic merged commit 9aa2259 into emdash-cms:main Nov 4, 2026
39 of 40 checks passed
@emdashbot

emdashbot Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing to EmDash! 🎉

Join the EmDash Discord, then run /link to connect your GitHub account and claim the Contributor role.

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.

2 participants