Skip to content

Commit 18e12da

Browse files
committed
fix: cache-control example
1 parent 3fc766d commit 18e12da

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

.changeset/warm-kangaroos-retire.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"framesjs-starter": patch
3+
"docs": patch
4+
---
5+
6+
fix: cache control example

docs/pages/guides/caching.mdx

+28-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,41 @@ description: ""
55

66
# Caching
77

8-
You can set the caching policy of your frames by defining the `Cache-Control` headers of your responses
8+
You can set the caching policy of your frames by defining the `Cache-Control` headers of your frame's **image response**
99

10-
```tsx
10+
Since images are returned as an inline Data URL by default, this will require you to create a separate endpoint for your dynamic images that you want to avoid caching for.
11+
12+
```tsx [frames/route.tsx]
1113
// ...
1214
const handleRequest = frames(async (ctx) => {
1315
return {
16+
image: "/images/my-image",
1417
// ...
15-
headers: {
16-
// Max cache age of 5 seconds
17-
"Cache-Control": "max-age=5",
18-
},
1918
};
2019
});
2120
```
2221

22+
```tsx [frames/images/my-image.tsx]
23+
import { NextRequest } from "next/server";
24+
import { ImageResponse } from "@vercel/og";
25+
26+
export async function GET(req: NextRequest) {
27+
const imageResponse = new ImageResponse(
28+
(
29+
<div tw="bg-purple-800 text-white w-full h-full justify-center items-center flex text-[48px]">
30+
The current time is {new Date().toLocaleString()}
31+
</div>
32+
),
33+
{ width: 1146, height: 600 }
34+
);
35+
36+
// Set the cache control headers to ensure the image is not cached
37+
imageResponse.headers.set("Cache-Control", "public, max-age=0");
38+
39+
return imageResponse;
40+
}
41+
```
42+
43+
Additional context can be passed to the image endpoint via URL query params.
44+
2345
See an example of how to use the `Cache-Control` header in the [Caching example](https://github.com/framesjs/frames.js/tree/main/examples/framesjs-starter/app/examples/new-api-cache-control).
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createFrames } from "frames.js/next";
22

33
export const frames = createFrames({
4-
basePath: "/examples/new-api-cache-control",
4+
basePath: "/examples/new-api-cache-control/frames",
55
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NextRequest } from "next/server";
2+
import { ImageResponse } from "@vercel/og";
3+
4+
export async function GET(req: NextRequest) {
5+
const imageResponse = new ImageResponse(
6+
(
7+
<div tw="bg-purple-800 text-white w-full h-full justify-center items-center flex text-[48px]">
8+
The current time is {new Date().toLocaleString()}
9+
</div>
10+
),
11+
{ width: 1146, height: 600 }
12+
);
13+
14+
// Set the cache control headers to ensure the image is not cached
15+
imageResponse.headers.set("Cache-Control", "public, max-age=0");
16+
17+
return imageResponse;
18+
}

examples/framesjs-starter/app/examples/new-api-cache-control/frames/route.tsx

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
/* eslint-disable react/jsx-key */
22
import { Button } from "frames.js/next";
33
import { frames } from "./frames";
4+
import { vercelURL } from "../../../utils";
45

56
const handleRequest = frames(async (ctx) => {
67
return {
7-
image: (
8-
<div tw="bg-purple-800 text-white w-full h-full justify-center items-center flex">
9-
The current time is {new Date().toLocaleString()}
10-
</div>
11-
),
12-
imageOptions: {
13-
aspectRatio: "1:1",
14-
},
8+
// Separate image response because cache control headers need to be set on the image response
9+
// Add a random query param to ensure the frame action response image is not cached
10+
image: `/images/current-time?t=${Math.random()}`,
1511
buttons: [<Button action="post">Refresh</Button>],
16-
headers: {
17-
// Max cache age in seconds
18-
"Cache-Control": "max-age=5",
19-
},
2012
};
2113
});
2214

0 commit comments

Comments
 (0)