Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/fresh/src/middlewares/static_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getBuildCache } from "../context.ts";
* app.use(staticFiles());
* ```
*/
export function staticFiles<T>(): Middleware<T> {
export function staticFiles<T>(serveDir = "/static"): Middleware<T> {
return async function freshServeStaticFiles(ctx) {
const { req, url, config } = ctx;

Expand All @@ -20,14 +20,17 @@ export function staticFiles<T>(): Middleware<T> {

let pathname = decodeURIComponent(url.pathname);
if (config.basePath) {
pathname = pathname !== config.basePath
? pathname.slice(config.basePath.length)
: "/";
pathname =
pathname !== config.basePath
? pathname.slice(config.basePath.length)
: "/";
}

// Fast path bail out
const startTime = performance.now() + performance.timeOrigin;
const file = await buildCache.readFile(pathname);

const modifiedPath = pathname.replace("/static", serveDir);
const file = await buildCache.readFile(modifiedPath);
if (pathname === "/" || file === null) {
// Optimization: Prevent long responses for favicon.ico requests
if (pathname === "/favicon.ico") {
Expand Down
Loading