fix(cf-function): guard against null metadata to prevent 502s#6849
Open
nicholasgubbins wants to merge 1 commit into
Open
fix(cf-function): guard against null metadata to prevent 502s#6849nicholasgubbins wants to merge 1 commit into
nicholasgubbins wants to merge 1 commit into
Conversation
When the KV store returns null/undefined for the metadata key — which can happen transiently during deployment as the KV entry propagates to edge nodes — routeSite throws an uncaught TypeError accessing metadata.base, causing CloudFront to return a 502. This is most visible when errorPage is configured, because that disables the function-level custom404 fallback in favour of distribution-level customErrorResponses. The 502 manifests inconsistently because only some edge nodes are affected during the propagation window. The fix fails open: if metadata is unavailable, pass the request through to the default origin unchanged rather than crashing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
7ae7cfd to
7ed9da2
Compare
Contributor
Author
The second invocation is essentially always a "fresh" request from CloudFront's perspective — it's not the same cache key as the original |
Contributor
Author
|
this is the issue i faced yesterday with #6848 |
vimtor
reviewed
May 13, 2026
Collaborator
vimtor
left a comment
There was a problem hiding this comment.
i don’t think this fully fixes the 502. when metadata is missing, returning event.request still sends the request to the default origin, which is placeholder.sst.dev
for standalone sites, that origin does not resolve, so CloudFront still returns 502 Failed to contact the origin instead of the intended site/error response
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
null/undefinedfor the metadata key — which can happen transiently as the KV entry propagates to edge nodes after a deployment —routeSitethrows an uncaughtTypeErroraccessingmetadata.base, causing CloudFront to return a 502.static-site.tsandssr-site.ts.Root cause
The issue is most visible when
errorPageis configured on aStaticSite. WhenerrorPageis set, SST storeserrorResponseCode: 404in the KV metadata and omitscustom404, disabling the function-level SPA fallback in favour of distribution-levelcustomErrorResponses. This means unmatched requests go through an extra round-trip (original request → S3 403 → CloudFront fetches error page → function invoked again), doubling the exposure to the KV propagation window and making the 502 appear inconsistently depending on which edge node handles the request.Test plan
StaticSitewitherrorPageset, request a non-existent path — should return 404 with the error page, not 502🤖 Generated with Claude Code