Skip to content

Commit a67dfdd

Browse files
committed
Fix client-server comms
1 parent 9480de8 commit a67dfdd

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

packages/start/src/server/server-functions-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function handleServerFunction(h3Event: H3Event) {
6060
contentType?.startsWith("application/x-www-form-urlencoded")
6161
) {
6262
parsed.push(await event.request.formData());
63-
} else {
63+
} else if (contentType?.startsWith('text/plain')) {
6464
parsed = (await deserializeJSONStream(event.request.clone())) as any[];
6565
}
6666
}
@@ -96,11 +96,11 @@ export async function handleServerFunction(h3Event: H3Event) {
9696
// handle no JS success case
9797
if (!instance) return handleNoJS(result, request, parsed);
9898

99+
h3Event.res.headers.set("x-serialized", "true");
99100
if (import.meta.env.SEROVAL_MODE === "js") {
100101
h3Event.res.headers.set("content-type", "text/javascript");
101102
return serializeToJSStream(instance, result);
102103
}
103-
h3Event.res.headers.set("content-type", "text/plain");
104104
return serializeToJSONStream(result);
105105
} catch (x) {
106106
if (x instanceof Response) {

packages/start/src/server/server-runtime.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ async function fetchServerFunction(
4747
})
4848
: createRequest(base, id, instance, {
4949
...options,
50-
body: serializeToJSONStream(args),
51-
headers: { ...options.headers, "Content-Type": "application/json" },
50+
// TODO(Alexis): move to serializeToJSONStream
51+
body: await serializeToJSONString(args),
52+
// duplex: 'half',
53+
// body: serializeToJSONStream(args),
54+
headers: { ...options.headers, "Content-Type": "text/plain" },
5255
}));
5356

5457
if (
@@ -70,14 +73,16 @@ async function fetchServerFunction(
7073

7174
const contentType = response.headers.get("Content-Type");
7275
let result;
73-
if (contentType && contentType.startsWith("text/plain")) {
76+
if (contentType?.startsWith("text/plain")) {
7477
result = await response.text();
75-
} else if (contentType && contentType.startsWith("application/json")) {
78+
} else if (contentType?.startsWith("application/json")) {
7679
result = await response.json();
77-
} else if (import.meta.env.SEROVAL_MODE === "js") {
78-
result = await deserializeJSStream(instance, response);
79-
} else {
80-
result = await deserializeJSONStream(response);
80+
} else if (response.headers.get('x-serialized')) {
81+
if (import.meta.env.SEROVAL_MODE === "js") {
82+
result = await deserializeJSStream(instance, response);
83+
} else {
84+
result = await deserializeJSONStream(response);
85+
}
8186
}
8287
if (response.headers.has("X-Error")) {
8388
throw result;

0 commit comments

Comments
 (0)