|
| 1 | +--- |
| 2 | +pcx_content_type: get-started |
| 3 | +title: Reuse sessions with geolocation |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Render, PackageManagers, WranglerConfig } from "~/components"; |
| 9 | + |
| 10 | +This guide explains how to build a Cloudflare Worker that renders a web page using a fork of Puppeteer. Each request launches a new browser session (with the session location set to Afghanistan in this case) to navigate to the specified URL and return the HTML content of the page. |
| 11 | + |
| 12 | +## 1. Create a Worker project |
| 13 | + |
| 14 | +[Cloudflare Workers](/workers/) provides a serverless execution environment that allows you to create new applications or augment existing ones without configuring or maintaining infrastructure. Your Worker application is a container to interact with a headless browser to do actions, such as taking screenshots. |
| 15 | + |
| 16 | +Create a new Worker project named `browser-worker` by running: |
| 17 | + |
| 18 | +<PackageManagers |
| 19 | + type="create" |
| 20 | + pkg="cloudflare@latest" |
| 21 | + args={"browser-worker"} |
| 22 | +/> |
| 23 | + |
| 24 | +<Render |
| 25 | + file="c3-post-run-steps" |
| 26 | + product="workers" |
| 27 | + params={{ |
| 28 | + category: "hello-world", |
| 29 | + type: "Worker only", |
| 30 | + lang: "TypeScript", |
| 31 | + }} |
| 32 | +/> |
| 33 | + |
| 34 | +## 2. Install Puppeteer |
| 35 | + |
| 36 | +In your `browser-worker` directory, install Cloudflare's [fork of Puppeteer](/browser-rendering/platform/puppeteer/): |
| 37 | + |
| 38 | +```sh |
| 39 | +npm install @cloudflare/puppeteer --save-dev |
| 40 | +``` |
| 41 | + |
| 42 | +## 3. Configure the [Wrangler configuration file](/workers/wrangler/configuration/) |
| 43 | + |
| 44 | +<WranglerConfig> |
| 45 | +```toml |
| 46 | +name = "browser-worker" |
| 47 | +main = "src/index.ts" |
| 48 | +compatibility_date = "2023-03-14" |
| 49 | +compatibility_flags = [ "nodejs_compat" ] |
| 50 | + |
| 51 | +browser = { binding = "MYBROWSER" } |
| 52 | + |
| 53 | +```` |
| 54 | +</WranglerConfig> |
| 55 | + |
| 56 | +The binding `MYBROWSER` allows the Worker to interact with the headless browser. |
| 57 | + |
| 58 | +## 4. Implement the Worker Code |
| 59 | + |
| 60 | +Replace your default code with the following updated script. This code launches a new browser session for every request, sets the session’s location to Afghanistan, navigates to the provided URL, and returns the HTML content of the page. |
| 61 | + |
| 62 | +```ts |
| 63 | + |
| 64 | +import puppeteer from "@cloudflare/puppeteer"; |
| 65 | + |
| 66 | +export default { |
| 67 | + async fetch(request, env, ctx): Promise<Response> { |
| 68 | + const { searchParams } = new URL(request.url); |
| 69 | + let url = searchParams.get('url'); |
| 70 | + let html; |
| 71 | + if (url) { |
| 72 | + url = new URL(url).toString(); // Normalize the URL |
| 73 | + // Launch a new browser session with the location set to Afghanistan ('AF') |
| 74 | + const browser = await puppeteer.launch(env.MYBROWSER, { location: 'AF' }); |
| 75 | + const page = await browser.newPage(); |
| 76 | + await page.goto(url); |
| 77 | + html = await page.content(); |
| 78 | + await browser.close(); |
| 79 | + return new Response(html); |
| 80 | + } else { |
| 81 | + return new Response('Please add an ?url=https://example.com/ parameter'); |
| 82 | + } |
| 83 | + }, |
| 84 | +} satisfies ExportedHandler<Env>; |
| 85 | +``` |
| 86 | + |
| 87 | +### Key points |
| 88 | + |
| 89 | +- Session management: |
| 90 | + A new browser session is launched for every incoming request. The session is automatically closed after retrieving the page content. |
| 91 | + |
| 92 | +- Location specification: |
| 93 | + The browser session is configured to run with a location parameter set to `'AF'` (Afghanistan). |
| 94 | + |
| 95 | +- URL validation: |
| 96 | + The script checks for a valid URL in the query string, normalizes it, and handles the case where the URL is not provided. |
| 97 | + |
| 98 | +## 5. Test the Worker |
| 99 | + |
| 100 | +Since local testing for browser rendering is not supported, run your Worker remotely by executing: |
| 101 | + |
| 102 | +```sh |
| 103 | +npx wrangler dev --remote |
| 104 | +``` |
| 105 | + |
| 106 | +Test the Worker by navigating to the following URL (replace `<LOCAL_HOST_URL>` with the appropriate endpoint): |
| 107 | + |
| 108 | +```sh |
| 109 | +<LOCAL_HOST_URL>/?url=https://example.com |
| 110 | +``` |
| 111 | + |
| 112 | +## 6. Deploy the Worker |
| 113 | + |
| 114 | +After testing, deploy your Worker to Cloudflare’s global network with: |
| 115 | + |
| 116 | +```sh |
| 117 | +npx wrangler deploy |
| 118 | +``` |
| 119 | + |
| 120 | +Once deployed, your Worker will be accessible at: |
| 121 | + |
| 122 | +```sh |
| 123 | + |
| 124 | +<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev/?url=https://example.com``` |
| 125 | +
|
| 126 | +``` |
| 127 | +```` |
0 commit comments