Skip to content

Commit b3bb8a0

Browse files
authored
Merge branch 'main' into fix-session-redirect-issue
2 parents 7041fd6 + 4c803e5 commit b3bb8a0

20 files changed

Lines changed: 399 additions & 9 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"@solidjs/start": minor
3+
---
4+
5+
Add `serialization.plugins` to configure custom Seroval plugins for server functions.
6+
7+
Values Seroval has no built-in support for (Mongo's `ObjectId`, Prisma's `Decimal`, `Temporal`, and other custom classes) previously threw when returned from or passed to a server function. Point the new option at a module whose default export is an array of plugins:
8+
9+
```ts
10+
// vite.config.ts
11+
solidStart({
12+
serialization: {
13+
plugins: "src/seroval-plugins.ts",
14+
},
15+
});
16+
```
17+
18+
```ts
19+
// src/seroval-plugins.ts
20+
import { createPlugin } from "@solidjs/start/serialization";
21+
```
22+
23+
The module is bundled into both the client and the server so both ends of a server function agree on the format, so it must not import server-only code. SolidStart's built-in plugins keep precedence. Only server-function and action payloads are affected; the SSR hydration payload is serialized by `solid-js/web`.
24+
25+
Also adds a `@solidjs/start/serialization` entrypoint re-exporting Seroval's `createPlugin`, `OpaqueReference`, and plugin types, so plugin authors stay on the same Seroval version SolidStart serializes with.

‎.changeset/lucky-moths-nonce.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Apply the configured `nonce` to the two script tags that were still missing it, so a strict `script-src` CSP no longer needs `unsafe-inline`:
6+
7+
- The client-side redirect that streaming mode emits after the shell has already flushed (`<script>window.location=...</script>`) now carries the nonce.
8+
- The SPA entry script tag now carries the nonce, matching the SSR entry script.

‎.changeset/pre.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
"bright-dogs-serve",
1010
"chilly-rabbits-lie",
1111
"eleven-actors-nail",
12+
"lucky-pans-gather",
1213
"olive-apes-clap",
1314
"olive-pugs-shave",
1415
"sour-knives-look",
1516
"spotty-jobs-relax",
16-
"spotty-moons-repeat"
17+
"spotty-moons-repeat",
18+
"tricky-goats-cheer",
19+
"wild-pots-repeat"
1720
]
1821
}

‎apps/tests/src/e2e/server-function.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ test.describe("server-function", () => {
7373
await expect(page.locator("#server-fn-test")).toContainText('{"result":true}');
7474
});
7575

76+
/**
77+
* A custom Seroval plugin registered through `serialization.plugins` must
78+
* apply to both the request and the response payload, so a class Seroval has
79+
* no built-in support for survives the round trip with its prototype intact.
80+
* https://github.com/solidjs/solid-start/issues/1474
81+
*/
82+
test("should round-trip a custom class through a custom seroval plugin", async ({ page }) => {
83+
await page.goto("http://localhost:3000/server-function-custom-class");
84+
await expect(page.locator("#server-fn-test")).toContainText(
85+
'{"receivedInstance":true,"receivedHex":"507f191e810c19729de860ea","returnedInstance":true,"returnedHex":"507f1f77bcf86cd799439011"}',
86+
);
87+
});
88+
7689
test("should build with a server function w/ form data", async ({ page }) => {
7790
await page.goto("http://localhost:3000/server-function-form-data");
7891
await expect(page.locator("#server-fn-test")).toContainText('{"result":true}');
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createEffect, createSignal } from "solid-js";
2+
import { ObjectId } from "../utils/object-id.ts";
3+
4+
/**
5+
* Exercises `serialization.plugins`: a class Seroval has no built-in support
6+
* for has to survive both directions of a server function call.
7+
*
8+
* @see https://github.com/solidjs/solid-start/issues/1474
9+
*/
10+
async function echoObjectId(id: ObjectId) {
11+
"use server";
12+
13+
return {
14+
receivedInstance: id instanceof ObjectId,
15+
receivedHex: id.toHexString(),
16+
returned: new ObjectId("507f1f77bcf86cd799439011"),
17+
};
18+
}
19+
20+
export default function App() {
21+
const [output, setOutput] = createSignal<Record<string, unknown>>({});
22+
23+
createEffect(async () => {
24+
const result = await echoObjectId(new ObjectId("507f191e810c19729de860ea"));
25+
26+
setOutput({
27+
receivedInstance: result.receivedInstance,
28+
receivedHex: result.receivedHex,
29+
returnedInstance: result.returned instanceof ObjectId,
30+
returnedHex: result.returned.toHexString(),
31+
});
32+
});
33+
34+
return (
35+
<main>
36+
<span id="server-fn-test">{JSON.stringify(output())}</span>
37+
</main>
38+
);
39+
}

‎apps/tests/src/seroval-plugins.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createPlugin } from "@solidjs/start/serialization";
2+
import { ObjectId } from "./utils/object-id.ts";
3+
4+
/**
5+
* Bundled into both the client and the server via `serialization.plugins`, so
6+
* this module must not import server-only code.
7+
*
8+
* `deserialize` is what rebuilds the value under the default `"json"`
9+
* serialization mode. `serialize` is only used by `mode: "js"`, where the
10+
* payload is evaluated on the client and can therefore reference globals only,
11+
* which is why it reaches for `globalThis` rather than the imported class.
12+
*/
13+
const ObjectIdPlugin = createPlugin<ObjectId, { hex: any }>({
14+
tag: "tests/ObjectId",
15+
test: value => value instanceof ObjectId,
16+
parse: {
17+
sync: (value, ctx) => ({ hex: ctx.parse(value.hex) }),
18+
async: async (value, ctx) => ({ hex: await ctx.parse(value.hex) }),
19+
stream: (value, ctx) => ({ hex: ctx.parse(value.hex) }),
20+
},
21+
serialize: (node, ctx) => `globalThis.ObjectId(${ctx.serialize(node.hex)})`,
22+
deserialize: (node, ctx) => new ObjectId(ctx.deserialize(node.hex) as string),
23+
});
24+
25+
(globalThis as any).ObjectId = (hex: string) => new ObjectId(hex);
26+
27+
export default [ObjectIdPlugin];

‎apps/tests/src/utils/object-id.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Stands in for a custom class that Seroval has no built-in support for, such
3+
* as Mongo's `ObjectId` or Prisma's `Decimal`. Serializing one of these across
4+
* a server function boundary requires a custom Seroval plugin.
5+
*
6+
* @see https://github.com/solidjs/solid-start/issues/1474
7+
*/
8+
export class ObjectId {
9+
constructor(readonly hex: string) {}
10+
11+
toHexString() {
12+
return this.hex;
13+
}
14+
}

‎apps/tests/vite.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export default defineConfig({
88
},
99
plugins: [
1010
solidStart({
11+
serialization: {
12+
plugins: "src/seroval-plugins.ts",
13+
},
1114
env: {
1215
server: {
1316
load() {

‎packages/start/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @solidjs/start
22

3+
## 2.0.0-rc.4
4+
5+
### Patch Changes
6+
7+
- b6dfaac: Updated dependencies, including `cookie-es` to 3, `shiki` to 4, `srvx` to 0.12.4, `oxc-parser` to 0.141 and `seroval` to 1.5.6. `parseSetCookie` now returns `undefined` for cookies with a forbidden name or an empty name and value, and those cookies are no longer forwarded to nested server function requests.
8+
- 02cd41e: Stop the dev toolbar from reporting benign `ResizeObserver loop` notifications as application errors. Browsers dispatch these as window `error` events carrying no error object, so the toolbar was capturing the raw `ErrorEvent` and force-opening the error panel over the app on every resize.
9+
- d3c2af2: Don't send server error stack traces to the client in production builds. When a server function throws, the error is serialized and rethrown on the client, and seroval included `Error.prototype.stack` by default, leaking server file paths and internal function names. Stacks are still serialized in development.
10+
311
## 2.0.0-rc.3
412

513
### Patch Changes

‎packages/start/package.json‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solidjs/start",
3-
"version": "2.0.0-rc.3",
3+
"version": "2.0.0-rc.4",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/solidjs/solid-start.git",
@@ -24,7 +24,8 @@
2424
"./http": "./src/http/index.ts",
2525
"./env": "./env.d.ts",
2626
"./fns/server": "./src/fns/server.ts",
27-
"./fns/client": "./src/fns/client.ts"
27+
"./fns/client": "./src/fns/client.ts",
28+
"./serialization": "./src/fns/plugins.ts"
2829
},
2930
"publishConfig": {
3031
"exports": {
@@ -39,7 +40,8 @@
3940
"./http": "./dist/http/index.js",
4041
"./env": "./env.d.ts",
4142
"./fns/server": "./dist/fns/server.js",
42-
"./fns/client": "./dist/fns/client.js"
43+
"./fns/client": "./dist/fns/client.js",
44+
"./serialization": "./dist/fns/plugins.js"
4345
},
4446
"access": "public"
4547
},

0 commit comments

Comments
 (0)