Skip to content

Commit

Permalink
correct wrong logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Dec 27, 2023
1 parent 2229136 commit ad9cdbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
12 changes: 5 additions & 7 deletions packages/next-on-pages/templates/_worker.js/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const CACHE_TAGS_HEADER = 'x-vercel-cache-tags';
// https://github.com/vercel/next.js/blob/ba23d986/packages/next/src/lib/constants.ts#L18
const NEXT_CACHE_SOFT_TAGS_HEADER = 'x-next-cache-soft-tags';

// https://github.com/vercel/next.js/blob/fc25fcef/packages/next/src/server/lib/incremental-cache/fetch-cache.ts#L21
const CACHE_STATE_HEADER = 'x-vercel-cache-state';

/**
* Handles an internal request to the suspense cache.
*
Expand Down Expand Up @@ -52,20 +55,15 @@ export async function handleSuspenseCacheRequest(
NEXT_CACHE_SOFT_TAGS_HEADER,
);

// Retrieve the value from the cache.
const data = await incrementalCache.get(cacheKey, { softTags });
if (!data) return new Response(null, { status: 404 });

const lastModified =
typeof data.curRevalidate === 'number'
? data.curRevalidate
: Date.now();
return new Response(JSON.stringify(data.value), {
status: 200,
headers: {
'Content-Type': 'application/json',
'x-vercel-cache-state': 'fresh',
age: `${(Date.now() - lastModified) / 1000}`,
[CACHE_STATE_HEADER]: 'fresh',
age: `${data.age}`,
},
});
}
Expand Down
49 changes: 27 additions & 22 deletions packages/next-on-pages/templates/cache/incrementalCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class CacheHandler {
/* eslint-enable */

/**
* Simplified version of the IncrementalCache from Next.js
* Simplified (and tweaked) version of the IncrementalCache from Next.js
* source: https://github.com/vercel/next.js/blob/c4adae89b/packages/next/src/server/lib/incremental-cache/index.ts#L65
*/
export class IncrementalCache {
Expand Down Expand Up @@ -89,12 +89,16 @@ export class IncrementalCache {
tags?: string[];
softTags?: string[];
} = {},
): Promise<IncrementalCacheEntry | null> {
let entry: IncrementalCacheEntry | null = null;
): Promise<(IncrementalCacheEntry & { age?: number }) | null> {
let entry: (IncrementalCacheEntry & { age?: number }) | null = null;
let revalidate = ctx.revalidate;

const cacheData = await this.cacheHandler?.get(cacheKey, ctx);

const age = cacheData
? (Date.now() - (cacheData.lastModified || 0)) / 1000
: undefined;

if (cacheData?.value?.kind === 'FETCH') {
const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])];
// if a tag was revalidated we don't return stale data
Expand All @@ -107,9 +111,10 @@ export class IncrementalCache {
}

revalidate = revalidate || cacheData.value.revalidate;
const age = (Date.now() - (cacheData.lastModified || 0)) / 1000;

const isStale = typeof revalidate === 'number' && age > revalidate;
const isStale =
typeof revalidate === 'number' &&
typeof age === 'number' &&
age > revalidate;
const data = cacheData.value.data;

return {
Expand All @@ -119,6 +124,7 @@ export class IncrementalCache {
data,
revalidate: revalidate,
},
age,
revalidateAfter:
typeof revalidate === 'number' && Date.now() + revalidate * 1000,
};
Expand All @@ -128,31 +134,30 @@ export class IncrementalCache {
let revalidateAfter: false | number;

if (cacheData?.lastModified === -1) {
isStale = -1;
revalidateAfter = -1 * CACHE_ONE_YEAR;
isStale = -1;
} else {
revalidateAfter = 1 * 1000 + (cacheData?.lastModified || Date.now());

isStale = revalidateAfter < Date.now() ? true : undefined;
}

if (cacheData) {
entry = {
isStale,
revalidateAfter,
value: cacheData.value,
};
}
entry = {
isStale,
revalidateAfter,
value: null,
};

if (!cacheData) {
entry = {
isStale,
value: null,
revalidateAfter,
};
if (cacheData) {
entry.value = cacheData.value;
entry.age = age;
} else {
await this.set(cacheKey, entry.value, ctx);
}
return entry;

return {
...entry,
age,
};
}

async set(
Expand Down

0 comments on commit ad9cdbf

Please sign in to comment.