From 2b88f4bf5696737d01157d5da3350f13cf63d290 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 5 Jun 2024 10:06:53 +0200 Subject: [PATCH] fix: falsy `f-*` attributes not preserved inside island (#2498) Fixes https://github.com/denoland/fresh/issues/2359 --- src/runtime/client/mod.tsx | 1 + src/runtime/client/preact_hooks_client.ts | 12 ++++++++- tests/fixtures_islands/FreshAttrs.tsx | 23 +++++++++++++++++ tests/islands_test.tsx | 31 +++++++++++++++++++++++ tests/test_utils.tsx | 2 ++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures_islands/FreshAttrs.tsx diff --git a/src/runtime/client/mod.tsx b/src/runtime/client/mod.tsx index 292cb13a..ec2d6fa8 100644 --- a/src/runtime/client/mod.tsx +++ b/src/runtime/client/mod.tsx @@ -1,4 +1,5 @@ import "./polyfills.ts"; +import "./preact_hooks_client.ts"; import "./partials.ts"; export { asset, IS_BROWSER, Partial, type PartialProps } from "../shared.ts"; export { boot, revive } from "./reviver.ts"; diff --git a/src/runtime/client/preact_hooks_client.ts b/src/runtime/client/preact_hooks_client.ts index a4ff983a..34564882 100644 --- a/src/runtime/client/preact_hooks_client.ts +++ b/src/runtime/client/preact_hooks_client.ts @@ -1,9 +1,19 @@ import { options } from "preact"; -import { assetHashingHook } from "../shared_internal.tsx"; +import { assetHashingHook, CLIENT_NAV_ATTR } from "../shared_internal.tsx"; import { BUILD_ID } from "../build_id.ts"; const oldVNodeHook = options.vnode; options.vnode = (vnode) => { assetHashingHook(vnode, BUILD_ID); + + if (typeof vnode.type === "string") { + if (CLIENT_NAV_ATTR in vnode.props) { + const value = vnode.props[CLIENT_NAV_ATTR]; + if (typeof value === "boolean") { + vnode.props[CLIENT_NAV_ATTR] = String(value); + } + } + } + oldVNodeHook?.(vnode); }; diff --git a/tests/fixtures_islands/FreshAttrs.tsx b/tests/fixtures_islands/FreshAttrs.tsx new file mode 100644 index 00000000..e6aae089 --- /dev/null +++ b/tests/fixtures_islands/FreshAttrs.tsx @@ -0,0 +1,23 @@ +import { useSignal } from "@preact/signals"; +import { useEffect } from "preact/hooks"; + +export interface FreshAttrs { + id?: string; +} + +export function FreshAttrs(props: FreshAttrs) { + const active = useSignal(false); + useEffect(() => { + active.value = true; + }, []); + + return ( +
+

Fresh attrs

+
f-client-nav=true
+
+ f-client-nav=false +
+
+ ); +} diff --git a/tests/islands_test.tsx b/tests/islands_test.tsx index 7db876aa..055a7c06 100644 --- a/tests/islands_test.tsx +++ b/tests/islands_test.tsx @@ -29,6 +29,7 @@ import * as path from "@std/path"; import { setBuildCache } from "../src/app.ts"; import { getBuildCache } from "../src/app.ts"; import type { FreshConfig } from "../src/config.ts"; +import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx"; await buildProd(allIslandApp); @@ -601,6 +602,36 @@ Deno.test({ }, }); +Deno.test({ + name: "islands - preserve f-* attributes", + fn: async () => { + const freshAttrs = getIsland("FreshAttrs.tsx"); + + const app = testApp() + .use(staticFiles()) + .island(freshAttrs, "FreshAttrs", FreshAttrs) + .get("/", (ctx) => + ctx.render( + + + , + )); + + await withBrowserApp(app, async (page, address) => { + await page.goto(address, { waitUntil: "load" }); + await page.locator(".ready").wait(); + + const truthy = await page.locator(".f-client-nav-true") + .evaluate((el) => el.getAttribute("f-client-nav")); + const falsy = await page.locator(".f-client-nav-false") + .evaluate((el) => el.getAttribute("f-client-nav")); + + expect(truthy).toEqual("true"); + expect(falsy).toEqual("false"); + }); + }, +}); + Deno.test({ name: "fsRoutes - load islands from group folder", fn: async () => { diff --git a/tests/test_utils.tsx b/tests/test_utils.tsx index e4c1e632..ca32613e 100644 --- a/tests/test_utils.tsx +++ b/tests/test_utils.tsx @@ -25,6 +25,7 @@ import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx"; import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx"; import { Foo } from "./fixture_island_groups/routes/foo/(_islands)/Foo.tsx"; import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx"; +import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx"; export function getIsland(pathname: string) { return path.join( @@ -378,6 +379,7 @@ export const allIslandApp = new App() .island(getIsland("PassThrough.tsx"), "PassThrough", PassThrough) .island(getIsland("SelfCounter.tsx"), "SelfCounter", SelfCounter) .island(getIsland("NodeProcess.tsx"), "NodeProcess", NodeProcess) + .island(getIsland("FreshAttrs.tsx"), "FreshAttrs", FreshAttrs) .island( getIsland("../fixture_island_groups/routes/foo/(_islands)/Foo.tsx"), "Foo",