Skip to content

Commit 193015e

Browse files
committed
add new serialization section
1 parent 75d802c commit 193015e

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

src/routes/solid-start/advanced/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"session.mdx",
66
"request-events.mdx",
77
"return-responses.mdx",
8+
"serialization.mdx",
89
"auth.mdx",
910
"websocket.mdx"
1011
]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Serialization
3+
use_cases: >-
4+
server function payloads, data transfer, csp, security, performance
5+
tags:
6+
- serialization
7+
- server-functions
8+
- csp
9+
- security
10+
- performance
11+
version: "1.0"
12+
description: >-
13+
Understand how SolidStart serializes server function payloads, supported
14+
types, and CSP tradeoffs.
15+
---
16+
17+
SolidStart serializes server function arguments and return values so they can travel between server and client. It uses Seroval under the hood and streams payloads to keep responses responsive.
18+
19+
## Configuration
20+
21+
Configure serialization in your `app.config.ts` with `defineConfig`:
22+
23+
```tsx tab title="v1"
24+
import { defineConfig } from "@solidjs/start/config";
25+
26+
export default defineConfig({
27+
serialization: {
28+
mode: "js",
29+
},
30+
});
31+
```
32+
33+
```tsx tab title="v2"
34+
import { defineConfig } from "vite";
35+
import { solidStart } from "@solidjs/start";
36+
37+
export default defineConfig({
38+
plugins: [
39+
solidStart({
40+
serialization: {
41+
mode: "json",
42+
},
43+
}),
44+
],
45+
});
46+
```
47+
48+
See the full config reference in [`defineConfig`](/solid-start/reference/config/define-config#serialization).
49+
50+
## Modes
51+
52+
- `json`: Uses `JSON.parse` on the client. Best for strict CSP because it avoids `eval`. Payloads can be slightly larger.
53+
- `js`: Uses Seroval's JS serializer for smaller payloads and better performance, but it requires `unsafe-eval` in CSP.
54+
55+
:::caution[v2 Breaking Change: Defaults]
56+
SolidStart v1 defaults to `js` for backwards compatibility. SolidStart v2 defaults to `json` for CSP compatibility.
57+
:::
58+
59+
## Supported types (default)
60+
61+
SolidStart enables Seroval plus a default set of web platform plugins. These plugins add support for:
62+
63+
- `AbortSignal`, `CustomEvent`, `DOMException`, `Event`
64+
- `FormData`, `Headers`, `ReadableStream`
65+
- `Request`, `Response`
66+
- `URL`, `URLSearchParams`
67+
68+
Seroval supports additional value types. The compatibility list is broader than what SolidStart enables by default, so treat it as a superset. See the [Seroval compatibility docs](https://github.com/lxsmnsyc/seroval/blob/main/docs/COMPATIBILITY.md).
69+
70+
## Limits and exclusions
71+
72+
- `RegExp` is disabled by default.
73+
- JSON mode enforces a maximum serialization depth of 64. If you exceed this, flatten the structure or return a simpler payload.
74+
75+
## Related guidance
76+
77+
- Configure modes and defaults in [`defineConfig`](/solid-start/reference/config/define-config#serialization).
78+
- CSP implications and nonce examples live in the [Security guide](/solid-start/guides/security#content-security-policy-csp).

src/routes/solid-start/guides/security.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tags:
1111
- csp
1212
- middleware
1313
- protection
14-
version: '1.0'
14+
version: "1.0"
1515
description: >-
1616
Secure your SolidStart apps against XSS, CSRF attacks. Configure CSP headers,
1717
CORS policies, and implement security best practices.
@@ -38,7 +38,7 @@ It is highly recommended to read the [Cross Site Scripting Prevention Cheat Shee
3838

3939
To configure the `Content-Security-Policy` HTTP header, a [middleware](/solid-start/advanced/middleware) can be used.
4040

41-
If you enforce a strict CSP, configure SolidStart to use JSON serialization mode to avoid `unsafe-eval` requirements. See [defineConfig serialization](/solid-start/reference/config/define-config#serialization). Note that `'unsafe-eval'` is only required when using `serialization.mode: "js"`, and the nonce-based CSP example below assumes this mode.
41+
If you enforce a strict CSP, configure SolidStart to use JSON serialization mode to avoid `unsafe-eval` requirements. See [defineConfig serialization](/solid-start/reference/config/define-config#serialization). Note that `unsafe-eval` is only required for `serialization.mode: "js"`.
4242

4343
### With nonce (recommended)
4444

@@ -62,7 +62,7 @@ export default createMiddleware({
6262

6363
const csp = `
6464
default-src 'self';
65-
script-src 'nonce-${nonce}' 'strict-dynamic' 'unsafe-eval';
65+
script-src 'nonce-${nonce}' 'strict-dynamic';
6666
object-src 'none';
6767
base-uri 'none';
6868
frame-ancestors 'none';

src/routes/solid-start/reference/config/define-config.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ export default defineConfig({
8181
- SolidStart v1 defaults to `js` for backwards compatibility.
8282
- SolidStart v2 defaults to `json` for CSP compatibility.
8383

84-
Seroval defines the supported value types and edge cases. See the full list in the [Seroval compatibility docs](https://github.com/lxsmnsyc/seroval/blob/main/docs/COMPATIBILITY.md).
84+
### Supported types (default)
85+
86+
SolidStart enables Seroval plus a default set of web platform plugins. These plugins add support for:
87+
88+
- `AbortSignal`, `CustomEvent`, `DOMException`, `Event`
89+
- `FormData`, `Headers`, `ReadableStream`
90+
- `Request`, `Response`
91+
- `URL`, `URLSearchParams`
92+
93+
Seroval supports additional value types. The compatibility list is broader than what SolidStart enables by default, so treat it as a superset. See the full list in the [Seroval compatibility docs](https://github.com/lxsmnsyc/seroval/blob/main/docs/COMPATIBILITY.md).
8594

8695
## Configuring Nitro
8796

src/routes/solid-start/reference/server/use-server.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ The data for the redirected page is fetched and streamed to the client in the sa
9898
## Serialization
9999

100100
Server functions allow the serialization of many different data types in the response, using the Seroval serializer.
101-
The full list is available [in Seroval's source code](https://github.com/lxsmnsyc/seroval/blob/main/docs/compatibility.md#supported-types).
101+
For supported types, defaults, and CSP tradeoffs, see the [Serialization guide](/solid-start/advanced/serialization).
102102
Configure serialization mode and CSP behavior in [`defineConfig`](/solid-start/reference/config/define-config#serialization).
103103

104104
## Meta information

0 commit comments

Comments
 (0)