Skip to content

Commit 8d7c6db

Browse files
author
Nicolas Dorseuil
committed
remove some useless log
1 parent 86aa013 commit 8d7c6db

File tree

5 files changed

+95
-158
lines changed

5 files changed

+95
-158
lines changed

packages/gitbook/openNext/incrementalCache.ts

Lines changed: 73 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { createHash } from 'node:crypto';
2-
3-
import { trace } from '@/lib/tracing';
42
import type {
53
CacheEntryType,
64
CacheValue,
@@ -34,41 +32,31 @@ class GitbookIncrementalCache implements IncrementalCache {
3432
cacheType?: CacheType
3533
): Promise<WithLastModified<CacheValue<CacheType>> | null> {
3634
const cacheKey = this.getR2Key(key, cacheType);
37-
return trace(
38-
{
39-
operation: 'openNextIncrementalCacheGet',
40-
name: cacheKey,
41-
},
42-
async (span) => {
43-
span.setAttribute('cacheType', cacheType ?? 'cache');
44-
const r2 = getCloudflareContext().env[BINDING_NAME];
45-
const localCache = await this.getCacheInstance();
46-
if (!r2) throw new Error('No R2 bucket');
47-
try {
48-
// Check local cache first if available
49-
const localCacheEntry = await localCache.match(this.getCacheUrlKey(cacheKey));
50-
if (localCacheEntry) {
51-
span.setAttribute('cacheHit', 'local');
52-
const result = (await localCacheEntry.json()) as WithLastModified<
53-
CacheValue<CacheType>
54-
>;
55-
return this.returnNullOn404(result);
56-
}
5735

58-
const r2Object = await r2.get(cacheKey);
59-
if (!r2Object) return null;
60-
61-
span.setAttribute('cacheHit', 'r2');
62-
return this.returnNullOn404({
63-
value: await r2Object.json(),
64-
lastModified: r2Object.uploaded.getTime(),
65-
});
66-
} catch (e) {
67-
console.error('Failed to get from cache', e);
68-
return null;
69-
}
36+
const r2 = getCloudflareContext().env[BINDING_NAME];
37+
const localCache = await this.getCacheInstance();
38+
if (!r2) throw new Error('No R2 bucket');
39+
try {
40+
// Check local cache first if available
41+
const localCacheEntry = await localCache.match(this.getCacheUrlKey(cacheKey));
42+
if (localCacheEntry) {
43+
const result = (await localCacheEntry.json()) as WithLastModified<
44+
CacheValue<CacheType>
45+
>;
46+
return this.returnNullOn404(result);
7047
}
71-
);
48+
49+
const r2Object = await r2.get(cacheKey);
50+
if (!r2Object) return null;
51+
52+
return this.returnNullOn404({
53+
value: await r2Object.json(),
54+
lastModified: r2Object.uploaded.getTime(),
55+
});
56+
} catch (e) {
57+
console.error('Failed to get from cache', e);
58+
return null;
59+
}
7260
}
7361

7462
//TODO: This is a workaround to handle 404 responses in the cache.
@@ -89,75 +77,60 @@ class GitbookIncrementalCache implements IncrementalCache {
8977
cacheType?: CacheType
9078
): Promise<void> {
9179
const cacheKey = this.getR2Key(key, cacheType);
92-
return trace(
93-
{
94-
operation: 'openNextIncrementalCacheSet',
95-
name: cacheKey,
96-
},
97-
async (span) => {
98-
span.setAttribute('cacheType', cacheType ?? 'cache');
99-
const localCache = await this.getCacheInstance();
100-
101-
try {
102-
await this.writeToR2(cacheKey, JSON.stringify(value));
103-
104-
//TODO: Check if there is any places where we don't have tags
105-
// Ideally we should always have tags, but in case we don't, we need to decide how to handle it
106-
// For now we default to a build ID tag, which allow us to invalidate the cache in case something is wrong in this deployment
107-
const tags = this.getTagsFromCacheEntry(value) ?? [
108-
`build_id/${process.env.NEXT_BUILD_ID}`,
109-
];
110-
111-
// We consider R2 as the source of truth, so we update the local cache
112-
// only after a successful R2 write
113-
await localCache.put(
114-
this.getCacheUrlKey(cacheKey),
115-
new Response(
116-
JSON.stringify({
117-
value,
118-
// Note: `Date.now()` returns the time of the last IO rather than the actual time.
119-
// See https://developers.cloudflare.com/workers/reference/security-model/
120-
lastModified: Date.now(),
121-
}),
122-
{
123-
headers: {
124-
// Cache-Control default to 30 minutes, will be overridden by `revalidate`
125-
// In theory we should always get the `revalidate` value
126-
'cache-control': `max-age=${value.revalidate ?? 60 * 30}`,
127-
'cache-tag': tags.join(','),
128-
},
129-
}
130-
)
131-
);
132-
} catch (e) {
133-
console.error('Failed to set to cache', e);
134-
}
135-
}
136-
);
80+
81+
const localCache = await this.getCacheInstance();
82+
83+
try {
84+
await this.writeToR2(cacheKey, JSON.stringify(value));
85+
86+
//TODO: Check if there is any places where we don't have tags
87+
// Ideally we should always have tags, but in case we don't, we need to decide how to handle it
88+
// For now we default to a build ID tag, which allow us to invalidate the cache in case something is wrong in this deployment
89+
const tags = this.getTagsFromCacheEntry(value) ?? [
90+
`build_id/${process.env.NEXT_BUILD_ID}`,
91+
];
92+
93+
// We consider R2 as the source of truth, so we update the local cache
94+
// only after a successful R2 write
95+
await localCache.put(
96+
this.getCacheUrlKey(cacheKey),
97+
new Response(
98+
JSON.stringify({
99+
value,
100+
// Note: `Date.now()` returns the time of the last IO rather than the actual time.
101+
// See https://developers.cloudflare.com/workers/reference/security-model/
102+
lastModified: Date.now(),
103+
}),
104+
{
105+
headers: {
106+
// Cache-Control default to 30 minutes, will be overridden by `revalidate`
107+
// In theory we should always get the `revalidate` value
108+
'cache-control': `max-age=${value.revalidate ?? 60 * 30}`,
109+
'cache-tag': tags.join(','),
110+
},
111+
}
112+
)
113+
);
114+
} catch (e) {
115+
console.error('Failed to set to cache', e);
116+
}
137117
}
138118

139119
async delete(key: string): Promise<void> {
140120
const cacheKey = this.getR2Key(key);
141-
return trace(
142-
{
143-
operation: 'openNextIncrementalCacheDelete',
144-
name: cacheKey,
145-
},
146-
async () => {
147-
const r2 = getCloudflareContext().env[BINDING_NAME];
148-
const localCache = await this.getCacheInstance();
149-
if (!r2) throw new Error('No R2 bucket');
150-
151-
try {
152-
await r2.delete(cacheKey);
153-
154-
// Here again R2 is the source of truth, so we delete from local cache first
155-
await localCache.delete(this.getCacheUrlKey(cacheKey));
156-
} catch (e) {
157-
console.error('Failed to delete from cache', e);
158-
}
159-
}
160-
);
121+
122+
const r2 = getCloudflareContext().env[BINDING_NAME];
123+
const localCache = await this.getCacheInstance();
124+
if (!r2) throw new Error('No R2 bucket');
125+
126+
try {
127+
await r2.delete(cacheKey);
128+
129+
// Here again R2 is the source of truth, so we delete from local cache first
130+
await localCache.delete(this.getCacheUrlKey(cacheKey));
131+
} catch (e) {
132+
console.error('Failed to delete from cache', e);
133+
}
161134
}
162135

163136
async writeToR2(key: string, value: string): Promise<void> {
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { trace } from '@/lib/tracing';
21
import type { Queue } from '@opennextjs/aws/types/overrides.js';
32
import { getCloudflareContext } from '@opennextjs/cloudflare';
43
import doQueue from '@opennextjs/cloudflare/overrides/queue/do-queue';
54

65
export default {
76
name: 'GitbookISRQueue',
87
send: async (msg) => {
9-
return trace({ operation: 'gitbookISRQueueSend', name: msg.MessageBody.url }, async () => {
10-
const { ctx } = getCloudflareContext();
11-
ctx.waitUntil(doQueue.send(msg));
12-
});
8+
const { ctx } = getCloudflareContext();
9+
ctx.waitUntil(doQueue.send(msg));
1310
},
1411
} satisfies Queue;

packages/gitbook/openNext/queue/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { getLogger } from '@/lib/logger';
12
import type { Queue } from '@opennextjs/aws/types/overrides.js';
2-
import { getLogger } from '@v2/lib/logger';
33

44
export default {
55
name: 'GitbookISRQueue',
Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { trace } from '@/lib/tracing';
1+
import { getLogger } from '@/lib/logger';
22
import type { NextModeTagCache } from '@opennextjs/aws/types/overrides.js';
33
import doShardedTagCache from '@opennextjs/cloudflare/overrides/tag-cache/do-sharded-tag-cache';
44
import { softTagFilter } from '@opennextjs/cloudflare/overrides/tag-cache/tag-cache-filter';
5-
import { getLogger } from '@v2/lib/logger';
65

76
const originalTagCache = doShardedTagCache({
87
baseShardSize: 12,
@@ -24,62 +23,27 @@ export default {
2423
getLastRevalidated: async (tags: string[]) => {
2524
const tagsToCheck = tags.filter(softTagFilter);
2625
if (tagsToCheck.length === 0) {
27-
const logger = getLogger().subLogger('gitbookTagCache');
28-
// If we reach here, it probably means that there is an issue that we'll need to address.
29-
logger.warn(
30-
'getLastRevalidated - No valid tags to check for last revalidation, original tags:',
31-
tags
32-
);
3326
return 0; // If no tags to check, return 0
3427
}
35-
return trace(
36-
{
37-
operation: 'gitbookTagCacheGetLastRevalidated',
38-
name: tagsToCheck.join(', '),
39-
},
40-
async () => {
41-
return await originalTagCache.getLastRevalidated(tagsToCheck);
42-
}
43-
);
28+
29+
return await originalTagCache.getLastRevalidated(tagsToCheck);
4430
},
4531
hasBeenRevalidated: async (tags: string[], lastModified?: number) => {
4632
const tagsToCheck = tags.filter(softTagFilter);
4733
if (tagsToCheck.length === 0) {
48-
const logger = getLogger().subLogger('gitbookTagCache');
49-
// If we reach here, it probably means that there is an issue that we'll need to address.
50-
logger.warn(
51-
'hasBeenRevalidated - No valid tags to check for revalidation, original tags:',
52-
tags
53-
);
5434
return false; // If no tags to check, return false
5535
}
56-
return trace(
57-
{
58-
operation: 'gitbookTagCacheHasBeenRevalidated',
59-
name: tagsToCheck.join(', '),
60-
},
61-
async () => {
62-
const result = await originalTagCache.hasBeenRevalidated(tagsToCheck, lastModified);
63-
return result;
64-
}
65-
);
36+
37+
return await originalTagCache.hasBeenRevalidated(tagsToCheck, lastModified);
6638
},
6739
writeTags: async (tags: string[]) => {
68-
return trace(
69-
{
70-
operation: 'gitbookTagCacheWriteTags',
71-
name: tags.join(', '),
72-
},
73-
async () => {
74-
const tagsToWrite = tags.filter(softTagFilter);
75-
if (tagsToWrite.length === 0) {
76-
const logger = getLogger().subLogger('gitbookTagCache');
77-
logger.warn('writeTags - No valid tags to write');
78-
return; // If no tags to write, exit early
79-
}
80-
// Write only the filtered tags
81-
await originalTagCache.writeTags(tagsToWrite);
82-
}
83-
);
40+
const tagsToWrite = tags.filter(softTagFilter);
41+
if (tagsToWrite.length === 0) {
42+
const logger = getLogger().subLogger('gitbookTagCache');
43+
logger.warn('writeTags - No valid tags to write');
44+
return; // If no tags to write, exit early
45+
}
46+
// Write only the filtered tags
47+
await originalTagCache.writeTags(tagsToWrite);
8448
},
8549
} satisfies NextModeTagCache;

packages/gitbook/src/lib/images/signatures.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ export async function verifyImageSignature(
3535
const generated = await generator(input);
3636

3737
const logger = getLogger().subLogger('image-signature');
38-
logger.log(
39-
`comparing image signature for "${input.url}" on identifier "${input.imagesContextId}": "${generated}" (expected) === "${signature}" (actual)`
40-
);
38+
if (generated !== signature) {
39+
// We only log if the signature does not match, to avoid logging useless information
40+
logger.log(
41+
`comparing image signature for "${input.url}" on identifier "${input.imagesContextId}": "${generated}" (expected) === "${signature}" (actual)`
42+
);
43+
}
4144
return generated === signature;
4245
}
4346

0 commit comments

Comments
 (0)