Skip to content

Commit 84ce90e

Browse files
authored
Setup cache for GET routes (#76)
1 parent df9ae8b commit 84ce90e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/routes/v8/artifacts.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ describe('v8 Artifacts API', () => {
9494
expect(res.headers.get('Content-Type')).toBe('application/octet-stream');
9595
expect(res.headers.get('x-artifact-tag')).toBe(artifactTag);
9696
});
97+
98+
test('should return cache headers on every request', async () => {
99+
const request = createArtifactGetRequest(
100+
`http://localhost/v8/artifacts/existing-${artifactId}?teamId=${teamId}`
101+
);
102+
const res = await app.fetch(request, workerEnv, ctx);
103+
expect(res.status).toBe(200);
104+
expect(res.headers.get('Cache-Control')).toBe('max-age=86400, stale-while-revalidate=3600');
105+
});
97106
});
98107

99108
describe('PUT artifact endpoint', () => {

src/routes/v8/artifacts.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { zValidator } from '@hono/zod-validator';
22
import { Hono } from 'hono';
3+
import { cache } from 'hono/cache';
34
import { z } from 'zod';
45
import { Env } from '../..';
56

@@ -42,6 +43,11 @@ artifactRouter.put(
4243
// Hono router .get() method captures both GET and HEAD requests
4344
artifactRouter.get(
4445
'/:artifactId/:teamId?',
46+
cache({
47+
cacheName: 'r2-artifacts',
48+
wait: false,
49+
cacheControl: 'max-age=86400, stale-while-revalidate=3600',
50+
}),
4551
zValidator('param', z.object({ artifactId: z.string() })),
4652
zValidator('query', z.object({ teamId: z.string().optional(), slug: z.string().optional() })),
4753
async (c) => {

0 commit comments

Comments
 (0)