Skip to content

Commit

Permalink
add CachedFetchValue overrides type (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz authored Feb 6, 2025
1 parent f9b9ae9 commit 867defe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-boats-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

add and expose new `CachedFetchValue` type
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ const buildDynamoKey = (key: string) => {
*/
const multiTierCache: IncrementalCache = {
name: "multi-tier-ddb-s3",
async get(key, isFetch) {
async get<IsFetch extends boolean = false>(key: string, isFetch?: IsFetch) {
// First we check the local cache
const localCacheEntry = localCache.get(key);
const localCacheEntry = localCache.get(key) as
| {
value: CacheValue<IsFetch>;
lastModified: number;
}
| undefined;

if (localCacheEntry) {
if (Date.now() - localCacheEntry.lastModified < localCacheTTL) {
debug("Using local cache without checking ddb");
Expand Down
18 changes: 16 additions & 2 deletions packages/open-next/src/types/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,29 @@ export type CachedFile =
meta?: Meta;
};

export type FetchCache = Object;
// type taken from: https://github.com/vercel/next.js/blob/9a1cd356/packages/next/src/server/response-cache/types.ts#L26-L38
export type CachedFetchValue = {
kind: "FETCH";
data: {
headers: { [k: string]: string };
body: string;
url: string;
status?: number;
// field used by older versions of Next.js (see: https://github.com/vercel/next.js/blob/fda1ecc/packages/next/src/server/response-cache/types.ts#L23)
tags?: string[];
};
// tags are only present with file-system-cache
// fetch cache stores tags outside of cache entry
tags?: string[];
};

export type WithLastModified<T> = {
lastModified?: number;
value?: T;
};

export type CacheValue<IsFetch extends boolean> = (IsFetch extends true
? FetchCache
? CachedFetchValue
: CachedFile) & { revalidate?: number | false };

export type IncrementalCache = {
Expand Down

0 comments on commit 867defe

Please sign in to comment.