Skip to content
Closed
Show file tree
Hide file tree
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
27 changes: 18 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ export class App<State> {
req: Request,
conn: Deno.ServeHandlerInfo = DEFAULT_CONN_INFO,
) => {
if (this.config.cache && req.method === "GET") {
const cachedResponse = await this.config.cache.match(req);
if (cachedResponse) return cachedResponse;
}

const url = new URL(req.url);
// Prevent open redirect attacks
url.pathname = url.pathname.replace(/\/+/g, "/");
Expand Down Expand Up @@ -225,24 +230,28 @@ export class App<State> {
span.setAttribute("http.route", pattern);
}

let response: Response;
try {
if (handlers.length === 1 && handlers[0].length === 1) {
return handlers[0][0](ctx);
}
return await runMiddlewares(handlers, ctx);
response = handlers.length === 1 && handlers[0].length === 1
? await handlers[0][0](ctx)
: await runMiddlewares(handlers, ctx);
} catch (err) {
if (err instanceof HttpError) {
if (err.status >= 500) {
// deno-lint-ignore no-console
console.error(err);
}
return new Response(err.message, { status: err.status });
response = new Response(err.message, { status: err.status });
} else {
// deno-lint-ignore no-console
console.error(err);
response = new Response("Internal server error", { status: 500 });
}

// deno-lint-ignore no-console
console.error(err);
return new Response("Internal server error", { status: 500 });
}
if (this.config.cache && req.method === "GET") {
await this.config.cache.put(req, response.clone());
}
return response;
};
}

Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface FreshConfig {
*/
basePath?: string;
staticDir?: string;
/**
* Server-side cache that stores responses for corresponding GET requests.
*/
cache?: Cache;
}

/**
Expand All @@ -32,6 +36,10 @@ export interface ResolvedFreshConfig {
* The mode Fresh can run in.
*/
mode: "development" | "production";
/**
* Server-side cache that stores responses for corresponding GET requests.
*/
cache?: Cache;
}

export function parseRootPath(root: string, cwd: string): string {
Expand Down Expand Up @@ -65,6 +73,7 @@ export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
basePath: options.basePath ?? "",
staticDir: options.staticDir ?? path.join(root, "static"),
mode: "production",
cache: options.cache,
};
}

Expand Down
4 changes: 3 additions & 1 deletion www/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import "./telemetry.ts";
import { App, fsRoutes, staticFiles, trailingSlashes } from "fresh";

export const app = new App({ root: import.meta.url })
const cache = await caches.open("fresh");

export const app = new App({ root: import.meta.url, cache })
.use(staticFiles())
.use(trailingSlashes("never"));

Expand Down
Loading