Skip to content

Commit e1dcb05

Browse files
atilafassinakodiakhq[bot]Copilot
authored
add docs about serialization modes (#1436)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 891ecb2 commit e1dcb05

File tree

5 files changed

+137
-18
lines changed

5 files changed

+137
-18
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: 5 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,12 +38,14 @@ 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 for `serialization.mode: "js"`.
42+
4143
### With nonce (recommended)
4244

4345
If you want to use a [strict CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP#strict_csp) with nonces:
4446

4547
1. Create a middleware that configures the CSP header.
46-
It must then be registered using the [`onRequest`](/solid-start/advanced/middleware#onrequest) event.
48+
It must then be registered using the [`onRequest`](/solid-start/advanced/middleware#onrequest) event.
4749
2. Create a nonce using a cryptographic random value generator, such as the [`randomBytes`](https://nodejs.org/api/crypto.html#cryptorandombytessize-callback) function from the `crypto` module.
4850
3. Store the nonce in the [`locals`](/solid-start/advanced/middleware#locals) object.
4951
4. Configure SolidStart to use the nonce in your [`entry-server.tsx`](/solid-start/reference/entrypoints/entry-server) file.
@@ -60,7 +62,7 @@ export default createMiddleware({
6062

6163
const csp = `
6264
default-src 'self';
63-
script-src 'nonce-${nonce}' 'strict-dynamic' 'unsafe-eval';
65+
script-src 'nonce-${nonce}' 'strict-dynamic';
6466
object-src 'none';
6567
base-uri 'none';
6668
frame-ancestors 'none';

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

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tags:
1010
- deployment
1111
- build
1212
- plugins
13-
version: '1.0'
13+
version: "1.0"
1414
description: >-
1515
Configure SolidStart apps with defineConfig. Set up Vite plugins, Nitro
1616
presets, and deployment targets for any platform.
@@ -35,11 +35,12 @@ export default defineConfig({
3535
});
3636
```
3737

38-
The `vite` option can also be a function that can be customized for each Vinxi router.
38+
The `vite` option can also be a function that can be customized for each Vinxi router.
3939

4040
In SolidStart, 3 routers are used:
41+
4142
- `server` - server-side routing
42-
- `client` - for the client-side routing
43+
- `client` - for the client-side routing
4344
- `server-function` - server functions.
4445

4546
```tsx
@@ -56,13 +57,48 @@ export default defineConfig({
5657
});
5758
```
5859

60+
## Serialization
61+
62+
SolidStart serializes server function payloads so they can move between server and client. You can configure the serializer mode to balance performance, payload size, and Content Security Policy (CSP) constraints.
63+
64+
```tsx
65+
import { defineConfig } from "@solidjs/start/config";
66+
67+
export default defineConfig({
68+
serialization: {
69+
mode: "json",
70+
},
71+
});
72+
```
73+
74+
### Modes
75+
76+
- `json`: Uses `JSON.parse` on the client. This is the safest option for strict CSP because it avoids `eval`. Payloads can be slightly larger.
77+
- `js`: Uses Seroval's JS serializer for smaller payloads and better performance, but it relies on `eval` during client-side deserialization and requires `unsafe-eval` in CSP.
78+
79+
### Defaults
80+
81+
- SolidStart v1 defaults to `js` for backwards compatibility.
82+
- SolidStart v2 defaults to `json` for CSP compatibility.
83+
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).
94+
5995
## Configuring Nitro
6096

61-
SolidStart uses [Nitro](https://nitro.build/) to run on a number of platforms.
62-
The `server` option exposes some Nitro options including the build and deployment presets.
97+
SolidStart uses [Nitro](https://nitro.build/) to run on a number of platforms.
98+
The `server` option exposes some Nitro options including the build and deployment presets.
6399
An overview of all available presets is available in the [Deploy section of the Nitro documentation](https://nitro.build/deploy).
64100

65-
Some common ones include:
101+
Some common ones include:
66102

67103
**Servers**
68104

@@ -99,7 +135,7 @@ export default defineConfig({
99135

100136
#### Special note
101137

102-
SolidStart uses async local storage.
138+
SolidStart uses async local storage.
103139
Netlify, Vercel, and Deno support this out of the box but if you're using Cloudflare you will need to specify the following:
104140

105141
```js
@@ -117,7 +153,6 @@ export default defineConfig({
117153

118154
Within `wrangler.toml` you will need to enable node compatibility:
119155

120-
121156
```
122157
compatibility_flags = [ "nodejs_compat" ]
123158
```
@@ -130,6 +165,7 @@ compatibility_flags = [ "nodejs_compat" ]
130165
| solid | object | | Configuration object for [vite-plugin-solid](https://github.com/solidjs/vite-plugin-solid) |
131166
| extensions | string[] | ["js", "jsx", "ts", "tsx"] | Array of file extensions to be treated as routes. |
132167
| server | object | | Nitro server config options |
168+
| serialization | object | | Serialization settings for server function payloads. |
133169
| appRoot | string | "./src" | The path to the root of the application. |
134170
| routeDir | string | "./routes" | The path to where the routes are located. |
135171
| middleware | string | | The path to an optional [middleware](/solid-start/advanced/middleware) file. |

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const logHello = async (message: string) => {
2626
console.log(message);
2727
};
2828
```
29+
2930
Or when using at the top of a file.
31+
3032
```tsx
3133
// File-level
3234
"use server";
@@ -74,16 +76,15 @@ The following examples show how to use server functions alongside solid-router's
7476

7577
```tsx {3}
7678
const getUser = query((id) => {
77-
"use server";
78-
return db.getUser(id);
79+
"use server";
80+
return db.getUser(id);
7981
}, "users");
8082

8183
const updateUser = action(async (id, data) => {
82-
"use server"
83-
await db.setUser(id, data);
84-
throw redirect("/", { revalidate: getUser.keyFor(id) });
84+
"use server";
85+
await db.setUser(id, data);
86+
throw redirect("/", { revalidate: getUser.keyFor(id) });
8587
});
86-
8788
```
8889

8990
When `getUser` or `updateUser` are triggered on the client, an http request will be made to the server, which calls the corresponding server function.
@@ -97,7 +98,8 @@ The data for the redirected page is fetched and streamed to the client in the sa
9798
## Serialization
9899

99100
Server functions allow the serialization of many different data types in the response, using the Seroval serializer.
100-
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).
102+
Configure serialization mode and CSP behavior in [`defineConfig`](/solid-start/reference/config/define-config#serialization).
101103

102104
## Meta information
103105

0 commit comments

Comments
 (0)