Skip to content

Commit b640049

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 b640049

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

.changeset/blue-dots-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Fixed an issue where runtimes like AWS Lambda would default to `application/json` when no header was present, causing parsing errors in `json` serialization mode. To ensure consistent behavior, the `Content-Type` is now explicitly set to `text/plain`.

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)