Skip to content

fix(@astrojs/node): return null from resolveClientDir instead of throwing when server dir not found - #16807

Closed
huextrat wants to merge 3 commits into
withastro:mainfrom
huextrat:fix/node-firebase-503-resolveClientDir
Closed

huextrat wants to merge 3 commits into
withastro:mainfrom
huextrat:fix/node-firebase-503-resolveClientDir

Conversation

@huextrat

Copy link
Copy Markdown

Summary

Firebase Hosting (with the webframeworks experiment) copies the contents of dist/server/ to the Cloud Functions root — the server/ directory name
is not preserved. At runtime, import.meta.url inside the server chunk is file:///workspace/chunks/server_XYZ.mjs — no server segment in the path.

resolveClientDir() in packages/integrations/node/src/shared.ts walked up from import.meta.url looking for a directory named server. When it couldn't
find 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 — resolveClientDir returns string | null instead of throwing when the server directory segment is not found in the runtime path
  • serve-app.ts — prerenderedErrorPageFetch is set to undefined when client is null; app.render already accepts prerenderedErrorPageFetch?: ...
    so this is a no-op fallback, not a regression
  • serve-static.ts — static handler falls through to SSR when client is null
  • test/units/resolve-client-dir.test.ts — updated to assert null return instead of a thrown error

On 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.ts updated and passing. The test simulates the Firebase scenario: import.meta.url at runtime
does not contain the expected server path segment, and resolveClientDir now returns null instead 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.

…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-bot

changeset-bot Bot commented May 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ef4fcdd

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

This PR includes changesets to release 35 packages
Name Type
@astrojs/node Patch
@e2e/actions-blog Patch
@e2e/actions-react-19 Patch
@e2e/csp-server-islands Patch
@e2e/i18n Patch
@e2e/server-islands-key Patch
@e2e/server-islands Patch
@e2e/view-transitions Patch
@test/cache-route Patch
@test/client-address-node Patch
@test/live-loaders Patch
@test/request-signal Patch
@test/ssr-api-route Patch
@test/static-build-ssr Patch
@test/nodejs-api-route Patch
@test/nodejs-badurls Patch
@test/nodejs-encoded Patch
@test/nodejs-errors Patch
@test/nodejs-headers Patch
@test/nodejs-image Patch
@test/locals Patch
@test/node-middleware Patch
@test/nodejs-prerender-404-500 Patch
@test/nodejs-prerender Patch
@test/nodejs-prerendered-error-page-fetch Patch
@test/nodejs-preview-headers Patch
@test/redirects Patch
@test/node-sessions Patch
@test/ssr-assets-middleware Patch
@test/node-static-headers Patch
@test/node-trailingslash Patch
@test/url Patch
@test/well-known-locations Patch
@test/sitemap-ssr Patch
astro-benchmark 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 the pkg: integration Related to any renderer integration (scope) label May 20, 2026
@huextrat
huextrat requested a review from matthewp May 20, 2026 14:04

@matthewp matthewp 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.

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?

@huextrat

huextrat commented May 20, 2026 •

Copy link
Copy Markdown
Author

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 firebase-tools/src/frameworks/astro/index.ts:

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 firebase-tools/src/frameworks/sveltekit/index.ts does the same thing.

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.

@huextrat
huextrat requested a review from matthewp May 20, 2026 14:22
@matthewp

Copy link
Copy Markdown
Contributor

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.

@matthewp

Copy link
Copy Markdown
Contributor

Looks like you opened a PR there, closing.

@matthewp matthewp closed this May 20, 2026
@huextrat

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@astrojs/node middleware crashes on startup when deployed to Firebase (503)

2 participants