Skip to content

fix: hydration for null body http status response #13985

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/kit/src/runtime/server/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NULL_BODY_STATUS = [101, 103, 204, 205, 304];
11 changes: 9 additions & 2 deletions packages/kit/src/runtime/server/page/load_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { disable_search, make_trackable } from '../../../utils/url.js';
import { validate_depends } from '../../shared.js';
import { b64_encode } from '../../utils.js';
import { with_event } from '../../app/server/event.js';
import { NULL_BODY_STATUS } from '../constants.js';

/**
* Calls the user's server `load` function.
Expand Down Expand Up @@ -278,7 +279,7 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts)
const proxy = new Proxy(response, {
get(response, key, _receiver) {
/**
* @param {string} body
* @param {string | undefined} body
* @param {boolean} is_b64
*/
async function push_fetched(body, is_b64) {
Expand Down Expand Up @@ -325,6 +326,11 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts)
async function text() {
const body = await response.text();

if (NULL_BODY_STATUS.includes(response.status) && typeof body === 'string' && body === '') {
await push_fetched(undefined, false);
return undefined;
}

if (!body || typeof body === 'string') {
await push_fetched(body, false);
}
Expand All @@ -342,7 +348,8 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts)

if (key === 'json') {
return async () => {
return JSON.parse(await text());
const body = await text();
return body ? JSON.parse(body) : undefined;
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface Fetched {
method: string;
request_body?: string | ArrayBufferView | null;
request_headers?: HeadersInit | undefined;
response_body: string;
response_body: string | undefined;
response: Response;
is_b64?: boolean;
}
Expand Down
Loading