Skip to content

Commit 65be906

Browse files
theTysterKianNH
andauthored
[Images] Update example to use ES module syntax. (#16602)
* Doc: Update example to use ES module syntax. https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/ * Fix: Default export left out. * Update src/content/docs/images/transform-images/transform-via-workers.mdx --------- Co-authored-by: Kian <[email protected]>
1 parent 852a7c5 commit 65be906

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

src/content/docs/images/transform-images/transform-via-workers.mdx

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -205,67 +205,65 @@ if (response.ok || response.redirected) {
205205
Assuming you [set up a Worker](/workers/get-started/guide/) on `https://example.com/image-resizing` to handle URLs like `https://example.com/image-resizing?width=80&image=https://example.com/uploads/avatar1.jpg`:
206206

207207
```js
208-
addEventListener("fetch", event => {
209-
event.respondWith(handleRequest(event.request))
210-
})
211-
212208
/**
213209
* Fetch and log a request
214210
* @param {Request} request
215211
*/
216-
async function handleRequest(request) {
217-
// Parse request URL to get access to query string
218-
let url = new URL(request.url)
219-
220-
// Cloudflare-specific options are in the cf object.
221-
let options = { cf: { image: {} } }
222-
223-
// Copy parameters from query string to request options.
224-
// You can implement various different parameters here.
225-
if (url.searchParams.has("fit")) options.cf.image.fit = url.searchParams.get("fit")
226-
if (url.searchParams.has("width")) options.cf.image.width = url.searchParams.get("width")
227-
if (url.searchParams.has("height")) options.cf.image.height = url.searchParams.get("height")
228-
if (url.searchParams.has("quality")) options.cf.image.quality = url.searchParams.get("quality")
229-
230-
// Your Worker is responsible for automatic format negotiation. Check the Accept header.
231-
const accept = request.headers.get("Accept");
232-
if (/image\/avif/.test(accept)) {
233-
options.cf.image.format = 'avif';
234-
} else if (/image\/webp/.test(accept)) {
235-
options.cf.image.format = 'webp';
236-
}
237-
238-
// Get URL of the original (full size) image to resize.
239-
// You could adjust the URL here, e.g., prefix it with a fixed address of your server,
240-
// so that user-visible URLs are shorter and cleaner.
241-
const imageURL = url.searchParams.get("image")
242-
if (!imageURL) return new Response('Missing "image" value', { status: 400 })
243-
244-
try {
245-
// TODO: Customize validation logic
246-
const { hostname, pathname } = new URL(imageURL)
247-
248-
// Optionally, only allow URLs with JPEG, PNG, GIF, or WebP file extensions
249-
// @see https://developers.cloudflare.com/images/url-format#supported-formats-and-limitations
250-
if (!/\.(jpe?g|png|gif|webp)$/i.test(pathname)) {
251-
return new Response('Disallowed file extension', { status: 400 })
212+
export default {
213+
async fetch(request) {
214+
// Parse request URL to get access to query string
215+
let url = new URL(request.url)
216+
217+
// Cloudflare-specific options are in the cf object.
218+
let options = { cf: { image: {} } }
219+
220+
// Copy parameters from query string to request options.
221+
// You can implement various different parameters here.
222+
if (url.searchParams.has("fit")) options.cf.image.fit = url.searchParams.get("fit")
223+
if (url.searchParams.has("width")) options.cf.image.width = url.searchParams.get("width")
224+
if (url.searchParams.has("height")) options.cf.image.height = url.searchParams.get("height")
225+
if (url.searchParams.has("quality")) options.cf.image.quality = url.searchParams.get("quality")
226+
227+
// Your Worker is responsible for automatic format negotiation. Check the Accept header.
228+
const accept = request.headers.get("Accept");
229+
if (/image\/avif/.test(accept)) {
230+
options.cf.image.format = 'avif';
231+
} else if (/image\/webp/.test(accept)) {
232+
options.cf.image.format = 'webp';
252233
}
253234
254-
// Demo: Only accept "example.com" images
255-
if (hostname !== 'example.com') {
256-
return new Response('Must use "example.com" source images', { status: 403 })
235+
// Get URL of the original (full size) image to resize.
236+
// You could adjust the URL here, e.g., prefix it with a fixed address of your server,
237+
// so that user-visible URLs are shorter and cleaner.
238+
const imageURL = url.searchParams.get("image")
239+
if (!imageURL) return new Response('Missing "image" value', { status: 400 })
240+
241+
try {
242+
// TODO: Customize validation logic
243+
const { hostname, pathname } = new URL(imageURL)
244+
245+
// Optionally, only allow URLs with JPEG, PNG, GIF, or WebP file extensions
246+
// @see https://developers.cloudflare.com/images/url-format#supported-formats-and-limitations
247+
if (!/\.(jpe?g|png|gif|webp)$/i.test(pathname)) {
248+
return new Response('Disallowed file extension', { status: 400 })
249+
}
250+
251+
// Demo: Only accept "example.com" images
252+
if (hostname !== 'example.com') {
253+
return new Response('Must use "example.com" source images', { status: 403 })
254+
}
255+
} catch (err) {
256+
return new Response('Invalid "image" value', { status: 400 })
257257
}
258-
} catch (err) {
259-
return new Response('Invalid "image" value', { status: 400 })
260-
}
261258
262-
// Build a request that passes through request headers
263-
const imageRequest = new Request(imageURL, {
264-
headers: request.headers
265-
})
259+
// Build a request that passes through request headers
260+
const imageRequest = new Request(imageURL, {
261+
headers: request.headers
262+
})
266263
267-
// Returning fetch() with resizing options will pass through response with the resized image.
268-
return fetch(imageRequest, options)
264+
// Returning fetch() with resizing options will pass through response with the resized image.
265+
return fetch(imageRequest, options)
266+
}
269267
}
270268
```
271269

0 commit comments

Comments
 (0)