You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
@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?
Comparing dataCenter430:fix/serve-public-assets-under-base-path (1cd3d8d) with main (325901e)1
Footnotes
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. ↩
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pkg: astroRelated to the core `astro` package (scope)
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Fixes #15914: public assets in
astro devare now served under the configuredbasepath when using the@astrojs/cloudflareadapter.Problem
With
base: "/docs/"and the Cloudflare adapter enabled:/docs/test.svg.GET /docs/test.svgreturns 404.GET /test.svgreturns 200.Without the adapter,
/docs/test.svgworks as expected. The bug is adapter-specific.Root cause
Astro’s dev server uses custom middleware (
baseMiddleware, etc.) to strip thebaseprefix from requests before Vite’s static middleware serves files frompublic/. That middleware is only registered when at least one runnable SSR/prerender environment exists.With
@astrojs/cloudflare, the SSR environment is a workerd-backedCloudflareDevEnvironment, which is not “runnable” in Node. SoconfigureServerreturned early and never registered the middleware stack. As a result, requests like/docs/test.svgreached Vite with the base prefix intact, and Vite looked forpublic/docs/test.svg→ 404.Solution
configureServerand always register the core dev middleware (baseMiddleware,trailingSlashMiddleware,routeGuardMiddleware,secFetchMiddleware), regardless of whether the SSR/prerender environments are runnable.ssrHandler/prerenderHandler, so Cloudflare’s workerd still handles those requests; only the base-path and security middleware are now applied in all cases.unhandledRejectionlistener is only registered when at least one runnable handler exists, to avoid attaching it when no handlers are present.Testing
base: "/docs/").GET /docs/test.svgreturns 200 whenpublic/test.svgexists.astro devwithout the Cloudflare adapter still behaves as before.