Skip to content

Commit

Permalink
Setup cache for GET routes (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdiRishi authored Sep 2, 2023
1 parent df9ae8b commit 84ce90e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/routes/v8/artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ describe('v8 Artifacts API', () => {
expect(res.headers.get('Content-Type')).toBe('application/octet-stream');
expect(res.headers.get('x-artifact-tag')).toBe(artifactTag);
});

test('should return cache headers on every request', async () => {
const request = createArtifactGetRequest(
`http://localhost/v8/artifacts/existing-${artifactId}?teamId=${teamId}`
);
const res = await app.fetch(request, workerEnv, ctx);
expect(res.status).toBe(200);
expect(res.headers.get('Cache-Control')).toBe('max-age=86400, stale-while-revalidate=3600');
});
});

describe('PUT artifact endpoint', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/routes/v8/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { zValidator } from '@hono/zod-validator';
import { Hono } from 'hono';
import { cache } from 'hono/cache';
import { z } from 'zod';
import { Env } from '../..';

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

0 comments on commit 84ce90e

Please sign in to comment.