From c172af67cf7ac4b21ec1ea482494cda8136a6f91 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 6 Nov 2023 14:04:43 +0100 Subject: [PATCH] fix: twind styles not updated on partial nav (#2017) --- plugins/twind.ts | 38 +++++++++---------- plugins/twindv1.ts | 8 +--- tests/fixture_twind_hydrate/fresh.gen.ts | 21 ++++++---- .../routes/island_twind/_layout.tsx | 29 ++++++++++++++ .../routes/island_twind/blue.tsx | 7 ++++ .../routes/island_twind/index.tsx | 7 ++++ tests/twind_test.ts | 20 ++++++++++ 7 files changed, 96 insertions(+), 34 deletions(-) create mode 100644 tests/fixture_twind_hydrate/routes/island_twind/_layout.tsx create mode 100644 tests/fixture_twind_hydrate/routes/island_twind/blue.tsx create mode 100644 tests/fixture_twind_hydrate/routes/island_twind/index.tsx diff --git a/plugins/twind.ts b/plugins/twind.ts index 62bd6a62..3c8c9d2d 100644 --- a/plugins/twind.ts +++ b/plugins/twind.ts @@ -17,32 +17,28 @@ export default function(state) { hydrate(options, state); }`; entrypoints: { "main": main }, async renderAsync(ctx) { sheet.reset(undefined); - const res = await ctx.renderAsync(); + await ctx.renderAsync(); const cssTexts = [...sheet.target]; const snapshot = sheet.reset(); - const scripts = []; - let cssText: string; - if (res.requiresHydration) { - const precedences = snapshot[1] as number[]; - cssText = cssTexts.map((cssText, i) => - `${cssText}/*${precedences[i].toString(36)}*/` - ).join("\n"); - const mappings: (string | [string, string])[] = []; - for ( - const [key, value] of (snapshot[3] as Map).entries() - ) { - if (key === value) { - mappings.push(key); - } else { - mappings.push([key, value]); - } + const precedences = snapshot[1] as number[]; + + const cssText = cssTexts.map((cssText, i) => + `${cssText}/*${precedences[i].toString(36)}*/` + ).join("\n"); + + const mappings: (string | [string, string])[] = []; + for ( + const [key, value] of (snapshot[3] as Map).entries() + ) { + if (key === value) { + mappings.push(key); + } else { + mappings.push([key, value]); } - scripts.push({ entrypoint: "main", state: mappings }); - } else { - cssText = cssTexts.join("\n"); } + return { - scripts, + scripts: [{ entrypoint: "main", state: mappings }], styles: [{ cssText, id: STYLE_ELEMENT_ID }], }; }, diff --git a/plugins/twindv1.ts b/plugins/twindv1.ts index 5af59f9d..7f1efa5e 100644 --- a/plugins/twindv1.ts +++ b/plugins/twindv1.ts @@ -24,14 +24,10 @@ export default function(state) { hydrate(options, state); }`; name: "twind", entrypoints: { "main": main }, async renderAsync(ctx) { - const res = await ctx.renderAsync(); + await ctx.renderAsync(); const cssText = stringify(sheet.target); - const scripts = []; - if (res.requiresHydration) { - scripts.push({ entrypoint: "main", state: [] }); - } return { - scripts, + scripts: [{ entrypoint: "main", state: [] }], styles: [{ cssText, id: STYLE_ELEMENT_ID }], }; }, diff --git a/tests/fixture_twind_hydrate/fresh.gen.ts b/tests/fixture_twind_hydrate/fresh.gen.ts index 0b15419a..9d16f912 100644 --- a/tests/fixture_twind_hydrate/fresh.gen.ts +++ b/tests/fixture_twind_hydrate/fresh.gen.ts @@ -4,25 +4,32 @@ import * as $0 from "./routes/check-duplication.tsx"; import * as $1 from "./routes/insert-cssrules.tsx"; -import * as $2 from "./routes/static.tsx"; -import * as $3 from "./routes/unused.tsx"; -import * as $4 from "./routes/unused_tw.tsx"; +import * as $2 from "./routes/island_twind/_layout.tsx"; +import * as $3 from "./routes/island_twind/blue.tsx"; +import * as $4 from "./routes/island_twind/index.tsx"; +import * as $5 from "./routes/static.tsx"; +import * as $6 from "./routes/unused.tsx"; +import * as $7 from "./routes/unused_tw.tsx"; import * as $$0 from "./islands/CheckDuplication.tsx"; import * as $$1 from "./islands/InsertCssrules.tsx"; +import { Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/check-duplication.tsx": $0, "./routes/insert-cssrules.tsx": $1, - "./routes/static.tsx": $2, - "./routes/unused.tsx": $3, - "./routes/unused_tw.tsx": $4, + "./routes/island_twind/_layout.tsx": $2, + "./routes/island_twind/blue.tsx": $3, + "./routes/island_twind/index.tsx": $4, + "./routes/static.tsx": $5, + "./routes/unused.tsx": $6, + "./routes/unused_tw.tsx": $7, }, islands: { "./islands/CheckDuplication.tsx": $$0, "./islands/InsertCssrules.tsx": $$1, }, baseUrl: import.meta.url, -}; +} satisfies Manifest; export default manifest; diff --git a/tests/fixture_twind_hydrate/routes/island_twind/_layout.tsx b/tests/fixture_twind_hydrate/routes/island_twind/_layout.tsx new file mode 100644 index 00000000..5f20a2ea --- /dev/null +++ b/tests/fixture_twind_hydrate/routes/island_twind/_layout.tsx @@ -0,0 +1,29 @@ +import { Partial } from "$fresh/runtime.ts"; +import { LayoutProps } from "$fresh/server.ts"; + +export default function Layout( + { Component }: LayoutProps, +) { + return ( +
+ +
+ + + +
+
+ ); +} diff --git a/tests/fixture_twind_hydrate/routes/island_twind/blue.tsx b/tests/fixture_twind_hydrate/routes/island_twind/blue.tsx new file mode 100644 index 00000000..1dd8fc6a --- /dev/null +++ b/tests/fixture_twind_hydrate/routes/island_twind/blue.tsx @@ -0,0 +1,7 @@ +export default function Blue() { + return ( +
+ Should have blue bg +
+ ); +} diff --git a/tests/fixture_twind_hydrate/routes/island_twind/index.tsx b/tests/fixture_twind_hydrate/routes/island_twind/index.tsx new file mode 100644 index 00000000..b599ca56 --- /dev/null +++ b/tests/fixture_twind_hydrate/routes/island_twind/index.tsx @@ -0,0 +1,7 @@ +export default function Home() { + return ( +
+ Should have red bg +
+ ); +} diff --git a/tests/twind_test.ts b/tests/twind_test.ts index 9235859d..f425044d 100644 --- a/tests/twind_test.ts +++ b/tests/twind_test.ts @@ -3,6 +3,7 @@ import { assert, assertEquals, assertMatch, delay, puppeteer } from "./deps.ts"; import { cmpStringArray } from "./fixture_twind_hydrate/utils/utils.ts"; import { startFreshServer, + waitForStyle, withFakeServe, withFresh, withPageName, @@ -385,3 +386,22 @@ Deno.test("don't duplicate css class with twindV1", async () => { }, ); }); + +Deno.test("render styles from partial update", async () => { + await withPageName( + "./tests/fixture_twind_hydrate/main.ts", + async (page, address) => { + await page.goto(`${address}/island_twind`); + await page.click(`a[href="/island_twind/blue"]`); + + await page.waitForSelector(".bg-blue-500"); + + await waitForStyle( + page, + ".bg-blue-500", + "backgroundColor", + "rgb(59, 130, 246)", + ); + }, + ); +});