Skip to content

Commit 1981a47

Browse files
authored
refactor: deprecate global disableDynamoDBCache and disableIncrementalCache (#702)
1 parent 7c9f274 commit 1981a47

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

.changeset/sweet-kiwis-agree.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
refactor(cache): deprecate global disableDynamoDBCache and disableIncrementalCache
6+
7+
In the cache adapter:
8+
9+
- `globalThis.disableDynamoDBCache` is deprecated and will be removed.
10+
use `globalThis.openNextConfig.dangerous?.disableTagCache` instead.
11+
- `globalThis.disableIncrementalCache` is deprecated and will be removed.
12+
use `globalThis.openNextConfig.dangerous?.disableIncrementalCache` instead.

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

Lines changed: 4 additions & 3 deletions
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/overrides/tagCache/dynamodb-lite.ts

Lines changed: 12 additions & 4 deletions
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

Lines changed: 12 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ declare global {
7575
var tagCache: TagCache;
7676
/**
7777
* A boolean that indicates if the DynamoDB cache is disabled.
78-
* TODO: Remove this, we already have access to the config file
78+
* @deprecated This will be removed, use `globalThis.openNextConfig.dangerous?.disableTagCache` instead.
7979
* Defined in esbuild banner for the cache adapter.
8080
*/
8181
var disableDynamoDBCache: boolean;
8282
/**
8383
* A boolean that indicates if the incremental cache is disabled.
84-
* TODO: Remove this, we already have access to the config file
84+
* @deprecated This will be removed, use `globalThis.openNextConfig.dangerous?.disableIncrementalCache` instead.
8585
* Defined in esbuild banner for the cache adapter.
8686
*/
8787
var disableIncrementalCache: boolean;

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

Lines changed: 15 additions & 10 deletions
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)