Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure fetch cache TTL is updated properly #69164

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Ensure fetch cache TTL is updated properly
  • Loading branch information
ijjk committed Aug 21, 2024
commit 90d0d3b12993223996404d618d482e7b9e6a6adf
20 changes: 0 additions & 20 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,26 +337,6 @@ export default class FetchCache implements CacheHandler {
public async set(...args: Parameters<CacheHandler['set']>) {
const [key, data, ctx] = args

const newValue =
data?.kind === CachedRouteKind.FETCH ? data.data : undefined
const existingCache = memoryCache?.get(key)
const existingValue = existingCache?.value
if (
existingValue?.kind === CachedRouteKind.FETCH &&
Object.keys(existingValue.data).every(
(field) =>
JSON.stringify(
(existingValue.data as Record<string, string | Object>)[field]
) ===
JSON.stringify((newValue as Record<string, string | Object>)[field])
)
) {
if (DEBUG) {
console.log(`skipping cache set for ${key} as not modified`)
}
return
}

const { fetchCache, fetchIdx, fetchUrl, tags } = ctx
if (!fetchCache) return

Expand Down
33 changes: 33 additions & 0 deletions test/production/app-dir/fetch-cache/app/not-changed/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { unstable_cache } from 'next/cache'

export const dynamic = 'force-dynamic'
export const fetchCache = 'default-cache'

// this is ensuring we still update TTL even
// if the cache value didn't change during revalidate
const stableCacheItem = unstable_cache(
async () => {
return 'consistent value'
},
[],
{
revalidate: 3,
tags: ['thankyounext'],
}
)

export default async function Page() {
const data = await fetch('https://example.vercel.sh', {
next: { tags: ['thankyounext'], revalidate: 3 },
}).then((res) => res.text())

const cachedValue = stableCacheItem()

return (
<>
<p>hello world</p>
<p id="data">{data}</p>
<p id="cache">{cachedValue}</p>
</>
)
}
30 changes: 30 additions & 0 deletions test/production/app-dir/fetch-cache/fetch-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,34 @@ describe('fetch-cache', () => {
fetchGetShouldError = false
}
})

it('should update cache TTL even if cache data does not change', async () => {
const fetchCacheRequestsIndex = fetchCacheRequests.length

for (let i = 0; i < 3; i++) {
const res = await fetchViaHTTP(appPort, '/not-changed')
expect(res.status).toBe(200)
// give time for revalidate period to pass
await new Promise((resolve) => setTimeout(resolve, 3_000))
}

const newCacheGets = []
const newCacheSets = []

for (
let i = fetchCacheRequestsIndex - 1;
i < fetchCacheRequests.length;
i++
) {
const requestItem = fetchCacheRequests[i]
if (requestItem.method === 'get') {
newCacheGets.push(requestItem)
}
if (requestItem.method === 'post') {
newCacheSets.push(requestItem)
}
}
expect(newCacheGets.length).toBeGreaterThanOrEqual(2)
expect(newCacheSets.length).toBeGreaterThanOrEqual(2)
})
})
Loading