Files
sweet/tests/islands_test.tsx
T
Marvin Hagemeister 118242eaa6 major: build islands + scan routeDir without importing user code (#3122)
This fixes a bunch of long standing issues in our tracker where a build
wouldn't finish because of importing user code. It's common for backend
apps to spawn some open ended tasks like subscribing to Deno KV or other
things. This addresses another problem of having to set up ENV vars just
for the build, even though they are technically not required to build
islands.

This also gives a roughly ~30% performance boost on cold starts (at
least on `deno.com`). Unfortunately, this does require some breaking
changes. But I believe the performance improvements + removing the
footgun of loading user code during build is well worth it. This work is
also a precursor in case we want to exploring moving to vite at some
point in the future.

## Breaking changes

1. Build options have moved to the builder.
```diff
  // main.ts
- const app = new App({ build: { outDir: "dist/" } })
+ const app = new App()

  // dev.ts
- const builder = new Builder();
+ const builder = new Builder({ outDir: "dist/" });
```

2. Another breaking change is how file based routes are loaded. Since
they're prepared and loaded by the builder the API changed.

```diff
  // main.ts
  const app = new App()
    .use(staticFiles())
  
- await fsRoutes(app, {
-   loadIsland: (path) => import(`./islands/${path}`),
-   loadRoute: (path) => import(`./routes/${path}`),
- }); 
+ app.fsRoutes()
```
You can optionally change the `islandDir` or `routeDir` by passing that
to the `Builder`:

```ts
// dev.ts
const builder = new Builder({
  islandDir: "/path/to/islands", 
  routeDir: "/path/to/routes"
});
```

3. Import `app` lazily in `dev.ts`. Passing `app` to `builder.build()`
is not necessary anymore.

```diff
- import { app } from "./main.ts";

  const builder = new Builder({ target: "safari12" });
  
  if (Deno.args.includes("build")) {
-    await builder.build(app);
+    await builder.build();
  } else {
-   await builder.listen(app);
+   await builder.listen(() => import("./main.ts"));
  }
```

4. The production run command has changed. It's now using `deno serve`
```diff
  // deno.json
  {
    "tasks": {
      "dev": "deno run -A --watch=static/,routes/ dev.ts",
      "dev": "deno run -A dev.ts build",
-     "start": "deno run -A main.ts",
+     "start": "deno serve -A _fresh/server.js",
    }
  }
```
Remove this bit in `main.ts`:

```diff
  // main.ts
  
- if (import.meta.main) {
-   await app.listen()
- }
```

Fixes https://github.com/denoland/fresh/issues/2240
Fixes https://github.com/denoland/fresh/issues/2640
Fixes https://github.com/denoland/fresh/issues/2592
Fixes https://github.com/denoland/fresh/issues/1843
2025-07-24 15:04:05 +02:00

732 lines
20 KiB
TypeScript

import { App, staticFiles } from "fresh";
import { Counter } from "./fixtures_islands/Counter.tsx";
import { IslandInIsland } from "./fixtures_islands/IslandInIsland.tsx";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
import { CounterWithSlots } from "./fixtures_islands/CounterWithSlots.tsx";
import { PassThrough } from "./fixtures_islands/PassThrough.tsx";
import { NullIsland } from "./fixtures_islands/NullIsland.tsx";
import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx";
import { JsxIsland } from "./fixtures_islands/JsxIsland.tsx";
import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx";
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
import { signal } from "@preact/signals";
import {
ALL_ISLAND_DIR,
buildProd,
Doc,
ISLAND_GROUP_DIR,
withBrowserApp,
} from "./test_utils.tsx";
import { parseHtml, waitForText } from "./test_utils.tsx";
import { expect } from "@std/expect";
import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx";
import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
import type { FreshConfig } from "../src/config.ts";
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
import { FakeServer } from "../src/test_utils.ts";
import { PARTIAL_SEARCH_PARAM } from "../src/constants.ts";
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
const islandGroupCache = await buildProd({ root: ISLAND_GROUP_DIR });
function testApp(config?: FreshConfig): App<unknown> {
const app = new App(config)
.use(staticFiles())
.fsRoutes();
allIslandCache(app);
return app;
}
function testGroupApp(config?: FreshConfig): App<unknown> {
const app = new App(config)
.use(staticFiles())
.fsRoutes();
islandGroupCache(app);
return app;
}
Deno.test({
name: "islands - should make signals interactive",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
const sig = signal(3);
return ctx.render(
<Doc>
<Counter count={sig} />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".increment").click();
await waitForText(page, ".output", "4");
});
},
});
Deno.test({
name: "islands - revive multiple islands from one island file",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<Multiple1 id="multiple-1" />
<Multiple2 id="multiple-2" />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator("#multiple-1.ready").wait();
await page.locator("#multiple-2.ready").wait();
await page.locator("#multiple-1 .increment").click();
await page.locator("#multiple-2 .increment").click();
await waitForText(page, "#multiple-1 .output", "1");
await waitForText(page, "#multiple-2 .output", "1");
});
},
});
Deno.test({
name: "islands - revive multiple islands with shared signal",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
const sig = signal(0);
return ctx.render(
<Doc>
<Counter id="counter-1" count={sig} />
<Counter id="counter-2" count={sig} />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator("#counter-1.ready").wait();
await page.locator("#counter-2.ready").wait();
await page.locator("#counter-1 .increment").click();
await waitForText(page, "#counter-1 .output", "1");
await waitForText(page, "#counter-2 .output", "1");
});
},
});
Deno.test({
name: "islands - import json",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<JsonIsland />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator("pre").wait();
const text = await page
.locator<HTMLPreElement>("pre")
.evaluate((el) => el.textContent!);
const json = JSON.parse(text);
expect(json).toEqual({ foo: 123 });
});
},
});
Deno.test({
name: "islands - returns null",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<NullIsland />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
});
},
});
Deno.test({
name: "islands - only instantiate top level island",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<IslandInIsland />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".trigger").click();
await waitForText(page, ".output", "1");
const html = await page.content();
expect(html).not.toContain("import { Counter }");
});
},
});
Deno.test({
name: "islands - pass null JSX props to islands",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<JsxIsland jsx={null}>{null}</JsxIsland>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
const html = await page.content();
const doc = parseHtml(html);
expect(doc.querySelector(".jsx")!.childNodes.length).toEqual(0);
expect(doc.querySelector(".children")!.childNodes.length).toEqual(0);
});
},
});
Deno.test({
name: "islands - pass JSX props to islands",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<JsxIsland jsx={<p>foo</p>}>
<p>bar</p>
</JsxIsland>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLPreElement>("pre")
.evaluate((el) => el.textContent!);
expect(JSON.parse(text)).toEqual({ jsx: true, children: true });
});
},
});
Deno.test({
name: "islands - never serialize children prop",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<JsxChildrenIsland>
foobar
</JsxChildrenIsland>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLScriptElement>("script")
.evaluate((el) => el.textContent!);
expect(text).not.toContain("foobar");
const childText = await page
.locator<HTMLDivElement>(".after")
.evaluate((el) => el.textContent!);
expect(childText).toEqual("foobar");
});
},
});
Deno.test({
name: "islands - instantiate islands in jsx children",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<PassThrough>
<div>
<SelfCounter />
</div>
</PassThrough>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".increment").click();
await waitForText(page, ".output", "1");
});
},
});
Deno.test({
name: "islands - instantiate islands in jsx children with slots",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<CounterWithSlots
jsx={
<div>
<SelfCounter />
</div>
}
>
<div>
<SelfCounter />
</div>
</CounterWithSlots>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".jsx .increment").click();
await page.locator(".children .increment").click();
await page.locator(".counter-with-children button").click();
await waitForText(page, ".counter-with-children .output", "1");
await waitForText(page, ".jsx .output", "1");
await waitForText(page, ".children .output", "1");
});
},
});
Deno.test({
name: "islands - nested children slots",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<PassThrough>
<PassThrough>
<div>
<SelfCounter id="a" />
</div>
</PassThrough>
<PassThrough>
<div>
<SelfCounter id="b" />
</div>
</PassThrough>
</PassThrough>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator("#a .increment").click();
await page.locator("#b .increment").click();
await waitForText(page, "#a .output", "1");
await waitForText(page, "#b .output", "1");
});
},
});
Deno.test({
name: "islands - conditional jsx children",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<JsxConditional
jsx={
<div>
<SelfCounter />
</div>
}
>
<div>
<SelfCounter />
</div>
</JsxConditional>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".jsx .increment").click();
await page.locator(".children .increment").click();
await page.locator(".cond-update").click();
await waitForText(page, ".cond-output", "1");
await waitForText(page, ".jsx .output", "1");
await waitForText(page, ".children .output", "1");
});
},
});
Deno.test({
name: "islands - revive DOM attributes",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<JsxConditional>
<div class="foo">
<form>
<label for="check">
checked
</label>
<input name="check" type="checkbox" checked />
<label for="text">
is required
</label>
<input name="text" type="text" required />
<label for="foo-1">
not selected
</label>
<input id="foo-1" type="radio" name="foo" value="1" />
<label for="foo-2">
selected
</label>
<input id="foo-2" type="radio" name="foo" value="2" checked />
<label for="select">
select value should be "bar"
</label>
<select name="select">
<option value="foo">foo</option>
<option value="bar" selected>bar</option>
</select>
</form>
<SelfCounter />
</div>
</JsxConditional>
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".children > .foo").wait();
const checkboxChecked = await page
.locator<HTMLInputElement>("input[name='check']")
.evaluate((el) => el.checked);
expect(checkboxChecked).toEqual(true);
const required = await page
.locator<HTMLInputElement>("input[name='text']")
.evaluate((el) => el.required);
expect(required).toEqual(true);
const radio1 = await page
.locator<HTMLInputElement>("input[type='radio'][value='1']")
.evaluate((el) => el.checked);
expect(radio1).toEqual(false);
const radio2 = await page
.locator<HTMLInputElement>("input[type='radio'][value='2']")
.evaluate((el) => el.checked);
expect(radio2).toEqual(true);
});
},
});
Deno.test({
name: "islands - revive island with fn inside",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<FnIsland />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLDivElement>(".ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
});
},
});
Deno.test({
name: "islands - escape props",
fn: async () => {
const app = testApp()
.get("/", (ctx) => {
return ctx.render(
<Doc>
<EscapeIsland str={`"foo"asdf`} />
</Doc>,
);
})
.get("/foo", (ctx) => {
return ctx.render(
<Doc>
<EscapeIsland str={`<script>alert('hey')</script>`} />
</Doc>,
);
})
.get("/bar", (ctx) => {
return ctx.render(
<Doc>
<EscapeIsland str={`<!--<script>alert('hey')</script>`} />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
// Page would error here
const text = await page
.locator<HTMLDivElement>(".ready p")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
// Page would error here
const text2 = await page
.locator<HTMLDivElement>(".ready div")
.evaluate((el) => el.textContent!);
expect(text2).toEqual(`"foo"asdf`);
});
// Check escaping of `</`
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo`, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLDivElement>(".ready p")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
const text2 = await page
.locator<HTMLDivElement>(".ready div")
.evaluate((el) => el.textContent!);
expect(text2).toEqual(`<script>alert('hey')</script>`);
});
// Check escaping of `<!--`
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/bar`, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLDivElement>(".ready p")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
const text2 = await page
.locator<HTMLDivElement>(".ready div")
.evaluate((el) => el.textContent!);
expect(text2).toEqual(`<!--<script>alert('hey')</script>`);
});
// Partials (they use a different code path)
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo?${PARTIAL_SEARCH_PARAM}`, {
waitUntil: "load",
});
const text = await page
.locator<HTMLScriptElement>("script[type='application/json']")
.evaluate((el) => el.textContent!);
const json = JSON.parse(text);
expect(json.props).toContain("<script>alert('hey')</script>");
});
},
});
Deno.test({
name: "islands - stub Node 'process.env'",
fn: async () => {
const app = testApp()
.get("/", (ctx) =>
ctx.render(
<Doc>
<NodeProcess />
</Doc>,
));
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/`, { waitUntil: "load" });
await page.locator(".ready").wait();
// Page would error here
const text = await page
.locator<HTMLDivElement>(".ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("value: production");
});
},
});
Deno.test({
name: "islands - in base path",
fn: async () => {
const app = testApp({ basePath: "/foo" })
.get("/", (ctx) =>
ctx.render(
<Doc>
<SelfCounter />
</Doc>,
));
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo`, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".increment").click();
await waitForText(page, ".output", "1");
});
},
});
Deno.test({
name: "islands - preserve f-* attributes",
fn: async () => {
const app = testApp()
.get("/", (ctx) =>
ctx.render(
<Doc>
<FreshAttrs />
</Doc>,
));
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".ready").wait();
const truthy = await page.locator<HTMLDivElement>(".f-client-nav-true")
.evaluate((el) => el.getAttribute("f-client-nav"));
const falsy = await page.locator<HTMLDivElement>(".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 () => {
const app = testGroupApp();
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo`, { waitUntil: "load" });
await page.locator(".ready").wait();
// Page would error here
const text = await page
.locator<HTMLDivElement>(".ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
});
},
});
Deno.test({
name: "fsRoutes - load islands from group folder with same name",
fn: async () => {
const app = testGroupApp();
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/both`, { waitUntil: "load" });
await page.locator(".ready").wait();
// Page would error here
let text = await page
.locator<HTMLDivElement>("#foo.ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
text = await page
.locator<HTMLDivElement>("#foo-both.ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
});
},
});
Deno.test({
name: "islands - adds preload HTTP headers",
fn: async () => {
const app = testApp()
.get("/", (ctx) =>
ctx.render(
<Doc>
<SelfCounter />
</Doc>,
));
const server = new FakeServer(app.handler());
const res = await server.get("/");
await res.body?.cancel();
const link = res.headers.get("Link");
expect(link).toMatch(
/<\/_fresh\/js\/[a-zA-Z0-9]+\/fresh-runtime\.js>; rel="modulepreload"; as="script", <\/_fresh\/js\/[a-zA-Z0-9]+\/SelfCounter\.js>; rel="modulepreload"; as="script"/,
);
},
});