@@ -65,10 +65,51 @@ Build full-stack AI applications with Vectorize, Cloudflare’s vector database.
6565
6666Use the [ Browser Rendering API] ( /browser-rendering/ ) to allow your agents to search the web, take screenshots, and directly interact with websites.
6767
68+ ``` sh
69+ npm install @cloudflare/puppeteer --save-dev
70+ ```
71+
6872``` ts
73+ import puppeteer from " @cloudflare/puppeteer" ;
6974
70- ```
75+ interface Env {
76+ MYBROWSER: Fetcher ;
77+ BROWSER_KV_DEMO: KVNamespace ;
78+ }
79+
80+ export default {
81+ async fetch(request : Request , env : Env ): Promise <Response > {
82+ const { searchParams } = new URL (request .url );
83+ const url = searchParams .get (" url" );
84+
85+ if (! url ) {
86+ return new Response (" Please add an ?url=https://example.com/ parameter" );
87+ }
88+
89+ const normalizedUrl = new URL (url ).toString ();
90+ let img = await env .BROWSER_KV_DEMO .get (normalizedUrl , { type: " arrayBuffer" });
7191
92+ if (img === null ) {
93+ const browser = await puppeteer .launch (env .MYBROWSER );
94+ const page = await browser .newPage ();
95+ await page .goto (normalizedUrl );
96+ img = await page .screenshot () as Buffer ;
97+
98+ await env .BROWSER_KV_DEMO .put (normalizedUrl , img , {
99+ expirationTtl: 60 * 60 * 24 , // 24 hours
100+ });
101+
102+ await browser .close ();
103+ }
104+
105+ return new Response (img , {
106+ headers: {
107+ " content-type" : " image/jpeg" ,
108+ },
109+ });
110+ },
111+ };
112+ ```
72113
73114</TabItem >
74115<TabItem label = " AI Gateway" >
0 commit comments