Skip to content

Commit 7d3de29

Browse files
committed
Enable Cloudflare image transformations
1 parent 8f5fd21 commit 7d3de29

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

docs/cloudflare-workers-migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ The Worker build was checked for `generated/create-manifest.js` and `create-mani
9999

100100
## Image Transformation Note
101101

102-
Cloudflare image transformations are disabled by default because `/cdn-cgi/image/*` returned 404 on the Worker preview URL before custom-domain image resizing was proven. Set `TANSTACK_IMAGE_TRANSFORMATIONS=true` during build only after Image Resizing works on the routed `tanstack.com` zone.
102+
Cloudflare image transformations are enabled for `build:cloudflare`. Transformed image URLs are emitted through the configured `SITE_URL` origin because `/cdn-cgi/image/*` works on the routed `tanstack.com` zone but still returns 404 on the `workers.dev` hostname.
103103

104104
## Markdown Audit Note
105105

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dev:frontend": "pnpm run with-env vite dev",
1212
"dev:cloudflare": "pnpm run with-env -- sh -c 'DISABLE_REDACT=true vite dev'",
1313
"build": "vite build --logLevel warn",
14-
"build:cloudflare": "pnpm run build",
14+
"build:cloudflare": "TANSTACK_IMAGE_TRANSFORMATIONS=true pnpm run build",
1515
"preview": "vite preview",
1616
"preview:cloudflare": "pnpm run preview",
1717
"deploy:cloudflare": "SITE_URL=https://tanstack.com VITE_SITE_URL=https://tanstack.com pnpm run build:cloudflare && wrangler deploy --name tanstack-com --var SITE_URL:https://tanstack.com",

src/utils/optimizedImage.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ export function getOptimizedImageUrl(
1919
return src
2020
}
2121

22+
const transformOrigin = getTransformOrigin()
2223
const source = encodeURI(src).replace(/^\//, '')
2324

24-
return `/cdn-cgi/image/${transformOptions}/${source}`
25+
return `${transformOrigin}/cdn-cgi/image/${transformOptions}/${source}`
2526
}
2627

2728
export function getAbsoluteOptimizedImageUrl(
@@ -54,6 +55,10 @@ function shouldTransformImage(src: string) {
5455
)
5556
}
5657

58+
function getTransformOrigin() {
59+
return __TANSTACK_SITE_URL__.replace(/\/$/, '')
60+
}
61+
5762
function createCloudflareTransformOptions(
5863
options: ImageOptimizationOptions,
5964
): string | undefined {

0 commit comments

Comments
 (0)