From b454ece12847e5de29abe33154c5398a44e19d9d Mon Sep 17 00:00:00 2001 From: Alex Grozav Date: Fri, 20 Nov 2026 23:09:01 +0200 Subject: [PATCH 1/2] fix(target-astro): start the render server when Vitest configures the browser project Co-authored-by: multica-agent --- .../target-astro/src/toolchain/browser.ts | 11 +- packages/target-astro/src/toolchain/index.ts | 5 +- packages/target-astro/src/toolchain/render.ts | 16 +++ .../target-astro/test/render-boot.test.ts | 111 ++++++++++++++++++ 4 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 packages/target-astro/test/render-boot.test.ts diff --git a/packages/target-astro/src/toolchain/browser.ts b/packages/target-astro/src/toolchain/browser.ts index 1cb80966c..975112d40 100644 --- a/packages/target-astro/src/toolchain/browser.ts +++ b/packages/target-astro/src/toolchain/browser.ts @@ -1,8 +1,9 @@ +import type { ToolchainContext } from "@unframework.dev/codegen"; // Vitest's `Plugin` is Vite's, with the `configureVitest` hook declared. import type { Plugin } from "vitest/config"; import { ASTRO_VIRTUAL_ID, astroComponentRef } from "./protocol.ts"; -import { closeAstroRenderServers } from "./render.ts"; +import { closeAstroRenderServers, startAstroRenderServer } from "./render.ts"; import { recordAstroSource } from "./sources.ts"; /** @@ -12,9 +13,12 @@ import { recordAstroSource } from "./sources.ts"; * * It records the Astro source the unframework plugin loaded for each virtual id, then replaces * the module with a serialisable reference (`AstroComponentRef`) that the mount adapter sends to - * `ufAstroRender`. When Vitest closes, it closes the render servers. + * `ufAstroRender`. When Vitest configures the project, it starts the render server for the + * context's root, with the context's plugins, so no test waits for the start (UXF-916); without + * a context, the first render starts it. When Vitest closes, it closes the render servers. + * Outside Vitest, as in the preview (ADR-0088), the hook never runs, and nothing starts early. */ -export function astroBrowserRef(): Plugin { +export function astroBrowserRef(context?: Pick): Plugin { return { name: "unframework:astro-browser-ref", enforce: "pre", @@ -28,6 +32,7 @@ export function astroBrowserRef(): Plugin { }, configureVitest({ vitest }) { vitest.onClose(closeAstroRenderServers); + if (context) startAstroRenderServer(context.root, context.plugins); }, }; } diff --git a/packages/target-astro/src/toolchain/index.ts b/packages/target-astro/src/toolchain/index.ts index d8cf8c634..f442aa173 100644 --- a/packages/target-astro/src/toolchain/index.ts +++ b/packages/target-astro/src/toolchain/index.ts @@ -24,8 +24,9 @@ export const toolchain: Toolchain = { vite(mode, context) { if (mode === "ssr") return astroViteConfig(context.root); // Nothing to pre-bundle: in the browser a compiled component is a plain reference that - // imports nothing, and the unframework plugin keeps virtual ids out of the dep scan. - return { plugins: [astroBrowserRef()] }; + // imports nothing, and the unframework plugin keeps virtual ids out of the dep scan. The + // plugin starts the render server when Vitest configures the project, for the context's root. + return { plugins: [astroBrowserRef(context)] }; }, browserCommands: (context) => ({ ufAstroRender: createAstroRenderCommand(context) }), renderCommandName: "ufAstroRender", diff --git a/packages/target-astro/src/toolchain/render.ts b/packages/target-astro/src/toolchain/render.ts index b247d2a1d..f7e4cbefc 100644 --- a/packages/target-astro/src/toolchain/render.ts +++ b/packages/target-astro/src/toolchain/render.ts @@ -252,6 +252,22 @@ function invalidate(ssr: RunnableDevEnvironment, id: string): void { } } +/** + * Starts the render server for a root now, rather than on the first render (UXF-916). A Vitest + * browser project calls it when Vitest configures the project (`astroBrowserRef`), so the start + * runs before any test, not inside the first test's render, where every Astro file that waits + * in the render queue would pay for it under a busy run. The first render awaits the same + * server; a server that fails to start fails that render with its error, as a lazy start does. + */ +export function startAstroRenderServer( + root: string, + plugins?: () => Promise, +): void { + // Handled here so a failed start with no render yet is not an unhandled rejection: the render + // that awaits the server still rejects with the error. + renderServer(root, plugins).catch(() => undefined); +} + function renderServer( root: string, plugins: (() => Promise) | undefined, diff --git a/packages/target-astro/test/render-boot.test.ts b/packages/target-astro/test/render-boot.test.ts new file mode 100644 index 000000000..58db39726 --- /dev/null +++ b/packages/target-astro/test/render-boot.test.ts @@ -0,0 +1,111 @@ +// When the render server starts (UXF-916): a Vitest browser project starts it when Vitest +// configures the project, so no test's render waits for the start in the render queue. +import { mkdirSync, rmSync } from "node:fs"; +import { join } from "node:path"; + +import type { Plugin } from "vite"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; + +import { astroBrowserRef } from "../src/toolchain/browser.ts"; +import { closeAstroRenderServers, renderAstroComponent } from "../src/toolchain/render.ts"; + +const base = join(import.meta.dirname, "..", ".uf-tmp", `render-boot-${process.pid}`); + +/** A browser project's plugin, as Vitest configures it: only `vitest.onClose` is read. */ +function configure(plugin: Plugin): ReturnType { + const onClose = vi.fn(); + const hook = plugin.configureVitest as unknown as (context: { + vitest: { onClose: (close: () => unknown) => void }; + }) => void; + hook({ vitest: { onClose } }); + return onClose; +} + +/** What a browser project does when it imports a component: the ref plugin's transform. */ +function compile(plugin: Plugin, id: string, source: string): void { + const transform = plugin.transform as { handler(code: string, id: string): unknown }; + transform.handler.call(undefined, source, id); +} + +/** + * The project's plugins, as `ToolchainContext.plugins`: the render server asks for them once, + * as it starts, so each call is one server start. + */ +function projectPlugins(start?: () => void) { + return vi.fn(async (): Promise => { + start?.(); + return []; + }); +} + +describe("the render server's start", { timeout: 60_000 }, () => { + beforeAll(() => mkdirSync(base, { recursive: true })); + afterAll(async () => { + await closeAstroRenderServers(); + rmSync(base, { recursive: true, force: true }); + }); + + it("starts when Vitest configures the browser project, before any render asks for it", async () => { + const root = join(base, "eager"); + mkdirSync(root, { recursive: true }); + const plugins = projectPlugins(); + const plugin = astroBrowserRef({ root, plugins }); + const onClose = configure(plugin); + expect(onClose).toHaveBeenCalledWith(closeAstroRenderServers); + // No render was asked for: only the configure hook can have started the server. + await vi.waitFor(() => expect(plugins).toHaveBeenCalledTimes(1), { timeout: 30_000 }); + + const id = join(root, "Hello.uf.tsx.astro"); + compile(plugin, id, "

hello

\n"); + const result = await renderAstroComponent(root, { id, props: {} }, undefined, plugins); + expect(result).toEqual({ html: "

hello

", console: [] }); + // The render used the server the configure hook started, and started no second one. + expect(plugins).toHaveBeenCalledTimes(1); + }); + + it("starts one server for two projects on one root, as browser:astro and hydrate:astro are", async () => { + const root = join(base, "shared"); + mkdirSync(root, { recursive: true }); + const browser = projectPlugins(); + const hydrate = projectPlugins(); + configure(astroBrowserRef({ root, plugins: browser })); + configure(astroBrowserRef({ root, plugins: hydrate })); + await vi.waitFor(() => expect(browser).toHaveBeenCalledTimes(1), { timeout: 30_000 }); + expect(hydrate).not.toHaveBeenCalled(); + }); + + it("fails the first render with the error of a start that failed, and raises nothing before", async () => { + const root = join(base, "broken"); + mkdirSync(root, { recursive: true }); + const plugins = projectPlugins(() => { + throw new Error("the styling engine's plugin cannot load"); + }); + const plugin = astroBrowserRef({ root, plugins }); + configure(plugin); + await vi.waitFor(() => expect(plugins).toHaveBeenCalledTimes(1), { timeout: 30_000 }); + // A rejection nobody handled yet would fail this file as an unhandled error here. + await new Promise((resolve) => setTimeout(resolve, 50)); + + const id = join(root, "Hello.uf.tsx.astro"); + compile(plugin, id, "

hello

\n"); + await expect( + renderAstroComponent(root, { id, props: {} }, undefined, plugins), + ).rejects.toThrow("the styling engine's plugin cannot load"); + }); + + it("starts nothing for a plugin made without a context, until the first render", async () => { + const root = join(base, "lazy"); + mkdirSync(root, { recursive: true }); + const plugins = projectPlugins(); + const plugin = astroBrowserRef(); + configure(plugin); + await new Promise((resolve) => setTimeout(resolve, 200)); + expect(plugins).not.toHaveBeenCalled(); + + const id = join(root, "Hello.uf.tsx.astro"); + compile(plugin, id, "

lazy

\n"); + const result = await renderAstroComponent(root, { id, props: {} }, undefined, plugins); + expect(result.html).toBe("

lazy

"); + expect(plugins).toHaveBeenCalledTimes(1); + }); +}); From 5872f7f2ae11275caacfc7b76d8cf10d68a7c4eb Mon Sep 17 00:00:00 2001 From: Alex Grozav Date: Fri, 20 Nov 2026 23:44:16 +0200 Subject: [PATCH 2/2] docs(target-astro): say when the render server starts, and add a changeset Co-authored-by: multica-agent --- .changeset/astro-render-boot-uxf-916.md | 7 +++++++ packages/target-astro/README.md | 6 ++++-- packages/target-astro/src/toolchain/render.ts | 5 +---- packages/target-astro/test/render-boot.test.ts | 6 +++--- 4 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 .changeset/astro-render-boot-uxf-916.md diff --git a/.changeset/astro-render-boot-uxf-916.md b/.changeset/astro-render-boot-uxf-916.md new file mode 100644 index 000000000..3108f4149 --- /dev/null +++ b/.changeset/astro-render-boot-uxf-916.md @@ -0,0 +1,7 @@ +--- +"@unframework.dev/target-astro": patch +--- + +Start the render server when Vitest configures an Astro browser project, before any test runs. A host outside Vitest, like the preview, still starts it on the first render. + +The server used to start on the first render, so the first test of each spec file waited for it. In a busy run, that start took up to 75 seconds, and those tests timed out. diff --git a/packages/target-astro/README.md b/packages/target-astro/README.md index 027af8391..9ba793a15 100644 --- a/packages/target-astro/README.md +++ b/packages/target-astro/README.md @@ -197,8 +197,10 @@ Its toolchain, for tests and tooling (the target's main entry never loads it): (`ToolchainContext.plugins`), so a styling engine's plugin resolves its `virtual:` modules, and the engine's stylesheet goes before the markup too (ADR-0064). A module of composables a component imports (`/abs/useToggle.uf.ts`, ADR-0069) is served as the browser project compiles - it, read again on each render. The toolchain names that command as `renderCommandName`, and - `close()` stops its render servers. The mount adapter (`/toolchain/client`) renders through + it, read again on each render. In a Vitest browser project, the render server starts when + Vitest configures the project, before any test runs, so no test waits for the start. Another + host, like the preview, starts it on the first render. The toolchain names that command as + `renderCommandName`, and `close()` stops its render servers. The mount adapter (`/toolchain/client`) renders through `MountOptions.renderCommand` when a host outside Vitest passes one, like the preview, which runs the command behind an endpoint of its own (ADR-0088), and through Vitest's command otherwise. The checks: diff --git a/packages/target-astro/src/toolchain/render.ts b/packages/target-astro/src/toolchain/render.ts index f7e4cbefc..83ce615a9 100644 --- a/packages/target-astro/src/toolchain/render.ts +++ b/packages/target-astro/src/toolchain/render.ts @@ -259,10 +259,7 @@ function invalidate(ssr: RunnableDevEnvironment, id: string): void { * in the render queue would pay for it under a busy run. The first render awaits the same * server; a server that fails to start fails that render with its error, as a lazy start does. */ -export function startAstroRenderServer( - root: string, - plugins?: () => Promise, -): void { +export function startAstroRenderServer(root: string, plugins?: () => Promise): void { // Handled here so a failed start with no render yet is not an unhandled rejection: the render // that awaits the server still rejects with the error. renderServer(root, plugins).catch(() => undefined); diff --git a/packages/target-astro/test/render-boot.test.ts b/packages/target-astro/test/render-boot.test.ts index 58db39726..342c42d91 100644 --- a/packages/target-astro/test/render-boot.test.ts +++ b/packages/target-astro/test/render-boot.test.ts @@ -88,9 +88,9 @@ describe("the render server's start", { timeout: 60_000 }, () => { const id = join(root, "Hello.uf.tsx.astro"); compile(plugin, id, "

hello

\n"); - await expect( - renderAstroComponent(root, { id, props: {} }, undefined, plugins), - ).rejects.toThrow("the styling engine's plugin cannot load"); + await expect(renderAstroComponent(root, { id, props: {} }, undefined, plugins)).rejects.toThrow( + "the styling engine's plugin cannot load", + ); }); it("starts nothing for a plugin made without a context, until the first render", async () => {