Skip to content

Commit 2c9431e

Browse files
author
Nicolas Dorseuil
committed
remove GITBOOK_API binding
1 parent c8a4994 commit 2c9431e

File tree

4 files changed

+19
-61
lines changed

4 files changed

+19
-61
lines changed

packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
"binding": "WORKER_SELF_REFERENCE",
3030
"service": "gitbook-open-v2-server-dev"
3131
},
32-
{
33-
"binding": "GITBOOK_API",
34-
"service": "gitbook-x-prod-api-cache"
35-
},
3632
{
3733
"binding": "MIDDLEWARE_REFERENCE",
3834
"service": "gitbook-open-v2-dev"
@@ -54,10 +50,6 @@
5450
"binding": "WORKER_SELF_REFERENCE",
5551
"service": "gitbook-open-v2-server-preview"
5652
},
57-
{
58-
"binding": "GITBOOK_API",
59-
"service": "gitbook-x-prod-api-cache"
60-
},
6153
{
6254
"binding": "MIDDLEWARE_REFERENCE",
6355
"service": "gitbook-open-v2-preview"
@@ -78,10 +70,6 @@
7870
"binding": "WORKER_SELF_REFERENCE",
7971
"service": "gitbook-open-v2-server-staging"
8072
},
81-
{
82-
"binding": "GITBOOK_API",
83-
"service": "gitbook-x-staging-api-cache"
84-
},
8573
{
8674
"binding": "MIDDLEWARE_REFERENCE",
8775
"service": "gitbook-open-v2-staging"
@@ -105,10 +93,6 @@
10593
"binding": "WORKER_SELF_REFERENCE",
10694
"service": "gitbook-open-v2-server-production"
10795
},
108-
{
109-
"binding": "GITBOOK_API",
110-
"service": "gitbook-x-prod-api-cache"
111-
},
11296
{
11397
"binding": "MIDDLEWARE_REFERENCE",
11498
"service": "gitbook-open-v2-production"

packages/gitbook-v2/openNext/customWorkers/middlewareWrangler.jsonc

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
"binding": "WORKER_SELF_REFERENCE",
3434
"service": "gitbook-open-v2-dev"
3535
},
36-
{
37-
"binding": "GITBOOK_API",
38-
"service": "gitbook-x-prod-api-cache"
39-
},
4036
{
4137
"binding": "DEFAULT_WORKER",
4238
"service": "gitbook-open-v2-server-dev"
@@ -60,10 +56,6 @@
6056
"binding": "WORKER_SELF_REFERENCE",
6157
"service": "gitbook-open-v2-preview"
6258
},
63-
{
64-
"binding": "GITBOOK_API",
65-
"service": "gitbook-x-prod-api-cache"
66-
},
6759
{
6860
"binding": "DEFAULT_WORKER",
6961
"service": "gitbook-open-v2-server-preview"
@@ -98,10 +90,6 @@
9890
"binding": "WORKER_SELF_REFERENCE",
9991
"service": "gitbook-open-v2-staging"
10092
},
101-
{
102-
"binding": "GITBOOK_API",
103-
"service": "gitbook-x-staging-api-cache"
104-
},
10593
{
10694
"binding": "DEFAULT_WORKER",
10795
"service": "gitbook-open-v2-server-staging"
@@ -163,10 +151,6 @@
163151
"binding": "WORKER_SELF_REFERENCE",
164152
"service": "gitbook-open-v2-production"
165153
},
166-
{
167-
"binding": "GITBOOK_API",
168-
"service": "gitbook-x-prod-api-cache"
169-
},
170154
{
171155
"binding": "DEFAULT_WORKER",
172156
"service": "gitbook-open-v2-server-production"

packages/gitbook-v2/openNext/tagCache/server.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import type { NextModeTagCache } from '@opennextjs/aws/types/overrides.js';
22
import { getCloudflareContext } from '@opennextjs/cloudflare';
33

44
interface Env {
5-
MIDDLEWARE_REFERENCE?: Pick<NextModeTagCache, 'writeTags' | 'getLastRevalidated'>;
5+
MIDDLEWARE_REFERENCE?: Pick<
6+
NextModeTagCache,
7+
'writeTags' | 'getLastRevalidated' | 'hasBeenRevalidated'
8+
>;
69
}
710

811
export default {
912
name: 'GitbookTagCache',
1013
mode: 'nextMode',
14+
// This one is used for the composable cache.
1115
getLastRevalidated: async (tags: string[]) => {
1216
try {
1317
const { env } = getCloudflareContext();
@@ -20,13 +24,19 @@ export default {
2024
return 0; // Fallback to 0 if there's an error
2125
}
2226
},
27+
// This one get called for ISR/SSG cache, if we reach here, it very likely means that we need to revalidate the page.
28+
// TODO: see if we could just return true.
2329
hasBeenRevalidated: async (tags: string[]) => {
24-
// We should never reach this point in the server. If that's the case, we should log it.
25-
console.warn(
26-
'GitbookTagCache: hasBeenRevalidated called on server side, this should not happen.',
27-
tags
28-
);
29-
return false;
30+
try {
31+
const { env } = getCloudflareContext();
32+
const hasBeenRevalidated = await (env as Env).MIDDLEWARE_REFERENCE?.hasBeenRevalidated(
33+
tags
34+
);
35+
return hasBeenRevalidated ?? false; // Fallback to false if there's no revalidation status
36+
} catch (error) {
37+
console.error('GitbookTagCache: Error checking revalidation:', error);
38+
return false; // Fallback to false if there's an error
39+
}
3040
},
3141
writeTags: async (tags: string[]) => {
3242
try {

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { trace } from '@/lib/tracing';
22
import {
33
type ComputedContentSource,
44
GitBookAPI,
5-
type GitBookAPIServiceBinding,
65
type HttpResponse,
76
type RenderIntegrationUI,
87
} from '@gitbook/api';
@@ -828,37 +827,18 @@ async function* streamAIResponse(
828827
}
829828
}
830829

831-
// const loggedServiceBinding = false;
832-
833830
/**
834831
* Create a new API client.
832+
* We don't use the binding because it can cause Error: Response closed due to connection limit
833+
* Connection limit are shared between all the bindings.
835834
*/
836835
export function apiClient(input: DataFetcherInput = { apiToken: null }) {
837836
const { apiToken } = input;
838-
let serviceBinding: GitBookAPIServiceBinding | undefined;
839-
840-
// TODO: Don't forget to uncomment if it wasn't the issue.
841-
// const cloudflareContext = getCloudflareContext();
842-
// if (cloudflareContext) {
843-
// // @ts-expect-error
844-
// serviceBinding = cloudflareContext.env.GITBOOK_API as GitBookAPIServiceBinding | undefined;
845-
// if (!loggedServiceBinding) {
846-
// loggedServiceBinding = true;
847-
// if (serviceBinding) {
848-
// // biome-ignore lint/suspicious/noConsole: we want to log here
849-
// console.log(`using service binding for the API (${GITBOOK_API_URL})`);
850-
// } else {
851-
// // biome-ignore lint/suspicious/noConsole: we want to log here
852-
// console.warn(`no service binding for the API (${GITBOOK_API_URL})`);
853-
// }
854-
// }
855-
// }
856837

857838
const api = new GitBookAPI({
858839
authToken: apiToken || GITBOOK_API_TOKEN || undefined,
859840
endpoint: GITBOOK_API_URL,
860841
userAgent: GITBOOK_USER_AGENT,
861-
serviceBinding,
862842
});
863843

864844
return api;

0 commit comments

Comments
 (0)