fix(modernjs-v3): compute Content-Length in bytes for served bundles - #4982
Open
scplay wants to merge 1 commit into
Open
fix(modernjs-v3): compute Content-Length in bytes for served bundles#4982scplay wants to merge 1 commit into
scplay wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: ef59c66 The changes in this PR will be included in the next version bump. This PR includes changesets to release 48 packages
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 |
3 tasks
Contributor
Overlap with #4983#4983 includes the same
Once #4983 is merged, this PR can be closed as superseded. |
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.
fix(modernjs-v3): compute Content-Length in bytes for served bundles
Summary
createStaticMiddlewareserves node-side federated chunks from/bundles. The file is read as a UTF-8 string byfileCache.getFile, butContent-Lengthis set fromcontent.length, which is the number of characters:For any chunk containing non-ASCII characters (comments, string literals), the byte length exceeds the character length, so the response is truncated by exactly that difference. An SSR consumer then evaluates an incomplete chunk and throws.
Fixed by using
Buffer.byteLength(fileResult.content).Impact
Only affects production (
modern serve) with SSR enabled — the middleware returns early whenNODE_ENV === 'development', and it is only registered whenserver.ssris set. Chunks that are pure ASCII are unaffected because byte length equals character length, which is why this went unnoticed.Deployments that serve remote assets via CDN or nginx do not hit this middleware at all.
Reproduction
A Modern.js producer with SSR enabled that exposes a module whose chunk contains non-ASCII characters, consumed by a Modern.js SSR host.
The chunk on disk is valid; the served copy is not:
Byte/character mismatch matches the truncation exactly:
A minimal probe shows the same off-by-multibyte for any file, not just federated chunks — a 46-byte file with non-ASCII content is served as 38 bytes.
Changes
packages/modernjs-v3/src/server/staticMiddleware.ts— useBuffer.byteLengthforContent-Lengthpackages/modernjs-v3/src/server/staticMiddleware.spec.ts— cover non-ASCII and ASCII contentNotes
fileCache.tshas a related fallback,size: stat.size || content.length, which mixes the same two units. It is only reached whenstat.sizeis falsy, so it is left alone here to keep this PR to a single issue. Happy to fix it separately if you'd prefer.Test plan
pnpm --filter @module-federation/modern-js-v3 run testThe added non-ASCII test fails before the fix and passes after. Verified end to end against a Modern.js SSR host consuming a producer whose chunks contain non-ASCII characters: the served chunk now passes
node --checkand the remote module renders on the server.