diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts index a52bd12db..8edc8ced3 100644 --- a/packages/open-next/src/adapters/cache.ts +++ b/packages/open-next/src/adapters/cache.ts @@ -73,17 +73,6 @@ type IncrementalCacheContext = { tags?: string[] | undefined; }; -interface CacheHandlerContext { - fs?: never; - dev?: boolean; - flushToDisk?: boolean; - serverDistDir?: string; - maxMemoryCacheSize?: number; - _appDir: boolean; - _requestHeaders: never; - fetchCacheKeyPrefix?: string; -} - interface CacheHandlerValue { lastModified?: number; age?: number; @@ -91,13 +80,6 @@ interface CacheHandlerValue { value: IncrementalCacheValue | null; } -/** Beginning single backslash is intentional, to look for the dot + the extension. Do not escape it again. */ -const CACHE_EXTENSION_REGEX = /\.(cache|fetch)$/; - -export function hasCacheExtension(key: string) { - return CACHE_EXTENSION_REGEX.test(key); -} - function isFetchCache( options?: | boolean @@ -120,7 +102,7 @@ function isFetchCache( return false; } // We need to use globalThis client here as this class can be defined at load time in next 12 but client is not available at load time -export default class S3Cache { +export default class Cache { public async get( key: string, // fetchCache is for next 13.5 and above, kindHint is for next 14 and above and boolean is for earlier versions @@ -152,7 +134,7 @@ export default class S3Cache { key, true, ); - // const { Body, LastModified } = await this.getS3Object(key, "fetch"); + const _lastModified = await globalThis.tagCache.getLastModified( key, lastModified, @@ -201,10 +183,7 @@ export default class S3Cache { try { const { value: cacheData, lastModified } = await globalThis.incrementalCache.get(key, false); - // const { Body, LastModified } = await this.getS3Object(key, "cache"); - // const cacheData = JSON.parse( - // (await Body?.transformToString()) ?? "{}", - // ) as S3CachedFile; + const meta = cacheData?.meta; const _lastModified = await globalThis.tagCache.getLastModified( key, diff --git a/packages/open-next/src/types/overrides.ts b/packages/open-next/src/types/overrides.ts index f2de3bbd3..51370e4b1 100644 --- a/packages/open-next/src/types/overrides.ts +++ b/packages/open-next/src/types/overrides.ts @@ -29,7 +29,7 @@ export interface Queue { // Incremental cache -export type S3CachedFile = +export type CachedFile = | { type: "redirect"; props?: Object; @@ -53,7 +53,7 @@ export type S3CachedFile = meta?: Meta; }; -export type S3FetchCache = Object; +export type FetchCache = Object; export type WithLastModified = { lastModified?: number; @@ -61,8 +61,8 @@ export type WithLastModified = { }; export type CacheValue = (IsFetch extends true - ? S3FetchCache - : S3CachedFile) & { revalidate?: number | false }; + ? FetchCache + : CachedFile) & { revalidate?: number | false }; export type IncrementalCache = { get( diff --git a/packages/tests-unit/tests/adapters/cache.test.ts b/packages/tests-unit/tests/adapters/cache.test.ts index f5624b8ca..ff57f89e8 100644 --- a/packages/tests-unit/tests/adapters/cache.test.ts +++ b/packages/tests-unit/tests/adapters/cache.test.ts @@ -1,5 +1,5 @@ /* eslint-disable sonarjs/no-duplicate-string */ -import S3Cache, { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js"; +import Cache from "@opennextjs/aws/adapters/cache.js"; import { vi } from "vitest"; declare global { @@ -7,29 +7,12 @@ declare global { var isNextAfter15: boolean; } -describe("hasCacheExtension", () => { - it("Should returns true if has an extension and it is a CacheExtension", () => { - expect(hasCacheExtension("hello.cache")).toBeTruthy(); - }); - - it("Should returns false if has an extension and it is not a CacheExtension", () => { - expect(hasCacheExtension("hello.json")).toBeFalsy(); - }); - - it("Should return false if does not have any extension", () => { - expect(hasCacheExtension("hello,json")).toBeFalsy(); - }); -}); - -describe("S3Cache", () => { - let cache: S3Cache; +describe("CacheHandler", () => { + let cache: Cache; vi.useFakeTimers().setSystemTime("2024-01-02T00:00:00Z"); - const getFetchCacheSpy = vi.spyOn(S3Cache.prototype, "getFetchCache"); - const getIncrementalCache = vi.spyOn( - S3Cache.prototype, - "getIncrementalCache", - ); + const getFetchCacheSpy = vi.spyOn(Cache.prototype, "getFetchCache"); + const getIncrementalCache = vi.spyOn(Cache.prototype, "getIncrementalCache"); const incrementalCache = { name: "mock", @@ -70,7 +53,7 @@ describe("S3Cache", () => { beforeEach(() => { vi.clearAllMocks(); - cache = new S3Cache(); + cache = new Cache(); globalThis.disableIncrementalCache = false; globalThis.isNextAfter15 = false;