-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
31 lines (30 loc) · 1015 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { createOgImage } from "./og_image.ts";
import { type Context, createHttpError, Status } from "./middleware_deps.ts";
/**
* The middleware generates cached dynamic Open Graph images that you can embed
* in your html `meta` tags.
* ```ts
* get({ pathname: "/{:text}.png" }, serveOgImage);
* ```
*/
export async function serveOgImage<C extends Context>(ctx: C): Promise<C> {
try {
const canvas = await createOgImage(ctx.request);
ctx.response = new Response(canvas, {
headers: {
"content-type": "image/png",
"Cache-Control":
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`,
},
});
console.log("canvas");
return ctx;
} catch (error: unknown) {
console.log("error", error);
throw error instanceof URIError
? createHttpError(Status.BadRequest, error.message)
: error instanceof URIError
? createHttpError(Status.InternalServerError, error.message)
: new Error("[non-error thrown]");
}
}