Skip to content

Commit

Permalink
Set "content-length" header in Response.json()
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Mar 11, 2024
1 parent 4a02167 commit 46ecc02
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rare-files-speak.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Set "content-length" header in `Response.json()`
5 changes: 4 additions & 1 deletion packages/runtime/src/fetch/response.ts
@@ -1,6 +1,7 @@
import { def } from '../utils';
import { Body, type BodyInit } from './body';
import { Headers, type HeadersInit } from './headers';
import { encoder } from '../polyfills/text-encoder';
import type { URL } from '../polyfills/url';

export interface ResponseInit {
Expand Down Expand Up @@ -102,7 +103,9 @@ export class Response extends Body implements globalThis.Response {
if (!headers.has('content-type')) {
headers.set('content-type', 'application/json');
}
return new Response(JSON.stringify(data), { ...init, headers });
const json = encoder.encode(JSON.stringify(data));
headers.set('content-length', String(json.length));
return new Response(json, { ...init, headers });
}
}
def(Response);

0 comments on commit 46ecc02

Please sign in to comment.