Skip to content

Commit 0937ca3

Browse files
fix: error on same island files in different folders
1 parent 386d3d2 commit 0937ca3

File tree

7 files changed

+69
-14
lines changed

7 files changed

+69
-14
lines changed

src/dev/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export class Builder<State = any> {
280280

281281
const prefix = `/_fresh/js/${BUILD_ID}/`;
282282

283-
for (const name of namer.getNames()) {
283+
for (const name of buildCache.islandModNameToChunk.keys()) {
284284
const chunkName = output.entryToChunk.get(name);
285285
if (chunkName === undefined) {
286286
throw new Error(`Could not find chunk for island ${name}`);

src/runtime/server/preact_hooks.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ function FreshRuntimeScript() {
457457
const islandImports = islandArr.map((island) => {
458458
const named = island.exportName === "default"
459459
? island.name
460-
: `{ ${island.exportName} }`;
460+
: island.exportName === island.name
461+
? `{ ${island.exportName} }`
462+
: `{ ${island.exportName} as ${island.name} }`;
461463
return `import ${named} from "${`${basePath}${island.file}`}";`;
462464
}).join("");
463465

src/utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,4 @@ export class UniqueNamer {
6262

6363
return name;
6464
}
65-
66-
getNames(): string[] {
67-
return Array.from(this.#seen.keys());
68-
}
6965
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useSignal } from "@preact/signals";
2+
import { useEffect } from "preact/hooks";
3+
4+
export function Foo() {
5+
const active = useSignal(false);
6+
useEffect(() => {
7+
active.value = true;
8+
}, []);
9+
10+
return (
11+
<div id="foo-both" class={active.value ? "ready" : ""}>
12+
{active.value ? "it works" : "it doesn't work"}
13+
</div>
14+
);
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Foo } from "./(_islands)/Foo.tsx";
2+
import { Foo as Foo2 } from "../foo/(_islands)/Foo.tsx";
3+
4+
export default function Home() {
5+
return (
6+
<div>
7+
<Foo />
8+
<Foo2 />
9+
</div>
10+
);
11+
}

tests/fixture_island_groups/routes/foo/(_islands)/Foo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function Foo() {
88
}, []);
99

1010
return (
11-
<div class={active.value ? "ready" : ""}>
11+
<div id="foo" class={active.value ? "ready" : ""}>
1212
{active.value ? "it works" : "it doesn't work"}
1313
</div>
1414
);

tests/islands_test.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,25 @@ const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
3232
const islandGroupdCache = await buildProd({ root: ISLAND_GROUP_DIR });
3333

3434
function testApp(config?: FreshConfig): App<unknown> {
35-
const app = new App(config);
36-
app.use(staticFiles());
35+
const app = new App(config)
36+
.use(staticFiles())
37+
.fsRoutes();
3738

3839
allIslandCache(app);
3940

4041
return app;
4142
}
4243

44+
function testGroupApp(config?: FreshConfig): App<unknown> {
45+
const app = new App(config)
46+
.use(staticFiles())
47+
.fsRoutes();
48+
49+
islandGroupdCache(app);
50+
51+
return app;
52+
}
53+
4354
Deno.test({
4455
name: "islands - should make signals interactive",
4556
fn: async () => {
@@ -657,11 +668,7 @@ Deno.test({
657668
Deno.test({
658669
name: "fsRoutes - load islands from group folder",
659670
fn: async () => {
660-
const app = new App()
661-
.use(staticFiles())
662-
.fsRoutes();
663-
664-
islandGroupdCache(app);
671+
const app = testGroupApp();
665672

666673
await withBrowserApp(app, async (page, address) => {
667674
await page.goto(`${address}/foo`, { waitUntil: "load" });
@@ -676,6 +683,30 @@ Deno.test({
676683
},
677684
});
678685

686+
Deno.test({
687+
name: "fsRoutes - load islands from group folder with same name",
688+
fn: async () => {
689+
const app = testGroupApp();
690+
691+
await withBrowserApp(app, async (page, address) => {
692+
await page.goto(`${address}/both`, { waitUntil: "load" });
693+
694+
await page.locator(".ready").wait();
695+
696+
// Page would error here
697+
let text = await page
698+
.locator<HTMLDivElement>("#foo.ready")
699+
.evaluate((el) => el.textContent!);
700+
expect(text).toEqual("it works");
701+
702+
text = await page
703+
.locator<HTMLDivElement>("#foo-both.ready")
704+
.evaluate((el) => el.textContent!);
705+
expect(text).toEqual("it works");
706+
});
707+
},
708+
});
709+
679710
Deno.test({
680711
name: "islands - adds preload HTTP headers",
681712
fn: async () => {

0 commit comments

Comments
 (0)