Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextResponse: add JSON support #30275

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/next/server/web/spec-extension/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ export class NextResponse extends Response {
url?: NextURL
}

constructor(body?: BodyInit | null, init: ResponseInit = {}) {
constructor(body?: BodyInit | object | null, init: ResponseInit = {}) {
if (body && typeof body === 'object' && !Buffer.isBuffer(objet)) {
body = JSON.stringify(body)
init.headers = {
Copy link
Member

@TooTallNate TooTallNate Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can use spread operator here. Consider when init.headers is a Headers instance already. Should be something like:

const headers = new Headers(init.headers);
headers.set('content-type', ...);
init.headers = headers;

Copy link

@weyert weyert Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be !Buffer.isBuffer(object)? Not the missing c in objet

...init.headers,
'content-type': 'application/json; charset=utf-8',
}
}

super(body, init)

const cookieParser = () => {
Expand Down