Skip to content

Commit b3069dc

Browse files
allow null to be returned as an incremental cache get result (#726)
1 parent 94d6eca commit b3069dc

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,19 @@ export default class Cache {
130130
async getFetchCache(key: string, softTags?: string[], tags?: string[]) {
131131
debug("get fetch cache", { key, softTags, tags });
132132
try {
133-
const { value, lastModified } = await globalThis.incrementalCache.get(
134-
key,
135-
true,
136-
);
133+
const cachedEntry = await globalThis.incrementalCache.get(key, true);
134+
135+
if (cachedEntry?.value === undefined) return null;
137136

138137
const _lastModified = await globalThis.tagCache.getLastModified(
139138
key,
140-
lastModified,
139+
cachedEntry?.lastModified,
141140
);
142141
if (_lastModified === -1) {
143142
// If some tags are stale we need to force revalidation
144143
return null;
145144
}
146145

147-
if (value === undefined) return null;
148-
149146
// For cases where we don't have tags, we need to ensure that the soft tags are not being revalidated
150147
// We only need to check for the path as it should already contain all the tags
151148
if ((tags ?? []).length === 0) {
@@ -159,7 +156,7 @@ export default class Cache {
159156
if (path) {
160157
const pathLastModified = await globalThis.tagCache.getLastModified(
161158
path.replace("_N_T_/", ""),
162-
lastModified,
159+
cachedEntry.lastModified,
163160
);
164161
if (pathLastModified === -1) {
165162
// In case the path has been revalidated, we don't want to use the fetch cache
@@ -170,7 +167,7 @@ export default class Cache {
170167

171168
return {
172169
lastModified: _lastModified,
173-
value: value,
170+
value: cachedEntry.value,
174171
} as CacheHandlerValue;
175172
} catch (e) {
176173
// We can usually ignore errors here as they are usually due to cache not being found
@@ -181,18 +178,22 @@ export default class Cache {
181178

182179
async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> {
183180
try {
184-
const { value: cacheData, lastModified } =
185-
await globalThis.incrementalCache.get(key, false);
181+
const cachedEntry = await globalThis.incrementalCache.get(key, false);
182+
183+
if (!cachedEntry?.value) {
184+
return null;
185+
}
186186

187-
const meta = cacheData?.meta;
187+
const meta = cachedEntry.value.meta;
188188
const _lastModified = await globalThis.tagCache.getLastModified(
189189
key,
190-
lastModified,
190+
cachedEntry?.lastModified,
191191
);
192192
if (_lastModified === -1) {
193193
// If some tags are stale we need to force revalidation
194194
return null;
195195
}
196+
const cacheData = cachedEntry?.value;
196197
const requestId = globalThis.__openNextAls.getStore()?.requestId ?? "";
197198
globalThis.lastModified[requestId] = _lastModified;
198199
if (cacheData?.type === "route") {

packages/open-next/src/core/routing/cacheInterceptor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ export async function cacheInterceptor(
157157
try {
158158
const cachedData = await globalThis.incrementalCache.get(localizedPath);
159159
debug("cached data in interceptor", cachedData);
160-
if (cachedData.value?.type === "app") {
160+
161+
if (!cachedData?.value) {
162+
return event;
163+
}
164+
165+
if (cachedData?.value?.type === "app") {
161166
// We need to check the tag cache now
162167
const _lastModified = await globalThis.tagCache.getLastModified(
163168
localizedPath,
@@ -169,7 +174,7 @@ export async function cacheInterceptor(
169174
}
170175
}
171176
const host = event.headers.host;
172-
switch (cachedData.value?.type) {
177+
switch (cachedData?.value?.type) {
173178
case "app":
174179
case "page":
175180
return generateResult(

packages/open-next/src/overrides/incrementalCache/multi-tier-ddb-s3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const multiTierCache: IncrementalCache = {
9292
}
9393
}
9494
const result = await S3Cache.get(key, isFetch);
95-
if (result.value) {
95+
if (result?.value) {
9696
localCache.set(key, {
9797
value: result.value,
9898
lastModified: result.lastModified ?? Date.now(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type IncrementalCache = {
6969
get<IsFetch extends boolean = false>(
7070
key: string,
7171
isFetch?: IsFetch,
72-
): Promise<WithLastModified<CacheValue<IsFetch>>>;
72+
): Promise<WithLastModified<CacheValue<IsFetch>> | null>;
7373
set<IsFetch extends boolean = false>(
7474
key: string,
7575
value: CacheValue<IsFetch>,

0 commit comments

Comments
 (0)