a tool for shared writing and social publishing
1import { IdResolver } from "@atproto/identity";
2import { NextRequest, NextResponse } from "next/server";
3let idResolver = new IdResolver();
4
5export async function GET(req: NextRequest) {
6 const url = new URL(req.url);
7 const params = {
8 did: url.searchParams.get("did") ?? "",
9 cid: url.searchParams.get("cid") ?? "",
10 };
11 if (!params.did || !params.cid)
12 return new NextResponse(null, { status: 404 });
13
14 let identity = await idResolver.did.resolve(params.did);
15 let service = identity?.service?.find((f) => f.id === "#atproto_pds");
16 if (!service) return new NextResponse(null, { status: 404 });
17 const response = await fetch(
18 `${service.serviceEndpoint}/xrpc/com.atproto.sync.getBlob?did=${params.did}&cid=${params.cid}`,
19 {
20 headers: {
21 "Accept-Encoding": "gzip, deflate, br, zstd",
22 },
23 },
24 );
25
26 // Clone the response to modify headers
27 const cachedResponse = new Response(response.body, response);
28
29 // Set cache-control header to cache indefinitely
30 cachedResponse.headers.set(
31 "Cache-Control",
32 "public, max-age=31536000, immutable, s-maxage=86400, stale-while-revalidate=604800",
33 );
34 cachedResponse.headers.set(
35 "CDN-Cache-Control",
36 "s-maxage=86400, stale-while-revalidate=86400",
37 );
38
39 return cachedResponse;
40}