Skip to content

Commit 7ffe317

Browse files
committed
add docs about serialization modes
1 parent 1ea9b56 commit 7ffe317

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

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

Lines changed: 4 additions & 2 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).
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.

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

Lines changed: 35 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,39 @@ 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+
Seroval defines the supported value types and edge cases. See the full list in the Seroval compatibility docs.
85+
5986
## Configuring Nitro
6087

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.
88+
SolidStart uses [Nitro](https://nitro.build/) to run on a number of platforms.
89+
The `server` option exposes some Nitro options including the build and deployment presets.
6390
An overview of all available presets is available in the [Deploy section of the Nitro documentation](https://nitro.build/deploy).
6491

65-
Some common ones include:
92+
Some common ones include:
6693

6794
**Servers**
6895

@@ -99,7 +126,7 @@ export default defineConfig({
99126

100127
#### Special note
101128

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

105132
```js
@@ -117,7 +144,6 @@ export default defineConfig({
117144

118145
Within `wrangler.toml` you will need to enable node compatibility:
119146

120-
121147
```
122148
compatibility_flags = [ "nodejs_compat" ]
123149
```
@@ -130,6 +156,7 @@ compatibility_flags = [ "nodejs_compat" ]
130156
| solid | object | | Configuration object for [vite-plugin-solid](https://github.com/solidjs/vite-plugin-solid) |
131157
| extensions | string[] | ["js", "jsx", "ts", "tsx"] | Array of file extensions to be treated as routes. |
132158
| server | object | | Nitro server config options |
159+
| serialization | object | | Serialization settings for server function payloads. |
133160
| appRoot | string | "./src" | The path to the root of the application. |
134161
| routeDir | string | "./routes" | The path to where the routes are located. |
135162
| 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
@@ -11,7 +11,7 @@ tags:
1111
- actions
1212
- security
1313
- database
14-
version: '1.0'
14+
version: "1.0"
1515
description: >-
1616
Create server-only functions in SolidStart using 'use server' directive.
1717
Handle database operations, API calls, and secure logic on the server.
@@ -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.
@@ -98,6 +99,7 @@ The data for the redirected page is fetched and streamed to the client in the sa
9899

99100
Server functions allow the serialization of many different data types in the response, using the Seroval serializer.
100101
The full list is available [in Seroval's source code](https://github.com/lxsmnsyc/seroval/blob/main/docs/compatibility.md#supported-types).
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)