Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 46 lines 1.3 kB view raw
1export interface Context { 2 request: Request; 3 next: () => Promise<Response>; 4} 5 6export const onRequest = async (context: Context) => { 7 const { request } = context; 8 const userAgent = request.headers.get("user-agent")?.toLowerCase() || ""; 9 const url = new URL(request.url); 10 const path = url.pathname; 11 12 const isBot = 13 /bot|baidu|iframely|whatsapp|babbar|bytedance|facebook|meta/.test( 14 userAgent 15 ); 16 17 const createNoCacheResponse = async (targetUrl: string) => { 18 const upstreamResponse = await fetch(targetUrl, { 19 body: ["GET", "HEAD"].includes(request.method) ? undefined : request.body, 20 headers: request.headers, 21 method: request.method, 22 redirect: "follow" 23 }); 24 25 const newHeaders = new Headers(upstreamResponse.headers); 26 newHeaders.set("Cache-Control", "no-store, no-cache, must-revalidate"); 27 28 return new Response(upstreamResponse.body, { 29 headers: newHeaders, 30 status: upstreamResponse.status, 31 statusText: upstreamResponse.statusText 32 }); 33 }; 34 35 if ( 36 isBot && 37 (path.startsWith("/u/") || 38 path.startsWith("/posts/") || 39 path.startsWith("/g/")) 40 ) { 41 const targetUrl = `https://api.hey.xyz/og${path}`; 42 return createNoCacheResponse(targetUrl); 43 } 44 45 return context.next(); 46};