From ad760975812736a21606f02ab532dc5a77ab2347 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 5 Aug 2025 14:56:35 +0200 Subject: [PATCH] fix: windows static file path handling (#3188) --- src/dev/builder_test.ts | 79 ++++++++++++++++++++++++++++++++++++++ src/dev/dev_build_cache.ts | 3 +- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts index d76e3d68..90e7779a 100644 --- a/src/dev/builder_test.ts +++ b/src/dev/builder_test.ts @@ -9,6 +9,7 @@ import { getStdOutput, withChildProcessServer, } from "../../tests/test_utils.tsx"; +import { staticFiles } from "../middlewares/static_files.ts"; Deno.test({ name: "Builder - chain onTransformStaticFile", @@ -388,6 +389,84 @@ Deno.test({ sanitizeResources: false, }); +Deno.test({ + name: "Builder - serves static files in subdir", + fn: async () => { + const root = path.join(import.meta.dirname!, "..", ".."); + await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" }); + const tmp = _tmp.dir; + + const app = new App() + .use(staticFiles()) + .get("/", () => new Response("no")); + + await writeFiles(tmp, { + "static/foo.txt": "ok", + "static/test/foo.txt": "ok", + }); + + const builder = new Builder({ + root: tmp, + }); + + const controller = new AbortController(); + const waiter = Promise.withResolvers(); + await builder.listen(() => Promise.resolve>(app), { + signal: controller.signal, + async onListen(addr) { + try { + let res = await fetch(`http://localhost:${addr.port}/foo.txt`); + expect(await res.text()).toEqual("ok"); + + res = await fetch(`http://localhost:${addr.port}/test/foo.txt`); + expect(await res.text()).toEqual("ok"); + + controller.abort(); + waiter.resolve(); + } catch (err) { + waiter.reject(err); + } + }, + }); + + await waiter.promise; + }, + sanitizeOps: false, + sanitizeResources: false, +}); + +Deno.test({ + name: "Builder - serves static files in subdir in prod", + fn: async () => { + const root = path.join(import.meta.dirname!, "..", ".."); + await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" }); + const tmp = _tmp.dir; + + await writeFiles(tmp, { + "main.ts": `import { App, staticFiles } from "fresh"; +export const app = new App() + .use(staticFiles());`, + "static/foo.txt": "ok", + "static/test/foo.txt": "ok", + }); + + await new Builder({ root: tmp }).build(); + + await withChildProcessServer( + { cwd: tmp, args: ["serve", "-A", "_fresh/server.js"] }, + async (address) => { + let res = await fetch(`${address}/foo.txt`); + expect(await res.text()).toEqual("ok"); + + res = await fetch(`${address}/test/foo.txt`); + expect(await res.text()).toEqual("ok"); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test({ name: "Builder - dev server error_overlay ignores _app and _layout", fn: async () => { diff --git a/src/dev/dev_build_cache.ts b/src/dev/dev_build_cache.ts index 1ea18b05..b6ed607d 100644 --- a/src/dev/dev_build_cache.ts +++ b/src/dev/dev_build_cache.ts @@ -133,7 +133,7 @@ export class MemoryBuildCache implements DevBuildCache { `Processed file resolved outside of static dir ${file.path}`, ); } - const pathname = `/${relative}`; + const pathname = new URL(relative, "http://localhost").pathname; this.addProcessedFile(pathname, file.content, null); } @@ -145,6 +145,7 @@ export class MemoryBuildCache implements DevBuildCache { const filePath = path.join(this.#config.staticDir, pathname); const relative = path.relative(this.#config.staticDir, filePath); if (!relative.startsWith(".") && (await Deno.stat(filePath)).isFile) { + const pathname = new URL(relative, "http://localhost").pathname; this.addUnprocessedFile(pathname, this.#config.staticDir); return this.readFile(pathname); }