Skip to content

Instantly share code, notes, and snippets.

@hmnd

hmnd/worker.ts Secret

Created June 7, 2024 22:40
Show Gist options
  • Save hmnd/5f6e7810c6e1feb8b5b66dab57769116 to your computer and use it in GitHub Desktop.
Save hmnd/5f6e7810c6e1feb8b5b66dab57769116 to your computer and use it in GitHub Desktop.
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>;
#: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