diff --git a/src/server/serializer.ts b/src/server/serializer.ts index 2c49201e..af3cb46e 100644 --- a/src/server/serializer.ts +++ b/src/server/serializer.ts @@ -88,6 +88,16 @@ export function serialize(data: unknown): SerializeResult { return value; } + // Bypass signal's `.toJSON` method because we want to serialize + // the signal itself including the signal's value and not just + // the value. This is needed because `JSON.stringify` always + // calls `.toJSON` automatically if available. + // deno-lint-ignore no-explicit-any + if (key !== null && isSignal((this as any)[key])) { + // deno-lint-ignore no-explicit-any + value = (this as any)[key]; + } + // For some object types, the path in the object graph from root is not the // same between the serialized representation, and deserialized objects. For // these cases, we have to change the contents of the key stack to match the @@ -155,22 +165,6 @@ export function serialize(data: unknown): SerializeResult { parentStack.push(res); return res; } else { - if (key !== null) { - // Bypass signal's `.toJSON` method because we want to serialize - // the signal itself including the signal's value and not just - // the value. This is needed because `JSON.stringify` always - // calls `.toJSON` automatically if available. - // deno-lint-ignore no-explicit-any - const realValue = (this as any)[key]; - if (isSignal(realValue)) { - requiresDeserializer = true; - hasSignals = true; - const res = { [KEY]: "s", v: realValue.peek() }; - parentStack.push(res); - return res; - } - } - parentStack.push(value); return value; } diff --git a/tests/fixture/deno.json b/tests/fixture/deno.json index 9d782057..bbc066e9 100644 --- a/tests/fixture/deno.json +++ b/tests/fixture/deno.json @@ -5,8 +5,8 @@ "preact": "https://esm.sh/preact@10.15.1", "preact/": "https://esm.sh/preact@10.15.1/", "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0", - "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", - "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.5", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.3.1" }, "compilerOptions": { "jsx": "react-jsx", diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 3692206c..c258f500 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -43,15 +43,16 @@ import * as $37 from "./routes/middleware_root.ts"; import * as $38 from "./routes/not_found.ts"; import * as $39 from "./routes/params.tsx"; import * as $40 from "./routes/props/[id].tsx"; -import * as $41 from "./routes/state-in-props/_middleware.ts"; -import * as $42 from "./routes/state-in-props/index.tsx"; -import * as $43 from "./routes/state-middleware/_middleware.ts"; -import * as $44 from "./routes/state-middleware/foo/_middleware.ts"; -import * as $45 from "./routes/state-middleware/foo/index.tsx"; -import * as $46 from "./routes/static.tsx"; -import * as $47 from "./routes/status_overwrite.tsx"; -import * as $48 from "./routes/umlaut-äöüß.tsx"; -import * as $49 from "./routes/wildcard.tsx"; +import * as $41 from "./routes/signal_shared.tsx"; +import * as $42 from "./routes/state-in-props/_middleware.ts"; +import * as $43 from "./routes/state-in-props/index.tsx"; +import * as $44 from "./routes/state-middleware/_middleware.ts"; +import * as $45 from "./routes/state-middleware/foo/_middleware.ts"; +import * as $46 from "./routes/state-middleware/foo/index.tsx"; +import * as $47 from "./routes/static.tsx"; +import * as $48 from "./routes/status_overwrite.tsx"; +import * as $49 from "./routes/umlaut-äöüß.tsx"; +import * as $50 from "./routes/wildcard.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/MultipleCounters.tsx"; import * as $$2 from "./islands/ReturningNull.tsx"; @@ -105,15 +106,16 @@ const manifest = { "./routes/not_found.ts": $38, "./routes/params.tsx": $39, "./routes/props/[id].tsx": $40, - "./routes/state-in-props/_middleware.ts": $41, - "./routes/state-in-props/index.tsx": $42, - "./routes/state-middleware/_middleware.ts": $43, - "./routes/state-middleware/foo/_middleware.ts": $44, - "./routes/state-middleware/foo/index.tsx": $45, - "./routes/static.tsx": $46, - "./routes/status_overwrite.tsx": $47, - "./routes/umlaut-äöüß.tsx": $48, - "./routes/wildcard.tsx": $49, + "./routes/signal_shared.tsx": $41, + "./routes/state-in-props/_middleware.ts": $42, + "./routes/state-in-props/index.tsx": $43, + "./routes/state-middleware/_middleware.ts": $44, + "./routes/state-middleware/foo/_middleware.ts": $45, + "./routes/state-middleware/foo/index.tsx": $46, + "./routes/static.tsx": $47, + "./routes/status_overwrite.tsx": $48, + "./routes/umlaut-äöüß.tsx": $49, + "./routes/wildcard.tsx": $50, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture/routes/signal_shared.tsx b/tests/fixture/routes/signal_shared.tsx new file mode 100644 index 00000000..a591e28d --- /dev/null +++ b/tests/fixture/routes/signal_shared.tsx @@ -0,0 +1,12 @@ +import { useSignal } from "@preact/signals"; +import Counter from "$fresh/tests/fixture/islands/Counter.tsx"; + +export default function SignalShared() { + const sig = useSignal(1); + return ( +