Skip to content

Fix Images docs code examples (7 files)#28396

Merged
elithrar merged 8 commits into
productionfrom
opencode/issue28270-20260217171319
Feb 23, 2026
Merged

Fix Images docs code examples (7 files)#28396
elithrar merged 8 commits into
productionfrom
opencode/issue28270-20260217171319

Conversation

@ask-bonk

@ask-bonk ask-bonk Bot commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Here is a summary of all fixes made across 7 files, organized by severity:

Summary of fixes

CRITICAL (would cause runtime errors)

  1. serve-private-images.mdx:77event.request.url references undefined event variable. Fixed to request.url (the actual parameter name in the fetch(request, env, ctx) handler).

  2. upload-file-worker.mdx:47 — Missing closing parenthesis in new File([bytes], 'image.jpg') — was new File([bytes], 'image.jpg'; which is a syntax error.

  3. transform-via-workers.mdx:195response.redirect(imageURL, 307) called as an instance method, but redirect() is a static method on Response. Fixed to Response.redirect(imageURL, 307).

  4. control-origin-access.mdx:45,88requestURL.path is not a property on URL. Fixed to requestURL.pathname.

  5. control-origin-access.mdx:124 — Missing comma after headers: signedHeaders in the fetch options object literal. This is a syntax error.

  6. draw-overlays.mdx:117,131,144,158 — Semicolons (;) used after array values inside object literals (draw: [...];) instead of commas. Syntax error in all 4 snippet examples.

HIGH (type errors / wrong behavior)

  1. transform-via-workers.mdx:235-239url.searchParams.get("width") returns string | null, but options.cf.image.width expects number. The string would be passed directly, causing either a type error or unexpected behavior. Fixed by wrapping in parseInt(..., 10).

  2. control-origin-access.mdx:65,69 — Same issue: searchParams.get("width") returns a string, so > 1000 performs string comparison (e.g., "9" > "1000" is true). Fixed by parsing to integer first. Also replaced throw Error(...) with return new Response(...) for proper HTTP error handling.

  3. draw-overlays.mdx:82-86.output() returns Promise<ImageTransformationResult>, so chaining .response() directly on it (without await) calls .response() on the Promise object, not the result. Fixed to (await ...output()).response() pattern.

  4. watermark-from-kv.mdx:46result.response() called twice: once to create response (line 42), then again on the return (line 46). Since response() creates a new Response each time, the second call may consume an already-read stream. Fixed to return the existing response variable.

MEDIUM (code quality / misleading patterns)

  1. transform-via-workers.mdx:161-169 — Used deprecated Service Worker addEventListener("fetch") syntax. Updated to modern ES module export default { async fetch(request) { ... } } pattern, consistent with all other examples on the page.

  2. optimize-user-uploaded-image.mdx (3 code blocks) — Contained var __defProp = Object.defineProperty; var __name = ... compiled JavaScript output (from esbuild/tsc). This is confusing build artifact boilerplate that readers would copy. Removed and replaced with clean source code.

Files NOT changed (reviewed, no issues found)

  • transcode-from-workers-ai.mdx — Code is correct
  • integrate-with-frameworks.mdx — Code is correct (Next.js loader examples are valid)
  • bindings.mdx — Code is correct (uses proper (await ...).response() pattern)

Closes #28270

github run

Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@ask-bonk
ask-bonk Bot requested a review from a team February 17, 2026 17:22
@ask-bonk ask-bonk Bot mentioned this pull request Feb 17, 2026
@github-actions github-actions Bot added product:images Related to Image Resizing product size/l labels Feb 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/images/ @ToriLindsay, @cloudflare/pcx-technical-writing, @renandincer, @third774

@cloudflare cloudflare deleted a comment from rohanmajhi727-gif Feb 17, 2026
@cloudflare cloudflare deleted a comment from rohanmajhi727-gif Feb 17, 2026
@cloudflare cloudflare deleted a comment from rohanmajhi727-gif Feb 17, 2026
@elithrar

Copy link
Copy Markdown
Collaborator

/bonk run a final review using the code-review skill, make any changes, and summarize your fixes.

…r() for Images binding input

env.IMAGES.input() expects ReadableStream<Uint8Array>, not ArrayBuffer.
Changed all 4 code blocks to use file.stream() consistently.
@ask-bonk

ask-bonk Bot commented Feb 17, 2026

Copy link
Copy Markdown
Contributor Author

Code Review Summary

Reviewed all 7 changed files against the @cloudflare/workers-types type definitions using the code-review skill. Here are the findings:

Issue found and fixed

[HIGH] optimize-user-uploaded-image.mdxenv.IMAGES.input() called with ArrayBuffer instead of ReadableStream

optimize-user-uploaded-image.mdx:234,293 (and 2 more code blocks) — The code called env.IMAGES.input(fileBuffer) where fileBuffer was an ArrayBuffer from await file.arrayBuffer(). Per the ImagesBinding type definition (node_modules/@cloudflare/workers-types/index.d.ts:10822), input() accepts ReadableStream<Uint8Array>, not ArrayBuffer. This would cause a runtime type error.

Fix: Changed all 4 code blocks to use file.stream() (which returns ReadableStream) instead of await file.arrayBuffer(). Updated the variable name from fileBuffer to fileStream, the validation check from typeof file.arrayBuffer !== "function" to typeof file.stream !== "function", and the prose to match.

Files reviewed — no additional issues

File Status
watermark-from-kv.mdx Correct — uses source.body (ReadableStream) for .input(), proper (await ...).response() pattern, ctx.waitUntil for cache
serve-private-images.mdx Correct — request.url fix is good, crypto HMAC usage is correct
control-origin-access.mdx Correct — .pathname fix, parseInt() fix, Response.redirect() fix, missing comma fix all verified
draw-overlays.mdx Correct — semicolons-to-commas fix verified, (await ...output()).response() pattern is correct
transform-via-workers.mdx Correct — Response.redirect() static method fix, parseInt() for numeric params, ES module syntax all verified
upload-file-worker.mdx Correct — new File([bytes], "image.png") syntax fix verified

github run

@elithrar
elithrar requested a review from deannalam February 17, 2026 18:22
Comment thread src/content/docs/images/upload-images/upload-file-worker.mdx
Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@ask-bonk

ask-bonk Bot commented Feb 17, 2026

Copy link
Copy Markdown
Contributor Author

All changes pass validation. Here's a summary:

Changes made

Wrapped all ```ts code blocks across the Images docs in <TypeScriptExample> components, which auto-transpile TypeScript to JavaScript and show both in synced tabs.

Files changed

File Change
upload-file-worker.mdx Wrapped both ts code blocks in <TypeScriptExample>, added import
watermark-from-kv.mdx Wrapped the ts code block in <TypeScriptExample>, added import
bindings.mdx Wrapped both ts code blocks in <TypeScriptExample>, added to existing import

Files reviewed — no change needed

File Reason
serve-private-images.mdx Already uses <TypeScriptExample>
control-origin-access.mdx All code blocks are js (plain JavaScript examples, no TS syntax)
draw-overlays.mdx All code blocks are js
transform-via-workers.mdx All code blocks are js
optimize-user-uploaded-image.mdx All code blocks are js (step-by-step tutorial with progressive examples)
integrate-with-frameworks.mdx ts blocks are Next.js config/loader files, not Worker code — <TypeScriptExample> would not produce useful JS output for these

npm run check passes with 0 errors.

github run

Comment thread src/content/docs/images/tutorials/optimize-user-uploaded-image.mdx Outdated
@ask-bonk

This comment was marked as outdated.

@ask-bonk

ask-bonk Bot commented Feb 17, 2026

Copy link
Copy Markdown
Contributor Author

Summary

Converted all 5 Workers code blocks in optimize-user-uploaded-image.mdx from plain js fences to TypeScript-first using <TypeScriptExample> (which auto-transpiles to show both TS and JS tabs).

Changes made

Block Section Changes
1 Build your frontend Added Env interface, typed handler params, satisfies ExportedHandler<Env>
2 Read the uploaded image Typed handler, (err as Error).message
3 Set up the overlay image Typed assetUrl() params/return, separated watermarkResponse/watermarkStream
4 Watermark and transcode Added !watermarkStream null guard before .input()
5 Full example (Upload to R2) Full Env interface, all type annotations, null guard, catch returns 500, method fallthrough returns 405

Code review findings (fixed)

  • HIGH: watermarkStream (Response.body) is ReadableStream | null but ImagesBinding.input() requires ReadableStream<Uint8Array> -- added null guards in blocks 4 and 5
  • MEDIUM: Missing Response returns in catch block and method fallthrough in the final executable example -- added proper error responses

npm run check passes with 0 errors.

github run

@elithrar
elithrar enabled auto-merge (squash) February 20, 2026 14:57
@elithrar

Copy link
Copy Markdown
Collaborator

/bonk fix the build issues. then re-check.

15:13:09   ├─ /images/reference/troubleshooting/index.html (+33ms) 
Unexpected token (2:35)
  1 | // Fetch the watermark from Workers Assets, R2, KV etc
> 2 | const watermark                 = ...
    |                                   ^
  3 |
  4 | // Fetch the main image
  5 | const image                 = ...
  Hint:
    This issue often occurs when your MDX component encounters runtime errors.
  Location:
    undefined:0:0
  Stack trace:
    at pa (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:14:16245)
    at Object.parse (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:15:2919)
    at async coreFormat (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/index.mjs:17287:25)
    at async format2 (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/index.mjs:19051:25)
  Caused by:
  Unexpected token (2:34)
    at r (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:3:12378)
    at ke.unexpected (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:7:19940)
    at ke.parseExprAtom (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:5:5334)
    at ke.parseUpdate (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:7:112313)
    at ke.parseMaybeUnaryOrPrivate (file:///home/runner/work/cloudflare-docs/cloudflare-docs/node_modules/prettier/plugins/babel.mjs:7:108840)
15:13:09   ├─ /images/transform-images/bindings/index.html
Error: Process completed with exit code 1.

Co-authored-by: elithrar <elithrar@users.noreply.github.com>
@ask-bonk

ask-bonk Bot commented Feb 20, 2026

Copy link
Copy Markdown
Contributor Author

All checks pass. Here's a summary of the fix:

Root cause: In src/content/docs/images/transform-images/bindings.mdx:68,71, the code block used = ... (literal three dots) as a placeholder value:

const watermark: ReadableStream = ...
const image: ReadableStream = ...

The <TypeScriptExample> component strips TypeScript type annotations to produce a JavaScript tab, which turned these into const watermark = ... — invalid JavaScript because ... (spread) requires an operand. Prettier then failed to parse the generated JS, crashing the build.

Fix: Replaced the = ... placeholders with valid function call placeholders (getWatermarkStream() and getImageStream()) that remain valid JavaScript after type stripping.

github run

@elithrar

Copy link
Copy Markdown
Collaborator

/bonk kick the build / create an empty commit to fix.

@ask-bonk

ask-bonk Bot commented Feb 23, 2026

Copy link
Copy Markdown
Contributor Author

Empty commit created to retrigger the CI build.

github run

Comment thread src/content/docs/images/upload-images/upload-file-worker.mdx
@github-actions

Copy link
Copy Markdown
Contributor

Preview URL: https://b900785c.preview.developers.cloudflare.com
Preview Branch URL: https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com

Files with changes (up to 15)

Original Link Updated Link
https://developers.cloudflare.com/images/tutorials/optimize-user-uploaded-image/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/tutorials/optimize-user-uploaded-image/
https://developers.cloudflare.com/images/transform-images/draw-overlays/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/transform-images/draw-overlays/
https://developers.cloudflare.com/images/examples/watermark-from-kv/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/examples/watermark-from-kv/
https://developers.cloudflare.com/images/upload-images/upload-file-worker/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/upload-images/upload-file-worker/
https://developers.cloudflare.com/images/transform-images/transform-via-workers/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/transform-images/transform-via-workers/
https://developers.cloudflare.com/images/transform-images/bindings/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/transform-images/bindings/
https://developers.cloudflare.com/images/transform-images/control-origin-access/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/transform-images/control-origin-access/
https://developers.cloudflare.com/images/manage-images/serve-images/serve-private-images/ https://opencode-issue28270-20260217171319.preview.developers.cloudflare.com/images/manage-images/serve-images/serve-private-images/

@elithrar
elithrar merged commit 1e85765 into production Feb 23, 2026
9 checks passed
@elithrar
elithrar deleted the opencode/issue28270-20260217171319 branch February 23, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:images Related to Image Resizing product size/l

Projects

None yet

Development

Successfully merging this pull request may close these issues.

review images code examples

5 participants