Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/sweet-kiwis-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@opennextjs/aws": patch
---

refactor(cache): deprecate global disableDynamoDBCache and disableIncrementalCache

In the cache adapter:

- `globalThis.disableDynamoDBCache` is deprecated and will be removed.
use `globalThis.openNextConfig.dangerous?.disableTagCache` instead.
- `globalThis.disableIncrementalCache` is deprecated and will be removed.
use `globalThis.openNextConfig.dangerous?.disableIncrementalCache` instead.
7 changes: 4 additions & 3 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class Cache {
kind?: "FETCH";
},
) {
if (globalThis.disableIncrementalCache) {
if (globalThis.openNextConfig.dangerous?.disableIncrementalCache) {
return null;
}

Expand Down Expand Up @@ -260,7 +260,7 @@ export default class Cache {
data?: IncrementalCacheValue,
ctx?: IncrementalCacheContext,
): Promise<void> {
if (globalThis.disableIncrementalCache) {
if (globalThis.openNextConfig.dangerous?.disableIncrementalCache) {
return;
}
// This one might not even be necessary anymore
Expand Down Expand Up @@ -396,7 +396,8 @@ export default class Cache {
}

public async revalidateTag(tags: string | string[]) {
if (globalThis.disableDynamoDBCache || globalThis.disableIncrementalCache) {
const config = globalThis.openNextConfig.dangerous;
if (config?.disableTagCache || config?.disableIncrementalCache) {
return;
}
try {
Expand Down
16 changes: 12 additions & 4 deletions packages/open-next/src/overrides/tagCache/dynamodb-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ function buildDynamoObject(path: string, tags: string, revalidatedAt?: number) {
const tagCache: TagCache = {
async getByPath(path) {
try {
if (globalThis.disableDynamoDBCache) return [];
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return [];
}
const { CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
const result = await awsFetch(
JSON.stringify({
Expand Down Expand Up @@ -101,7 +103,9 @@ const tagCache: TagCache = {
},
async getByTag(tag) {
try {
if (globalThis.disableDynamoDBCache) return [];
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return [];
}
const { CACHE_DYNAMO_TABLE, NEXT_BUILD_ID } = process.env;
const result = await awsFetch(
JSON.stringify({
Expand Down Expand Up @@ -133,7 +137,9 @@ const tagCache: TagCache = {
},
async getLastModified(key, lastModified) {
try {
if (globalThis.disableDynamoDBCache) return lastModified ?? Date.now();
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return lastModified ?? Date.now();
}
const { CACHE_DYNAMO_TABLE } = process.env;
const result = await awsFetch(
JSON.stringify({
Expand Down Expand Up @@ -168,7 +174,9 @@ const tagCache: TagCache = {
async writeTags(tags) {
try {
const { CACHE_DYNAMO_TABLE } = process.env;
if (globalThis.disableDynamoDBCache) return;
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return;
}
const dataChunks = chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map(
(Items) => ({
RequestItems: {
Expand Down
16 changes: 12 additions & 4 deletions packages/open-next/src/overrides/tagCache/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ function buildDynamoObject(path: string, tags: string, revalidatedAt?: number) {
const tagCache: TagCache = {
async getByPath(path) {
try {
if (globalThis.disableDynamoDBCache) return [];
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return [];
}
const result = await dynamoClient.send(
new QueryCommand({
TableName: CACHE_DYNAMO_TABLE,
Expand All @@ -69,7 +71,9 @@ const tagCache: TagCache = {
},
async getByTag(tag) {
try {
if (globalThis.disableDynamoDBCache) return [];
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return [];
}
const { Items } = await dynamoClient.send(
new QueryCommand({
TableName: CACHE_DYNAMO_TABLE,
Expand All @@ -95,7 +99,9 @@ const tagCache: TagCache = {
},
async getLastModified(key, lastModified) {
try {
if (globalThis.disableDynamoDBCache) return lastModified ?? Date.now();
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return lastModified ?? Date.now();
}
const result = await dynamoClient.send(
new QueryCommand({
TableName: CACHE_DYNAMO_TABLE,
Expand Down Expand Up @@ -123,7 +129,9 @@ const tagCache: TagCache = {
},
async writeTags(tags) {
try {
if (globalThis.disableDynamoDBCache) return;
if (globalThis.openNextConfig.dangerous?.disableTagCache) {
return;
}
const dataChunks = chunk(tags, MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT).map(
(Items) => ({
RequestItems: {
Expand Down
4 changes: 2 additions & 2 deletions packages/open-next/src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ declare global {
var tagCache: TagCache;
/**
* A boolean that indicates if the DynamoDB cache is disabled.
* TODO: Remove this, we already have access to the config file
* @deprecated This will be removed, use `globalThis.openNextConfig.dangerous?.disableTagCache` instead.
* Defined in esbuild banner for the cache adapter.
*/
var disableDynamoDBCache: boolean;
/**
* A boolean that indicates if the incremental cache is disabled.
* TODO: Remove this, we already have access to the config file
* @deprecated This will be removed, use `globalThis.openNextConfig.dangerous?.disableIncrementalCache` instead.
* Defined in esbuild banner for the cache adapter.
*/
var disableIncrementalCache: boolean;
Expand Down
25 changes: 15 additions & 10 deletions packages/tests-unit/tests/adapters/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import Cache from "@opennextjs/aws/adapters/cache.js";
import { vi } from "vitest";

declare global {
var disableIncrementalCache: boolean;
var disableDynamoDBCache: boolean;
var openNextConfig: {
dangerous: { disableIncrementalCache?: boolean; disableTagCache?: boolean };
};
var isNextAfter15: boolean;
}

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

cache = new Cache();

globalThis.disableIncrementalCache = false;
globalThis.openNextConfig = {
dangerous: {
disableIncrementalCache: false,
},
};
globalThis.isNextAfter15 = false;

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

describe("disableIncrementalCache", () => {
beforeEach(() => {
globalThis.disableIncrementalCache = true;
globalThis.openNextConfig.dangerous.disableIncrementalCache = true;
});

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

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

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

expect(incrementalCache.set).not.toHaveBeenCalled();
});

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

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

Expand Down Expand Up @@ -480,19 +485,19 @@ describe("CacheHandler", () => {

describe("revalidateTag", () => {
beforeEach(() => {
globalThis.disableDynamoDBCache = false;
globalThis.disableIncrementalCache = false;
globalThis.openNextConfig.dangerous.disableTagCache = false;
globalThis.openNextConfig.dangerous.disableIncrementalCache = false;
});
it("Should do nothing if disableIncrementalCache is true", async () => {
globalThis.disableIncrementalCache = true;
globalThis.openNextConfig.dangerous.disableIncrementalCache = true;

await cache.revalidateTag("tag");

expect(tagCache.writeTags).not.toHaveBeenCalled();
});

it("Should do nothing if disableTagCache is true", async () => {
globalThis.disableDynamoDBCache = true;
globalThis.openNextConfig.dangerous.disableTagCache = true;

await cache.revalidateTag("tag");

Expand Down
Loading