Skip to content

Add temporary logging for Cloudflare debugging #3249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/gitbook-v2/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@
"vars": {
// This is a bit misleading, but it means that we can have 500 concurrent revalidations
// This means that we'll have up to 100 durable objects instance running at the same time
"MAX_REVALIDATE_CONCURRENCY": "100"
"MAX_REVALIDATE_CONCURRENCY": "100",
// Temporary variable to find the issue once deployed
// TODO: remove this once the issue is fixed
"DEBUG_CLOUDFLARE": "true"
},
"routes": [
{
Expand Down
15 changes: 15 additions & 0 deletions packages/gitbook/src/routes/ogimage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,25 @@ async function loadCustomFont(input: { url: string; weight: 400 | 700 }) {
};
}

/**
* Temporary function to log some data on Cloudflare.
* TODO: remove this when we found the issue
*/
function logOnCloudflareOnly(message: string) {
if (process.env.DEBUG_CLOUDFLARE === 'true') {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(message);
}
}

/**
* Fetch a resource from the function itself.
* To avoid error with worker to worker requests in the same zone, we use the `WORKER_SELF_REFERENCE` binding.
*/
async function fetchSelf(url: string) {
const cloudflare = getCloudflareContext();
if (cloudflare?.env.WORKER_SELF_REFERENCE) {
logOnCloudflareOnly(`Fetching self: ${url}`);
return await cloudflare.env.WORKER_SELF_REFERENCE.fetch(
// `getAssetURL` can return a relative URL, so we need to make it absolute
// the URL doesn't matter, as we're using the worker-self-reference binding
Expand All @@ -334,6 +346,9 @@ async function fetchSelf(url: string) {
async function readImage(response: Response) {
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.startsWith('image/')) {
logOnCloudflareOnly(`Invalid content type: ${contentType},
status: ${response.status}
rayId: ${response.headers.get('cf-ray')}`);
throw new Error(`Invalid content type: ${contentType}`);
}

Expand Down