diff --git a/.changeset/rare-files-speak.md b/.changeset/rare-files-speak.md new file mode 100644 index 00000000..aac924cd --- /dev/null +++ b/.changeset/rare-files-speak.md @@ -0,0 +1,5 @@ +--- +"nxjs-runtime": patch +--- + +Set "content-length" header in `Response.json()` diff --git a/packages/runtime/src/fetch/response.ts b/packages/runtime/src/fetch/response.ts index 4f7cfe29..5e86fead 100644 --- a/packages/runtime/src/fetch/response.ts +++ b/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 { @@ -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);