fix: twind styles not updated on partial nav (#2017)

This commit is contained in:
Marvin Hagemeister
2023-11-06 14:04:43 +01:00
committed by GitHub
parent 8b846816a6
commit c172af67cf
7 changed files with 96 additions and 34 deletions
+17 -21
View File
@@ -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<string, string>).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<string, string>).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 }],
};
},
+2 -6
View File
@@ -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 }],
};
},
+14 -7
View File
@@ -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;
@@ -0,0 +1,29 @@
import { Partial } from "$fresh/runtime.ts";
import { LayoutProps } from "$fresh/server.ts";
export default function Layout(
{ Component }: LayoutProps<unknown, unknown>,
) {
return (
<div f-client-nav>
<nav>
<ul class="text-blue-500 underline m-8 flex gap-12">
<li>
<a href="/island_twind">red bg</a>
</li>
<li>
<a href="/island_twind/blue">blue bg</a>
</li>
<li>
<a href="/island_twind/no-route-here">404 page</a>
</li>
</ul>
</nav>
<div class="mx-auto max-w-screen-md">
<Partial name="main-content">
<Component />
</Partial>
</div>
</div>
);
}
@@ -0,0 +1,7 @@
export default function Blue() {
return (
<div class="bg-blue-500">
Should have blue bg
</div>
);
}
@@ -0,0 +1,7 @@
export default function Home() {
return (
<div class="bg-red-500">
Should have red bg
</div>
);
}
+20
View File
@@ -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)",
);
},
);
});