fix: replace FileReader with Blob.text() in Angular blobToText for SSR compatibility#5363
Open
PeterTheOne wants to merge 2 commits intoRicoSuter:masterfrom
Open
Conversation
FileReader is a browser-only API that crashes in Node.js environments (Angular SSR / Universal). Blob.text() achieves the same result and is available in all modern browsers (Chrome 76+, Firefox 69+, Safari 14.1+) and Node.js 16+. Fixes RicoSuter#3177 Related: RicoSuter#1692 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
f68d5e6 to
81383a9
Compare
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.
Problem
The generated Angular
blobToTextfunction usesFileReader, which is a browser-only DOM API. This causes crashes in any non-browser Angular environment:ReferenceError: FileReader is not definedon every API call, since NSwag setsresponseType: 'blob'for all requests and every response (including error bodies) is routed throughblobToTextFileReader.onloadevent is undefined in NativeScript's runtime (see blobToText in NativeScript Angular -> FileReader event is undefined #3177)FileReadermay not be available or may behave differentlyBecause
blobToTextsits in the hot path of every generatedprocessXxxmethod (both success and error branches), there is no easy way to work around this without patching the generated output or adding layered interceptors that fight each other.Fix
Replace
FileReaderwithBlob.text(), which is:The change is in
File.Utilities.liquid, Angular block only — the AngularJSblobToTextis unchanged:All 8 affected Angular snapshot files have been updated accordingly.
Verification
I have applied this fix locally in an Angular SSR project (Node 18) and the crash is resolved. However I want to be upfront: real-world testing has been limited — I haven't verified all edge cases like large blobs, binary responses, or error body handling across different Angular versions. Additional review and testing from maintainers or other users would be very welcome before merging.
Related issues
Fixes #3177
Related: #1692
Drafted with the help of Claude Code. The root cause analysis, template change, and snapshot updates were done with AI assistance — flagging this for transparency.