Skip to content

Commit eeb39a4

Browse files
committed
Log and use same function for service binding
1 parent 1ef31f8 commit eeb39a4

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/gitbook-v2/src/lib/data/cloudflare.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getCloudflareContext as getCloudflareContextOpenNext } from '@opennextjs/cloudflare';
2+
13
/**
24
* Load the cloudflare context.
35
* This is a workaround to avoid webpack trying to bundle this cloudflare only module.
@@ -19,17 +21,26 @@ export async function getCloudflareContext(): Promise<{
1921
ctx: Record<string, unknown>;
2022
} | null> {
2123
try {
22-
// HACK: This is a workaround to avoid webpack trying to bundle this cloudflare only module
2324
// @ts-ignore
24-
const cloudflareContext = await import(
25-
/* webpackIgnore: true */ `${'__cloudflare:workers'.replaceAll('_', '')}`
26-
);
27-
return cloudflareContext;
25+
return await getCloudflareContextOpenNext();
2826
} catch (error) {
2927
if (process.env.NODE_ENV === 'production' && !process.env.VERCEL) {
3028
throw error;
3129
}
3230
}
3331

3432
return null;
33+
// try {
34+
// // HACK: This is a workaround to avoid webpack trying to bundle this cloudflare only module
35+
// // @ts-ignore
36+
// const cloudflareContext = await import(
37+
// /* webpackIgnore: true */ `${'__cloudflare:workers'.replaceAll('_', '')}`
38+
// );
39+
// return cloudflareContext;
40+
// } catch (error) {
41+
// if (process.env.NODE_ENV === 'production' && !process.env.VERCEL) {
42+
// throw error;
43+
// }
44+
// }
45+
// return null;
3546
}

packages/gitbook-v2/src/lib/data/memoize.ts

+5
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ async function getRequestCacheWeakMap(): Promise<WeakMap<any, CacheStorage<strin
7070
// `cf` changes for each request, we can use it as an identifier of the request to isolate the cache per request
7171
const requestCache = perRequestCache.get(cloudflareContext.cf);
7272
if (requestCache) {
73+
console.log('Reusing per-request cache', cloudflareContext.cf);
7374
return requestCache;
7475
}
7576

77+
console.log('Allocating per-request cache', cloudflareContext.cf);
78+
7679
const newRequestCache = new WeakMap<any, CacheStorage<string, unknown>>();
7780
perRequestCache.set(cloudflareContext.cf, newRequestCache);
7881
return newRequestCache;
@@ -81,6 +84,8 @@ async function getRequestCacheWeakMap(): Promise<WeakMap<any, CacheStorage<strin
8184
if (process.env.NODE_ENV === 'production' && !process.env.VERCEL) {
8285
throw error;
8386
}
87+
88+
console.warn('Failed to get cloudflare context, using global cache', error);
8489
}
8590

8691
return globalCache;

0 commit comments

Comments
 (0)