Skip to content

Commit 7ec3637

Browse files
authored
[Web] Use null Response body when Playground responde with a null body status code (#2314)
# Description Ensures the service worker uses a null body when constructing a `new Response()` with one of the [null body status codes](https://fetch.spec.whatwg.org/#statuses). ## Rationale [WooCommerce failed in Playground](woocommerce/woocommerce#58718) after skipping the setup wizard with this error: > utils.ts:91 Uncaught (in promise) TypeError: Failed to construct 'Response': Response with null body status cannot have body Turns out, the [fetch() spec defines](https://fetch.spec.whatwg.org/#statuses) something called a "null body status" which are status codes 101, 103, 204, 205, and 304. When `new Response()` is created with one of these codes, the body must be null. The offending request was: /wp-json/wc-admin/onboarding/profile/update-store-currency-and-measurement-units?_locale=user Request Method: POST Status Code: 204 No Content (from service worker) # Testing instructions * Go to http://127.0.0.1:5400/website-server/?plugin=woocommerce * Go to wp-admin * Skip the onboarding wizard * Click the CTA to launch the store * Confirm the flow doesn't get stuck on the "turning on the lights" screen but actually finishes cc @nerrad
1 parent d6df50e commit 7ec3637

File tree

1 file changed

+13
-2
lines changed
  • packages/php-wasm/web-service-worker/src

1 file changed

+13
-2
lines changed

packages/php-wasm/web-service-worker/src/utils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,19 @@ export async function convertFetchEventToPHPRequest(event: FetchEvent) {
8787
phpResponse.httpStatusCode
8888
);
8989
}
90-
91-
return new Response(phpResponse.bytes, {
90+
/**
91+
* Make sure we don't pass an actual body string to new Response()
92+
* if the status is a null body status (101, 103, 204, 205, or 304).
93+
* new Response() throws a TypeError in that case, as the fetch() spec
94+
* requires.
95+
*
96+
* @see https://fetch.spec.whatwg.org/#statuses
97+
*/
98+
const isNullBodyCode = [101, 103, 204, 205, 304].includes(
99+
phpResponse.httpStatusCode
100+
);
101+
const responseBody = isNullBodyCode ? null : phpResponse.bytes;
102+
return new Response(responseBody, {
92103
headers: phpResponse.headers,
93104
status: phpResponse.httpStatusCode,
94105
});

0 commit comments

Comments
 (0)