fix: CSS modules not working in _app/_layout/_error and across routes in dev (#3764)

Islands used in `_app.tsx`, `_layout.tsx`, or `_error.tsx` are
discovered after `<head>` has already rendered, so their CSS `<link>`
tags were never injected. `FreshScripts` (at end of `<body>`) now emits
late CSS for any islands missed by `RemainingHead`, using an
`injectedCss` set to avoid duplicates.

In dev mode, `collectCss` only walked the `fresh:client-entry` module
graph, missing CSS imported by island modules. The dev server now also
walks `fresh-island::*` module graphs when collecting CSS.

Note: late-injected `<link>` tags in `<body>` may cause a brief flash of
unstyled content — this is inherent to the problem since `<head>`
renders before `<body>` islands are discovered.

Closes #3633

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bartek Iwańczuk
2026-04-09 17:37:10 +02:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 14f895517e
commit 36dd6375eb
8 changed files with 170 additions and 3 deletions
@@ -235,6 +235,53 @@ integrationTest("vite dev - css modules", async () => {
});
});
// Issue: https://github.com/denoland/fresh/issues/3633
integrationTest(
"vite dev - css modules in _app.tsx island are injected",
async () => {
await withBrowser(async (page) => {
await page.goto(`${demoServer.address()}/tests/css_modules`, {
waitUntil: "networkidle2",
});
await waitFor(async () => {
const bgColor = await page
.locator(".app-nav")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).backgroundColor);
expect(bgColor).toEqual("rgb(30, 30, 30)");
return true;
});
});
},
);
// Issue: https://github.com/denoland/fresh/issues/3633
integrationTest(
"vite dev - css modules work on second page with shared island",
async () => {
await withBrowser(async (page) => {
// First visit a different page, then visit page2 that shares
// the CssModules island. In dev mode, the CSS must still work.
await page.goto(`${demoServer.address()}/`, {
waitUntil: "networkidle2",
});
await page.goto(`${demoServer.address()}/tests/css_modules_page2`, {
waitUntil: "networkidle2",
});
await waitFor(async () => {
const color = await page
.locator(".red > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 0, 0)");
return true;
});
});
},
);
integrationTest("vite dev - route css import", async () => {
await withBrowser(async (page) => {
await page.goto(`${demoServer.address()}/tests/css`, {