Repository navigation
fix(@astrojs/node): return null from resolveClientDir instead of throwing when server dir not found - #16807
fix(@astrojs/node): return null from resolveClientDir instead of throwing when server dir not found#16807huextrat wants to merge 3 commits into
Conversation
…wing when server dir not found Firebase Hosting flattens dist/server/ contents to a root directory, removing the "server" path segment at runtime. resolveClientDir() then threw a fatal error at module init time, crashing Cloud Functions with a 503 before any request was handled. Return null instead of throwing, and guard prerenderedErrorPageFetch and the static handler to skip disk-based features gracefully when client dir cannot be resolved. Fixes withastro#16806 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
🦋 Changeset detectedLatest commit: ef4fcdd The changes in this PR will be included in the next version bump. This PR includes changesets to release 35 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 |
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
matthewp
left a comment
There was a problem hiding this comment.
I don't believe this is the right approach to fixing this problem. It's taking a side-effect of how Firebase is building and using that to alter how the Node adapter works; not serving static files for example is a majorly unexpected change here.
I don't know how to fix this problem. I could see an option perhaps, but also why does the Firebase adapter do this in the first place and can it be configured not to?
Thanks @matthewp to taking a look into this From my POV Firebase's flat copy is intentional and not configurable :/ The behavior is hardcoded in await copy(join(sourceDir, outDir, "server"), join(destDir));There is no firebase.json option, no CLI flag. This is the defined contract of the ɵcodegenFunctionsDirectory API. SvelteKit has the exact same flat copy pattern On the static files concern: on Firebase Hosting, static files are served by the CDN layer, not by the Node adapter — Firebase only routes SSR requests to Cloud Functions. So serve-static falling through to SSR when client is null has no real-world impact on Firebase users. The static files are already being served by Hosting before the request ever reaches the adapter. That said, I'm open to a different approach if you have one in mind — for example an explicit clientDir option to override the path resolution. Happy to go that route instead. |
|
But that code is open-source too: https://github.com/firebase/firebase-tools/ It's fine if Firebase doesn't need the client/ folder, but flattening is unexpected by the Node adapter. Why should the fix not be applied there? Doesn't this mean that Astro is broken in Firebase at the moment entirely? Seems like they'd want to fix that, if that's the case. |
|
Looks like you opened a PR there, closing. |
We'll see if they're okay with this change. If not, I'll leave it to you and the firebase maintainers to find a solution that works for everyone. |
Summary
@astrojs/nodemiddleware crashes on startup when deployed to Firebase (503) #16806Firebase Hosting (with the
webframeworksexperiment) copies the contents ofdist/server/to the Cloud Functions root — theserver/directory nameis not preserved. At runtime,
import.meta.urlinside the server chunk isfile:///workspace/chunks/server_XYZ.mjs— noserversegment in the path.resolveClientDir()inpackages/integrations/node/src/shared.tswalked up fromimport.meta.urllooking for a directory namedserver. When it couldn'tfind it, it threw a fatal error at module initialization time, crashing the Cloud Function before any request was handled (503 on every request).
Changes
shared.ts—resolveClientDirreturnsstring | nullinstead of throwing when the server directory segment is not found in the runtime pathserve-app.ts—prerenderedErrorPageFetchis set toundefinedwhenclientisnull;app.renderalready acceptsprerenderedErrorPageFetch?: ...so this is a no-op fallback, not a regression
serve-static.ts— static handler falls through to SSR whenclientisnulltest/units/resolve-client-dir.test.ts— updated to assertnullreturn instead of a thrown errorOn CDN-backed deployments like Firebase Hosting, static files (including prerendered 404/500 pages) are served by the CDN layer — skipping disk-based error
pages in that case is the correct graceful fallback.
Testing
Existing unit test in
test/units/resolve-client-dir.test.tsupdated and passing. The test simulates the Firebase scenario:import.meta.urlat runtimedoes not contain the expected
serverpath segment, andresolveClientDirnow returnsnullinstead of throwing.Docs
No docs update needed — this is a bug fix for a crash at startup. The feature behavior (disk-based prerendered error pages) is unchanged for standard
deployments where the
server/directory segment is present at runtime.