Skip to content

Commit c2bd673

Browse files
fix: explicitly set Content-Type to text/plain for json serialization mode
Serialization mode:"json" uses Seroval special serialization format that is not valid JSON. Some runtimes (e.g. AWS Lambda) default to `application/json` when no `Content-Type` is set, causing downstream `JSON.parse` errors such as: `SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.` When using "json" serialization mode, explicitly set `Content-Type` to `text/plain` for server-function responses to prevent incorrect parsing by runtimes. This restores the 2.x behavior, is safer for downstream environments, and is recommended best practice.
1 parent c8e0697 commit c2bd673

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ async function handleServerFunction(h3Event: HTTPEvent) {
154154
setHeader(h3Event, "content-type", "text/javascript");
155155
return serializeToJSStream(instance, result);
156156
}
157+
// Explicitly set the Content-Type to avoid runtimes (e.g., AWS Lambda)
158+
// that default to `application/json`, which can break serialization
159+
// when the SEROVAL output is not valid JSON.
160+
setHeader(h3Event, "content-type", "text/plain");
157161
return serializeToJSONStream(result);
158162
} catch (x) {
159163
if (x instanceof Response) {
@@ -181,6 +185,10 @@ async function handleServerFunction(h3Event: HTTPEvent) {
181185
setHeader(h3Event, "content-type", "text/javascript");
182186
return serializeToJSStream(instance, x);
183187
}
188+
// Explicitly set the Content-Type to avoid runtimes (e.g., AWS Lambda)
189+
// that default to `application/json`, which can break serialization
190+
// when the SEROVAL output is not valid JSON.
191+
setHeader(h3Event, "content-type", "text/plain");
184192
return serializeToJSONStream(x);
185193
}
186194
return x;

0 commit comments

Comments
 (0)