diff --git a/src/server/context.ts b/src/server/context.ts index c12fdcd3..4fe70bdf 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -244,7 +244,10 @@ export class ServerContext { const staticFiles: StaticFile[] = []; try { - const staticFolder = new URL("./static", manifest.baseUrl); + const staticFolder = new URL( + opts.staticDir ?? "./static", + manifest.baseUrl, + ); // TODO(lucacasonato): remove the extranious Deno.readDir when // https://github.com/denoland/deno_std/issues/1310 is fixed. for await (const _ of Deno.readDir(fromFileUrl(staticFolder))) { diff --git a/src/server/types.ts b/src/server/types.ts index 6918fa0a..930f339e 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -9,6 +9,7 @@ export type StartOptions = ServeInit & FreshOptions; export interface FreshOptions { render?: RenderFunction; plugins?: Plugin[]; + staticDir?: string; } export type RenderFunction = ( diff --git a/tests/fixture/custom_static/custom.txt b/tests/fixture/custom_static/custom.txt new file mode 100644 index 00000000..87245193 --- /dev/null +++ b/tests/fixture/custom_static/custom.txt @@ -0,0 +1 @@ +dir \ No newline at end of file diff --git a/tests/main_test.ts b/tests/main_test.ts index 55fd2452..8fb14072 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -191,6 +191,34 @@ Deno.test("/foo/:path*", async () => { assert(body.includes("bar/baz")); }); +Deno.test("static files in custom directory", async () => { + const newCtx = await ServerContext.fromManifest(manifest, { + ...options, + staticDir: "./custom_static", + }); + const newRouter = (req: Request) => { + return newCtx.handler()(req, { + localAddr: { + transport: "tcp", + hostname: "127.0.0.1", + port: 80, + }, + remoteAddr: { + transport: "tcp", + hostname: "127.0.0.1", + port: 80, + }, + }); + }; + + const resp = await newRouter( + new Request("https://fresh.deno.dev/custom.txt"), + ); + assertEquals(resp.status, Status.OK); + const body = await resp.text(); + assert(body.startsWith("dir")); +}); + Deno.test("static file - by file path", async () => { const resp = await router(new Request("https://fresh.deno.dev/foo.txt")); assertEquals(resp.status, Status.OK);