fix(cf-function): guard against null metadata to prevent 502s#6849
fix(cf-function): guard against null metadata to prevent 502s#6849nicholasgubbins 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
The second invocation is essentially always a "fresh" request from CloudFront's perspective — it's not the same cache key as the original |
|
this is the issue i faced yesterday with #6848 |
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
|
i monkey patched this successfully in static-site.ts - same logic applies to ssr-site.ts as well. Replaced the placeholder origin with S3 + OAC in createDistribution() (starting at line 1168): Before: function createDistribution() {
return new Cdn(
...transform(
args.transform?.cdn,
`${name}Cdn`,
{
comment: `${name} site`,
domain: args.domain,
origins: [
{
originId: "default",
domainName: "placeholder.sst.dev",
customOriginConfig: {
httpPort: 80,
httpsPort: 443,
originProtocolPolicy: "https-only",
originReadTimeout: 20,
originSslProtocols: ["TLSv1.2"],
},
},
],After: function createDistribution() {
const oac = new OriginAccessControl(
`${name}OAC`,
{ name: physicalName(64, name) },
{ parent: self, ignoreChanges: ["name"] },
);
return new Cdn(
...transform(
args.transform?.cdn,
`${name}Cdn`,
{
comment: `${name} site`,
domain: args.domain,
origins: [
{
originId: "default",
domainName: bucketDomain,
originAccessControlId: oac.id,
s3OriginConfig: {
originAccessIdentity: "",
},
},
], |
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