🐛 stop proxy from serving decompressed HTML as gzip#487
Merged
Conversation
The proxy reads an upstream response with `response.text()`, which fetch transparently decompresses, then rebuilds the response from the rewritten HTML. It copied the upstream headers verbatim onto the new body — including `content-encoding: gzip` and the stale `content-length` — so it served plain HTML labelled as gzip. Clients (staticalize, browsers) then failed to decode it with `Invalid gzip header`, which broke the deploy for every proxied /effection, /interactors and /graphgen page. Drop content-encoding/content-length/transfer-encoding when copying headers for a rebuilt body and let the server recompute them.
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.
Motivation
The
deployworkflow was failing at the Staticalize step with an uncaughtTypeError: Invalid gzip header. Running the (newly improved) staticalize against the live local site pinpointed it: 259 pages OK, but 349 failures — every oneInvalid gzip header, all confined to the proxied doc sub-sites/effection/*,/interactors/*,/graphgen/*.Root cause is in
routes/proxy-route.ts. For HTML responses the proxy reads the upstream body withresponse.text()— whichfetch()has already decompressed — rewrites the HTML, and rebuilds theResponsefrom the plain-text result. But it copied the upstream headers verbatim onto the new body, includingcontent-encoding: gzipand the stalecontent-length. So it served uncompressed HTML labelled as gzip, and any client (staticalize, browsers) failed to decode it.Assets went through the untouched
return responsepass-through, which Deno serves correctly — which is why only the rewritten HTML pages broke.Approach
Add a
copyHeaders()helper that copies an upstream response's headers while dropping the ones that describe the original wire framing —content-encoding,content-length,transfer-encoding— and let the server recompute them for the rebuilt body. Applied in both the HTML-rewrite branch and the redirect branch.Verification
Ran the site locally with the fix and re-ran staticalize exactly as CI does:
259 pages, 349 errors, exit 1608 pages, 322 assets, 0 errors, exit 0/effection/now responds withoutcontent-encoding: gzipand with a correctcontent-length, and the body decodes as HTML.The staticalize error-reporting improvement (staticalize#8) is what surfaced the offending URLs but is not required for this fix.