Skip to content

Commit c8a4994

Browse files
author
Nicolas Dorseuil
committed
test without the binding
1 parent 042011a commit c8a4994

File tree

4 files changed

+48
-90
lines changed

4 files changed

+48
-90
lines changed

packages/gitbook-v2/openNext/customWorkers/middleware.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@ export default class extends WorkerEntrypoint {
4040
});
4141
}
4242

43-
/**
44-
* Forwards the message from the server to the DO queue.
45-
*/
46-
async send(message) {
47-
return runWithCloudflareRequestContext(
48-
new Request('http://local'),
49-
this.env,
50-
this.ctx,
51-
async () => {
52-
const queue = await onConfig.middleware.override.queue();
53-
if (queue) {
54-
return queue.send(message);
55-
}
56-
}
57-
);
58-
}
59-
6043
/**
6144
* All the functions below are used to interact with the tag cache.
6245
* They are needed for the server who wouldn't be able to access the tag cache directly.
@@ -76,20 +59,6 @@ export default class extends WorkerEntrypoint {
7659
);
7760
}
7861

79-
async hasBeenRevalidated() {
80-
return runWithCloudflareRequestContext(
81-
new Request('http://local'),
82-
this.env,
83-
this.ctx,
84-
async () => {
85-
const tagCache = await onConfig.middleware.override.tagCache();
86-
if (tagCache) {
87-
return tagCache.hasBeenRevalidated();
88-
}
89-
}
90-
);
91-
}
92-
9362
async writeTags(tags) {
9463
return runWithCloudflareRequestContext(
9564
new Request('http://local'),
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import type { Queue } from '@opennextjs/aws/types/overrides.js';
2-
// import { getCloudflareContext } from '@opennextjs/cloudflare';
3-
4-
// biome-ignore lint/correctness/noUnusedVariables: <explanation>
5-
interface Env {
6-
MIDDLEWARE_REFERENCE?: Pick<Queue, 'send'>;
7-
}
82

93
export default {
104
name: 'GitbookISRQueue',
11-
// biome-ignore lint/correctness/noUnusedVariables: <explanation>
125
send: async (msg) => {
13-
//TODO: Re add this
14-
// const { env, ctx } = getCloudflareContext();
15-
// ctx.waitUntil((env as Env).MIDDLEWARE_REFERENCE?.send(msg) ?? Promise.resolve());
6+
// We should never reach this point in the server. If that's the case, we should log it.
7+
console.warn('GitbookISRQueue: send called on server side, this should not happen.', msg);
168
},
179
} satisfies Queue;
Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
import type { NextModeTagCache } from '@opennextjs/aws/types/overrides.js';
2+
import { getCloudflareContext } from '@opennextjs/cloudflare';
23

3-
// interface Env {
4-
// MIDDLEWARE_REFERENCE?: Pick<
5-
// NextModeTagCache,
6-
// 'getLastRevalidated' | 'hasBeenRevalidated' | 'writeTags' | 'getPathsByTags'
7-
// >;
8-
// }
4+
interface Env {
5+
MIDDLEWARE_REFERENCE?: Pick<NextModeTagCache, 'writeTags' | 'getLastRevalidated'>;
6+
}
97

10-
//TODO: Reenable all of this.
118
export default {
129
name: 'GitbookTagCache',
1310
mode: 'nextMode',
14-
getLastRevalidated: async (_tags: string[]) => {
15-
return 0;
16-
// try {
17-
// const { env } = getCloudflareContext();
18-
// return (env as Env).MIDDLEWARE_REFERENCE?.getLastRevalidated(tags) ?? 0;
19-
// } catch (error) {
20-
// console.error('Server - Error getting last revalidated tags:', error);
21-
// return 0; // Fallback to 0 if there's an error
22-
// }
11+
getLastRevalidated: async (tags: string[]) => {
12+
try {
13+
const { env } = getCloudflareContext();
14+
const lastRevalidated = await (env as Env).MIDDLEWARE_REFERENCE?.getLastRevalidated(
15+
tags
16+
);
17+
return lastRevalidated ?? 0; // Fallback to 0 if there's no last revalidated time
18+
} catch (error) {
19+
console.error('GitbookTagCache: Error getting last revalidated:', error);
20+
return 0; // Fallback to 0 if there's an error
21+
}
2322
},
24-
hasBeenRevalidated: async (_tags: string[]) => {
25-
// try {
26-
// const { env } = getCloudflareContext();
27-
// return (env as Env).MIDDLEWARE_REFERENCE?.hasBeenRevalidated(tags) ?? false;
28-
// } catch (error) {
29-
// console.error('Server - Error checking if tags have been revalidated:', error);
30-
// return false; // Fallback to false if there's an error
31-
// }
23+
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+
);
3229
return false;
3330
},
34-
writeTags: async (_tags: string[]) => {
35-
// try {
36-
// const { env } = getCloudflareContext();
37-
// (env as Env).MIDDLEWARE_REFERENCE?.writeTags(tags);
38-
// } catch (error) {
39-
// console.error('Server - Error writing tags:', error);
40-
// }
41-
return;
31+
writeTags: async (tags: string[]) => {
32+
try {
33+
const { env } = getCloudflareContext();
34+
(env as Env).MIDDLEWARE_REFERENCE?.writeTags(tags);
35+
} catch (error) {
36+
console.error('Server - Error writing tags:', error);
37+
}
4238
},
4339
} satisfies NextModeTagCache;

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { getCacheTag, getComputedContentSourceCacheTags } from '@gitbook/cache-tags';
1010
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
1111
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
12-
import { getCloudflareContext, getCloudflareRequestGlobal } from './cloudflare';
12+
import { getCloudflareRequestGlobal } from './cloudflare';
1313
import { DataFetcherError, wrapDataFetcherError } from './errors';
1414
import { withCacheKey, withoutConcurrentExecution } from './memoize';
1515
import type { GitBookDataFetcher } from './types';
@@ -828,7 +828,7 @@ async function* streamAIResponse(
828828
}
829829
}
830830

831-
let loggedServiceBinding = false;
831+
// const loggedServiceBinding = false;
832832

833833
/**
834834
* Create a new API client.
@@ -837,21 +837,22 @@ export function apiClient(input: DataFetcherInput = { apiToken: null }) {
837837
const { apiToken } = input;
838838
let serviceBinding: GitBookAPIServiceBinding | undefined;
839839

840-
const cloudflareContext = getCloudflareContext();
841-
if (cloudflareContext) {
842-
// @ts-expect-error
843-
serviceBinding = cloudflareContext.env.GITBOOK_API as GitBookAPIServiceBinding | undefined;
844-
if (!loggedServiceBinding) {
845-
loggedServiceBinding = true;
846-
if (serviceBinding) {
847-
// biome-ignore lint/suspicious/noConsole: we want to log here
848-
console.log(`using service binding for the API (${GITBOOK_API_URL})`);
849-
} else {
850-
// biome-ignore lint/suspicious/noConsole: we want to log here
851-
console.warn(`no service binding for the API (${GITBOOK_API_URL})`);
852-
}
853-
}
854-
}
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+
// }
855856

856857
const api = new GitBookAPI({
857858
authToken: apiToken || GITBOOK_API_TOKEN || undefined,

0 commit comments

Comments
 (0)