Skip to content

Commit 4c37530

Browse files
authored
Cloudflare: fix image resizing (#3143)
1 parent 89f8e16 commit 4c37530

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

packages/gitbook-v2/src/lib/images/resizer/cdn-cgi.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GITBOOK_IMAGE_RESIZE_URL } from '@v2/lib/env';
22
import type { CloudflareImageOptions } from './types';
3+
import { copyImageResponse } from './utils';
34

45
/**
56
* Resize an image by doing a request to a /cdn/cgi/ endpoint.
@@ -26,16 +27,18 @@ export async function resizeImageWithCDNCgi(
2627
// biome-ignore lint/suspicious/noConsole: this log is useful for debugging
2728
console.log(`resize image using cdn-cgi: ${resizeURL}`);
2829

29-
return await fetch(resizeURL, {
30-
headers: {
31-
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
32-
Accept:
33-
resizeOptions.format === 'json'
34-
? 'application/json'
35-
: `image/${resizeOptions.format || 'jpeg'}`,
36-
},
37-
signal,
38-
});
30+
return copyImageResponse(
31+
await fetch(resizeURL, {
32+
headers: {
33+
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
34+
Accept:
35+
resizeOptions.format === 'json'
36+
? 'application/json'
37+
: `image/${resizeOptions.format || 'jpeg'}`,
38+
},
39+
signal,
40+
})
41+
);
3942
}
4043

4144
function stringifyOptions(options: CloudflareImageOptions): string {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CloudflareImageOptions } from './types';
2+
import { copyImageResponse } from './utils';
23

34
/**
45
* Resize an image by doing a request to the image itself using the Cloudflare fetch.
@@ -17,15 +18,17 @@ export async function resizeImageWithCFFetch(
1718
// biome-ignore lint/suspicious/noConsole: this log is useful for debugging
1819
console.log(`resize image using cf-fetch: ${input}`);
1920

20-
return await fetch(input, {
21-
headers: {
22-
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
23-
Accept:
24-
resizeOptions.format === 'json'
25-
? 'application/json'
26-
: `image/${resizeOptions.format || 'jpeg'}`,
27-
},
28-
signal,
29-
cf: { image: resizeOptions },
30-
});
21+
return copyImageResponse(
22+
await fetch(input, {
23+
headers: {
24+
// Pass the `Accept` header, as Cloudflare uses this to validate the format.
25+
Accept:
26+
resizeOptions.format === 'json'
27+
? 'application/json'
28+
: `image/${resizeOptions.format || 'jpeg'}`,
29+
},
30+
signal,
31+
cf: { image: resizeOptions },
32+
})
33+
);
3134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copy a response to make sure it can be mutated by the rest of the middleware.
3+
* To avoid errors "Can't modify immutable headers".
4+
*/
5+
export function copyImageResponse(response: Response) {
6+
return new Response(response.body, response);
7+
}

0 commit comments

Comments
 (0)