Skip to content

Commit 2c93f9a

Browse files
committed
refactor: drop global disableDynamoDBCache and disableIncrementalCache
1 parent 7c9f274 commit 2c93f9a

File tree

6 files changed

+44
-42
lines changed

6 files changed

+44
-42
lines changed

packages/open-next/src/adapters/cache.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default class Cache {
116116
kind?: "FETCH";
117117
},
118118
) {
119-
if (globalThis.disableIncrementalCache) {
119+
if (globalThis.openNextConfig.dangerous?.disableIncrementalCache) {
120120
return null;
121121
}
122122

@@ -260,7 +260,7 @@ export default class Cache {
260260
data?: IncrementalCacheValue,
261261
ctx?: IncrementalCacheContext,
262262
): Promise<void> {
263-
if (globalThis.disableIncrementalCache) {
263+
if (globalThis.openNextConfig.dangerous?.disableIncrementalCache) {
264264
return;
265265
}
266266
// This one might not even be necessary anymore
@@ -396,7 +396,8 @@ export default class Cache {
396396
}
397397

398398
public async revalidateTag(tags: string | string[]) {
399-
if (globalThis.disableDynamoDBCache || globalThis.disableIncrementalCache) {
399+
const config = globalThis.openNextConfig.dangerous;
400+
if (config?.disableTagCache || config?.disableIncrementalCache) {
400401
return;
401402
}
402403
try {

packages/open-next/src/build/compileCache.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ export function compileCache(
2828
target: ["node18"],
2929
format,
3030
banner: {
31-
js: [
32-
`globalThis.disableIncrementalCache = ${
33-
config.dangerous?.disableIncrementalCache ?? false
34-
};`,
35-
`globalThis.disableDynamoDBCache = ${
36-
config.dangerous?.disableTagCache ?? false
37-
};`,
38-
`globalThis.isNextAfter15 = ${isAfter15};`,
39-
].join(""),
31+
js: `globalThis.isNextAfter15 = ${isAfter15};`,
4032
},
4133
},
4234
options,

packages/open-next/src/overrides/tagCache/dynamodb-lite.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ function buildDynamoObject(path: string, tags: string, revalidatedAt?: number) {
6868
const tagCache: TagCache = {
6969
async getByPath(path) {
7070
try {
71-
if (globalThis.disableDynamoDBCache) return [];
71+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
72+
return [];
73+
}
7274
const { CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
7375
const result = await awsFetch(
7476
JSON.stringify({
@@ -101,7 +103,9 @@ const tagCache: TagCache = {
101103
},
102104
async getByTag(tag) {
103105
try {
104-
if (globalThis.disableDynamoDBCache) return [];
106+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
107+
return [];
108+
}
105109
const { CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
106110
const result = await awsFetch(
107111
JSON.stringify({
@@ -133,7 +137,9 @@ const tagCache: TagCache = {
133137
},
134138
async getLastModified(key, lastModified) {
135139
try {
136-
if (globalThis.disableDynamoDBCache) return lastModified ?? Date.now();
140+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
141+
return lastModified ?? Date.now();
142+
}
137143
const { CACHE_DYNAMO_TABLE } = process.env;
138144
const result = await awsFetch(
139145
JSON.stringify({
@@ -168,7 +174,9 @@ const tagCache: TagCache = {
168174
async writeTags(tags) {
169175
try {
170176
const { CACHE_DYNAMO_TABLE } = process.env;
171-
if (globalThis.disableDynamoDBCache) return;
177+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
178+
return;
179+
}
172180
const dataChunks = chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map(
173181
(Items) => ({
174182
RequestItems: {

packages/open-next/src/overrides/tagCache/dynamodb.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ function buildDynamoObject(path: string, tags: string, revalidatedAt?: number) {
4444
const tagCache: TagCache = {
4545
async getByPath(path) {
4646
try {
47-
if (globalThis.disableDynamoDBCache) return [];
47+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
48+
return [];
49+
}
4850
const result = await dynamoClient.send(
4951
new QueryCommand({
5052
TableName: CACHE_DYNAMO_TABLE,
@@ -69,7 +71,9 @@ const tagCache: TagCache = {
6971
},
7072
async getByTag(tag) {
7173
try {
72-
if (globalThis.disableDynamoDBCache) return [];
74+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
75+
return [];
76+
}
7377
const { Items } = await dynamoClient.send(
7478
new QueryCommand({
7579
TableName: CACHE_DYNAMO_TABLE,
@@ -95,7 +99,9 @@ const tagCache: TagCache = {
9599
},
96100
async getLastModified(key, lastModified) {
97101
try {
98-
if (globalThis.disableDynamoDBCache) return lastModified ?? Date.now();
102+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
103+
return lastModified ?? Date.now();
104+
}
99105
const result = await dynamoClient.send(
100106
new QueryCommand({
101107
TableName: CACHE_DYNAMO_TABLE,
@@ -123,7 +129,9 @@ const tagCache: TagCache = {
123129
},
124130
async writeTags(tags) {
125131
try {
126-
if (globalThis.disableDynamoDBCache) return;
132+
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
133+
return;
134+
}
127135
const dataChunks = chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map(
128136
(Items) => ({
129137
RequestItems: {

packages/open-next/src/types/global.ts

-12
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ declare global {
7373
* Defined in `createMainHandler` or in `adapter/middleware.ts`.
7474
*/
7575
var tagCache: TagCache;
76-
/**
77-
* A boolean that indicates if the DynamoDB cache is disabled.
78-
* TODO: Remove this, we already have access to the config file
79-
* Defined in esbuild banner for the cache adapter.
80-
*/
81-
var disableDynamoDBCache: boolean;
82-
/**
83-
* A boolean that indicates if the incremental cache is disabled.
84-
* TODO: Remove this, we already have access to the config file
85-
* Defined in esbuild banner for the cache adapter.
86-
*/
87-
var disableIncrementalCache: boolean;
8876

8977
/**
9078
* An object that contains the last modified time of the pages.

packages/tests-unit/tests/adapters/cache.test.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import Cache from "@opennextjs/aws/adapters/cache.js";
33
import { vi } from "vitest";
44

55
declare global {
6-
var disableIncrementalCache: boolean;
7-
var disableDynamoDBCache: boolean;
6+
var openNextConfig: {
7+
dangerous: { disableIncrementalCache?: boolean; disableTagCache?: boolean };
8+
};
89
var isNextAfter15: boolean;
910
}
1011

@@ -62,7 +63,11 @@ describe("CacheHandler", () => {
6263

6364
cache = new Cache();
6465

65-
globalThis.disableIncrementalCache = false;
66+
globalThis.openNextConfig = {
67+
dangerous: {
68+
disableIncrementalCache: false,
69+
},
70+
};
6671
globalThis.isNextAfter15 = false;
6772

6873
globalThis.lastModified = {};
@@ -79,7 +84,7 @@ describe("CacheHandler", () => {
7984

8085
describe("disableIncrementalCache", () => {
8186
beforeEach(() => {
82-
globalThis.disableIncrementalCache = true;
87+
globalThis.openNextConfig.dangerous.disableIncrementalCache = true;
8388
});
8489

8590
it("Should return null when incremental cache is disabled", async () => {
@@ -89,15 +94,15 @@ describe("CacheHandler", () => {
8994
});
9095

9196
it("Should not set cache when incremental cache is disabled", async () => {
92-
globalThis.disableIncrementalCache = true;
97+
globalThis.openNextConfig.dangerous.disableIncrementalCache = true;
9398

9499
await cache.set("key", { kind: "REDIRECT", props: {} });
95100

96101
expect(incrementalCache.set).not.toHaveBeenCalled();
97102
});
98103

99104
it("Should not delete cache when incremental cache is disabled", async () => {
100-
globalThis.disableIncrementalCache = true;
105+
globalThis.openNextConfig.dangerous.disableIncrementalCache = true;
101106

102107
await cache.set("key", undefined);
103108

@@ -480,19 +485,19 @@ describe("CacheHandler", () => {
480485

481486
describe("revalidateTag", () => {
482487
beforeEach(() => {
483-
globalThis.disableDynamoDBCache = false;
484-
globalThis.disableIncrementalCache = false;
488+
globalThis.openNextConfig.dangerous.disableTagCache = false;
489+
globalThis.openNextConfig.dangerous.disableIncrementalCache = false;
485490
});
486491
it("Should do nothing if disableIncrementalCache is true", async () => {
487-
globalThis.disableIncrementalCache = true;
492+
globalThis.openNextConfig.dangerous.disableIncrementalCache = true;
488493

489494
await cache.revalidateTag("tag");
490495

491496
expect(tagCache.writeTags).not.toHaveBeenCalled();
492497
});
493498

494499
it("Should do nothing if disableTagCache is true", async () => {
495-
globalThis.disableDynamoDBCache = true;
500+
globalThis.openNextConfig.dangerous.disableTagCache = true;
496501

497502
await cache.revalidateTag("tag");
498503

0 commit comments

Comments
 (0)