-
-
Save hmnd/5f6e7810c6e1feb8b5b66dab57769116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import puppeteer from '@cloudflare/puppeteer'; | |
/** | |
* Welcome to Cloudflare Workers! This is your first worker. | |
* | |
* - Run `npm run dev` in your terminal to start a development server | |
* - Open a browser tab at http://localhost:8787/ to see your worker in action | |
* - Run `npm run deploy` to publish your worker | |
* | |
* Bind resources to your worker in `wrangler.toml`. After adding bindings, a type definition for the | |
* `Env` object can be regenerated with `npm run cf-typegen`. | |
* | |
* Learn more at https://developers.cloudflare.com/workers/ | |
*/ | |
export type Env = { | |
MY_BROWSER: Fetcher; | |
}; | |
export default { | |
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { | |
const browser = await puppeteer.launch(env.MY_BROWSER); | |
const page = await browser.newPage(); | |
await page.goto('https://www.google.com'); | |
return new Response(await page.screenshot(), { | |
headers: { | |
'content-type': 'image/jpeg', | |
}, | |
}); | |
}, | |
} satisfies ExportedHandler<Env>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#:schema node_modules/wrangler/config-schema.json | |
name = "browser-worker" | |
main = "src/index.ts" | |
compatibility_date = "2024-06-05" | |
compatibility_flags = ["nodejs_compat"] | |
# Bind a headless browser instance running on Cloudflare's global network. | |
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering | |
[browser] | |
binding = "MY_BROWSER" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment