Skip to content

Commit 3c0e49d

Browse files
authored
Fix cache race condition (#3738)
1 parent c35d0e8 commit 3c0e49d

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

packages/gitbook/open-next.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export default {
77
converter: 'edge',
88
proxyExternalRequest: 'fetch',
99
queue: () => import('./openNext/queue/middleware').then((m) => m.default),
10-
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default),
10+
incrementalCache: () =>
11+
import('./openNext/incrementalCache/server').then((m) => m.default),
1112
tagCache: () => import('./openNext/tagCache/middleware').then((m) => m.default),
1213
},
1314
},
@@ -18,7 +19,8 @@ export default {
1819
converter: 'edge',
1920
proxyExternalRequest: 'fetch',
2021
queue: () => import('./openNext/queue/middleware').then((m) => m.default),
21-
incrementalCache: () => import('./openNext/incrementalCache').then((m) => m.default),
22+
incrementalCache: () =>
23+
import('./openNext/incrementalCache/middleware').then((m) => m.default),
2224
tagCache: () => import('./openNext/tagCache/middleware').then((m) => m.default),
2325
},
2426
},

packages/gitbook/openNext/incrementalCache.ts renamed to packages/gitbook/openNext/incrementalCache/incrementalCache.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import type {
77
} from '@opennextjs/aws/types/overrides.js';
88
import { getCloudflareContext } from '@opennextjs/cloudflare';
99

10-
import { withRegionalCache } from '@opennextjs/cloudflare/overrides/incremental-cache/regional-cache';
11-
1210
import type { DurableObjectNamespace, Rpc } from '@cloudflare/workers-types';
1311

1412
export const BINDING_NAME = 'NEXT_INC_CACHE_R2_BUCKET';
@@ -23,7 +21,7 @@ export type KeyOptions = {
2321
* It is very similar to the `R2IncrementalCache` in the `@opennextjs/cloudflare` package, but it has an additional
2422
* R2WriteBuffer Durable Object to handle writes to R2. Given how we set up cache, we often end up writing to the same key too fast.
2523
*/
26-
class GitbookIncrementalCache implements IncrementalCache {
24+
export class GitbookIncrementalCache implements IncrementalCache {
2725
name = 'GitbookIncrementalCache';
2826

2927
async get<CacheType extends CacheEntryType = 'cache'>(
@@ -137,12 +135,3 @@ class GitbookIncrementalCache implements IncrementalCache {
137135
);
138136
}
139137
}
140-
141-
export default withRegionalCache(new GitbookIncrementalCache(), {
142-
mode: 'long-lived',
143-
// We can do it because we use our own logic to invalidate the cache
144-
bypassTagCacheOnCacheHit: true,
145-
defaultLongLivedTtlSec: 60 * 60 * 24 /* 24 hours */,
146-
// We don't want to update the cache entry on every cache hit
147-
shouldLazilyUpdateOnCacheHit: false,
148-
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { withRegionalCache } from '@opennextjs/cloudflare/overrides/incremental-cache/regional-cache';
2+
import { GitbookIncrementalCache } from './incrementalCache';
3+
4+
export default withRegionalCache(new GitbookIncrementalCache(), {
5+
mode: 'long-lived',
6+
// We can do it because we use our own logic to invalidate the cache
7+
bypassTagCacheOnCacheHit: true,
8+
defaultLongLivedTtlSec: 60 * 60 * 24 /* 24 hours */,
9+
// We don't want to update the cache entry on every cache hit
10+
shouldLazilyUpdateOnCacheHit: false,
11+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { withRegionalCache } from '@opennextjs/cloudflare/overrides/incremental-cache/regional-cache';
2+
import { GitbookIncrementalCache } from './incrementalCache';
3+
4+
export default withRegionalCache(new GitbookIncrementalCache(), {
5+
mode: 'long-lived',
6+
// Because of a race condition, the middleware may have populated the cache entry before `cache.match` had time to run on the server.
7+
// TODO: We should bypass the incremental cache entirely when the interceptor has caught the request. Should be done in OpenNext.
8+
bypassTagCacheOnCacheHit: false,
9+
defaultLongLivedTtlSec: 60 * 60 * 24 /* 24 hours */,
10+
// We don't want to update the cache entry on every cache hit
11+
shouldLazilyUpdateOnCacheHit: false,
12+
});

0 commit comments

Comments
 (0)