Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/yellow-ladybugs-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-storefront/sdk": patch
---

**[FIXED]** `Content-Length` header will no longer be appended to requests if its value is "0" - such situation caused Next.js server to crash
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,67 @@ describe("middlewareModule", () => {
);
});

it("should remove zero-valued Content-Length header as it would crash Next.js server", async () => {
const customHttpClient = jest.fn();
const sdkConfig = {
commerce: buildModule(middlewareModule<Endpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: "commit-hash",
httpClient: customHttpClient,
defaultRequestConfig: {
headers: {
"Content-Length": "0",
},
},
}),
};
const sdk = initSDK(sdkConfig);

await sdk.commerce.getProduct({ id: 1 });

expect(customHttpClient).toHaveBeenCalledWith(
"http://localhost:8181/commerce/getProduct",
expect.any(Array),
expect.objectContaining({
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
);
});

it("should pass non-zero-valued Content-Length header", async () => {
const customHttpClient = jest.fn();
const sdkConfig = {
commerce: buildModule(middlewareModule<Endpoints>, {
apiUrl: "http://localhost:8181/commerce",
cdnCacheBustingId: "commit-hash",
httpClient: customHttpClient,
defaultRequestConfig: {
headers: {
"Content-Length": "1000",
},
},
}),
};
const sdk = initSDK(sdkConfig);

await sdk.commerce.getProduct({ id: 1 });

expect(customHttpClient).toHaveBeenCalledWith(
"http://localhost:8181/commerce/getProduct",
expect.any(Array),
expect.objectContaining({
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Content-Length": "1000",
},
})
);
});

it("should use different base URL during SSR if defined", async () => {
const customHttpClient = jest.fn();
const sdkConfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,21 @@ export const getRequestSender = (options: Options): RequestSender => {
Accept: "application/json",
...defaultRequestConfig.headers,
};
const mergedHeaders = {
...defaultHeaders,
...methodHeaders,
...headers,
};
const mergedHeaders = Object.fromEntries(
Object.entries({
...defaultHeaders,
...methodHeaders,
...headers,
}).reduce<[string, string | string[]][]>(
(headersSoFar, [key, value]) =>
// Removing Content-Lenght header if it equals 0 because it would crash Next.js server
// eslint-disable-next-line eqeqeq
key.toLocaleLowerCase() === "content-length" && value == "0"
? headersSoFar
: [...headersSoFar, [key, value]],
[]
)
);

const computedHeaders: ComputedConfig["headers"] = {};
Object.entries(mergedHeaders).forEach(([key, value]) => {
Expand Down