diff --git a/packages/app/README.md b/packages/app/README.md index ff0d5563d1a..ff6ca832d9d 100644 --- a/packages/app/README.md +++ b/packages/app/README.md @@ -39,7 +39,7 @@ bun run test:e2e:local bun run test:e2e:local -- --grep "settings" ``` -CI builds the app once and runs the same suite against Vite preview, serving production assets from `dist`. Managed built runs never reuse an existing server, so a running dev server cannot silently replace the production build. To run this mode locally: +CI builds the app once and runs the same suite against a static Bun server (`e2e/utils/serve.ts`) that serves the production assets in `dist` with Vite preview's SPA fallback rules. Managed built runs never reuse an existing server, so a running dev server cannot silently replace the production build. To run this mode locally: ```bash bun run test:e2e:built @@ -64,9 +64,9 @@ Compiled CLI startup and service lifecycle coverage runs separately in CI via `p Environment options: -- `PLAYWRIGHT_BUILD=1` (build and preview locally; always enabled when `CI` is set) +- `PLAYWRIGHT_BUILD=1` (build and serve `dist` locally; always enabled when `CI` is set) - `PLAYWRIGHT_SERVER_HOST` / `PLAYWRIGHT_SERVER_PORT` (dev fixture API address, default: `127.0.0.1:4096`; built runs use the app's origin, matching production) -- `PLAYWRIGHT_PORT` (managed dev or preview server port, default: `3000`) +- `PLAYWRIGHT_PORT` (managed dev or static server port, default: `3000`) - `PLAYWRIGHT_BASE_URL` (use an externally managed app instead of starting a server; otherwise defaults to `http://127.0.0.1:`) ## Deployment diff --git a/packages/app/e2e/utils/serve.ts b/packages/app/e2e/utils/serve.ts new file mode 100644 index 00000000000..f1629485841 --- /dev/null +++ b/packages/app/e2e/utils/serve.ts @@ -0,0 +1,65 @@ +import { stat } from "node:fs/promises" +import path from "node:path" +import { parseArgs } from "node:util" + +// Serves the e2e build with vite preview's routing rules. vite preview checks the case of every requested +// path with a synchronous directory read on case-insensitive file systems (Windows, macOS), and the +// 3000-file _assets directory makes that the bottleneck for every page load across all workers. +const options = parseArgs({ options: { host: { type: "string" }, port: { type: "string" } } }).values + +const dist = path.resolve(import.meta.dirname, "../../dist") + +// Sent as text like vite's index.html middleware, so a media Range request that falls back gets a plain 200. +const index = await Bun.file(path.join(dist, "index.html")).text() + +Bun.serve({ + hostname: options.host, + port: Number(options.port), + async fetch(request) { + const pathname = decode(new URL(request.url).pathname) + + if (pathname === undefined) return new Response(null, { status: 404 }) + + const target = path.resolve(dist, `.${pathname}`) + + // Like vite's existsSync, a name the file system rejects (too long, NUL) is treated as missing. + const stats = target.startsWith(dist + path.sep) ? await stat(target).catch(() => undefined) : undefined + + if (stats?.isFile()) { + const file = Bun.file(target) + + return new Response(file, { + // Bun names .aac audio/x-aac, and only keeps the type on HEAD responses when it is set explicitly. + headers: { "content-type": path.extname(target) === ".aac" ? "audio/aac" : file.type }, + }) + } + + if (fallback(request, pathname)) + return new Response(index, { headers: { "content-type": "text/html;charset=utf-8" } }) + + return new Response(null, { status: 404 }) + }, +}) + +function decode(pathname: string) { + try { + return decodeURIComponent(pathname) + } catch { + return undefined + } +} + +// Mirrors vite's htmlFallbackMiddleware: only page loads get index.html, so a missing script or a fetch +// asking for JSON still sees a 404. +function fallback(request: Request, pathname: string) { + if (request.method !== "GET" && request.method !== "HEAD") return false + + if (pathname === "/favicon.ico") return false + + // vite's index.html middleware never answers a script load with the page, so a missing chunk stays a 404. + if (request.headers.get("sec-fetch-dest") === "script") return false + + const accept = request.headers.get("accept") + + return !accept || accept.includes("text/html") || accept.includes("*/*") +} diff --git a/packages/app/playwright.config.ts b/packages/app/playwright.config.ts index bfabc8ca7cb..d3b1d353183 100644 --- a/packages/app/playwright.config.ts +++ b/packages/app/playwright.config.ts @@ -10,7 +10,7 @@ if (url.protocol !== "http:") throw new Error("E2E fixtures require an http:// a const built = !!process.env.CI || process.env.PLAYWRIGHT_BUILD === "1" -// Production connects to its own origin, so fixture URLs must match the preview server. +// Production connects to its own origin, so fixture URLs must match the static server. if (built) { process.env.PLAYWRIGHT_SERVER_HOST = url.hostname process.env.PLAYWRIGHT_SERVER_PORT = url.port || "80" @@ -21,7 +21,7 @@ const serverHost = process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1" const serverPort = process.env.PLAYWRIGHT_SERVER_PORT ?? "4096" const command = built - ? `bun run build && bun run serve -- --host 127.0.0.1 --port ${port} --strictPort` + ? `bun run build && bun e2e/utils/serve.ts --host 127.0.0.1 --port ${port}` : `bun run dev -- --host 127.0.0.1 --port ${port} --strictPort` const workers = Number(process.env.PLAYWRIGHT_WORKERS ?? (process.env.CI ? 5 : 0)) || undefined diff --git a/packages/app/test-browser/playwright-config.test.ts b/packages/app/test-browser/playwright-config.test.ts index 1fa7cfc9885..829d4910b0a 100644 --- a/packages/app/test-browser/playwright-config.test.ts +++ b/packages/app/test-browser/playwright-config.test.ts @@ -27,7 +27,7 @@ test.each([ expect(config.webServer.url).toBe(config.use.baseURL) expect(config.webServer.command).toBe( built - ? "bun run build && bun run serve -- --host 127.0.0.1 --port 4321 --strictPort" + ? "bun run build && bun e2e/utils/serve.ts --host 127.0.0.1 --port 4321" : "bun run dev -- --host 127.0.0.1 --port 4321 --strictPort", ) expect(config.webServer.reuseExistingServer).toBe(!built)