Skip to content

fix: serve public assets under base path with Cloudflare adapter#15947

Merged
matthewp merged 1 commit intowithastro:mainfrom
dataCenter430:fix/serve-public-assets-under-base-path
Mar 16, 2026
Merged

fix: serve public assets under base path with Cloudflare adapter#15947
matthewp merged 1 commit intowithastro:mainfrom
dataCenter430:fix/serve-public-assets-under-base-path

Conversation

@dataCenter430
Copy link
Contributor

Changes

Fixes #15914: public assets in astro dev are now served under the configured base path when using the @astrojs/cloudflare adapter.

Problem

With base: "/docs/" and the Cloudflare adapter enabled:

  • The page correctly renders URLs like /docs/test.svg.
  • GET /docs/test.svg returns 404.
  • GET /test.svg returns 200.

Without the adapter, /docs/test.svg works as expected. The bug is adapter-specific.

Root cause

Astro’s dev server uses custom middleware (baseMiddleware, etc.) to strip the base prefix from requests before Vite’s static middleware serves files from public/. That middleware is only registered when at least one runnable SSR/prerender environment exists.

With @astrojs/cloudflare, the SSR environment is a workerd-backed CloudflareDevEnvironment, which is not “runnable” in Node. So configureServer returned early and never registered the middleware stack. As a result, requests like /docs/test.svg reached Vite with the base prefix intact, and Vite looked for public/docs/test.svg → 404.

Solution

  • Always return the post-hook from configureServer and always register the core dev middleware (baseMiddleware, trailingSlashMiddleware, routeGuardMiddleware, secFetchMiddleware), regardless of whether the SSR/prerender environments are runnable.
  • SSR/prerender request handlers remain conditional on ssrHandler / prerenderHandler, so Cloudflare’s workerd still handles those requests; only the base-path and security middleware are now applied in all cases.
  • The unhandledRejection listener is only registered when at least one runnable handler exists, to avoid attaching it when no handlers are present.

Testing

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Mar 16, 2026
@changeset-bot
Copy link

changeset-bot bot commented Mar 16, 2026

⚠️ No Changeset found

Latest commit: 1cd3d8d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

@matthewp
Copy link
Contributor

@dataCenter430 is this with prerenderEnvironment: 'node'?

@dataCenter430
Copy link
Contributor Author

dataCenter430 commented Mar 16, 2026

@dataCenter430 is this with prerenderEnvironment: 'node'?

No. The bug shows up with the default setup (workerd for both SSR and prerender). With prerenderEnvironment: 'node' you get a runnable prerender environment, so the old code did register the middleware and the bug typically didn’t appear.

Also, the CI test failure (CI/test integration) does not seem to be related to my code changes.
Should I fix the issue to get a PR merged?

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 16, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing dataCenter430:fix/serve-public-assets-under-base-path (1cd3d8d) with main (325901e)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (85060cd) during the generation of this report, so 325901e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@matthewp matthewp merged commit f5cc2b0 into withastro:main Mar 16, 2026
43 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@astrojs/cloudflare dev server does not serve public assets under base path

2 participants