Unverified Commit ef2961c4 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Disable fetch cache size limit for implicit caching during build (#80480)

During build-time prerendering, when we're only caching `fetch` calls
implicitly to improve build performance, and not because the user has
explicitly opted into fetch caching, we should not enforce the 2MB size
limit.

1. The size limit is not required of the file-system-based cache that's
used at build time.
2. The warning is not helpful to users, as they are not aware of the
implicit caching behavior.

Related PRs:

- https://github.com/vercel/next.js/pull/68546
- https://github.com/vercel/next.js/pull/79384
parent 7dad4ac2
...@@ -35,7 +35,7 @@ export interface WorkStore { ...@@ -35,7 +35,7 @@ export interface WorkStore {
readonly cacheLifeProfiles?: { [profile: string]: CacheLife } readonly cacheLifeProfiles?: { [profile: string]: CacheLife }
readonly isOnDemandRevalidate?: boolean readonly isOnDemandRevalidate?: boolean
readonly isPrerendering?: boolean readonly isBuildTimePrerendering?: boolean
readonly isRevalidate?: boolean readonly isRevalidate?: boolean
forceDynamic?: boolean forceDynamic?: boolean
......
...@@ -122,7 +122,7 @@ export function createWorkStore({ ...@@ -122,7 +122,7 @@ export function createWorkStore({
renderOpts.incrementalCache || (globalThis as any).__incrementalCache, renderOpts.incrementalCache || (globalThis as any).__incrementalCache,
cacheLifeProfiles: renderOpts.cacheLifeProfiles, cacheLifeProfiles: renderOpts.cacheLifeProfiles,
isRevalidate: renderOpts.isRevalidate, isRevalidate: renderOpts.isRevalidate,
isPrerendering: renderOpts.nextExport, isBuildTimePrerendering: renderOpts.nextExport,
fetchCache: renderOpts.fetchCache, fetchCache: renderOpts.fetchCache,
isOnDemandRevalidate: renderOpts.isOnDemandRevalidate, isOnDemandRevalidate: renderOpts.isOnDemandRevalidate,
......
...@@ -562,10 +562,13 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -562,10 +562,13 @@ export class IncrementalCache implements IncrementalCacheType {
const itemSize = JSON.stringify(data).length const itemSize = JSON.stringify(data).length
if ( if (
ctx.fetchCache && ctx.fetchCache &&
// we don't show this error/warning when a custom cache handler is being used itemSize > 2 * 1024 * 1024 &&
// as it might not have this limit // We ignore the size limit when custom cache handler is being used, as it
// might not have this limit
!this.hasCustomCacheHandler && !this.hasCustomCacheHandler &&
itemSize > 2 * 1024 * 1024 // We also ignore the size limit when it's an implicit build-time-only
// caching that the user isn't even aware of.
!ctx.isImplicitBuildTimeCache
) { ) {
const warningText = `Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 2MB can not be cached (${itemSize} bytes)` const warningText = `Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 2MB can not be cached (${itemSize} bytes)`
......
...@@ -87,6 +87,7 @@ describe('createPatchedFetcher', () => { ...@@ -87,6 +87,7 @@ describe('createPatchedFetcher', () => {
fetchIdx: 1, fetchIdx: 1,
fetchUrl: 'https://example.com/', fetchUrl: 'https://example.com/',
tags: [], tags: [],
isImplicitBuildTimeCache: false,
} }
) )
}) })
......
...@@ -391,16 +391,24 @@ export function createPatchedFetcher( ...@@ -391,16 +391,24 @@ export function createPatchedFetcher(
currentFetchCacheConfig === 'default') && currentFetchCacheConfig === 'default') &&
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
currentFetchRevalidate == undefined currentFetchRevalidate == undefined
const autoNoCache =
// this condition is hit for null/undefined let autoNoCache = Boolean(
// eslint-disable-next-line eqeqeq (hasUnCacheableHeader || isUnCacheableMethod) &&
(hasNoExplicitCacheConfig && revalidateStore?.revalidate === 0
// we disable automatic no caching behavior during build time SSG so that we can still )
// leverage the fetch cache between SSG workers
!workStore.isPrerendering) || let isImplicitBuildTimeCache = false
((hasUnCacheableHeader || isUnCacheableMethod) &&
revalidateStore && if (!autoNoCache && hasNoExplicitCacheConfig) {
revalidateStore.revalidate === 0) // We don't enable automatic no-cache behavior during build-time
// prerendering so that we can still leverage the fetch cache between
// export workers.
if (workStore.isBuildTimePrerendering) {
isImplicitBuildTimeCache = true
} else {
autoNoCache = true
}
}
if ( if (
hasNoExplicitCacheConfig && hasNoExplicitCacheConfig &&
...@@ -670,7 +678,13 @@ export function createPatchedFetcher( ...@@ -670,7 +678,13 @@ export function createPatchedFetcher(
data: fetchedData, data: fetchedData,
revalidate: normalizedRevalidate, revalidate: normalizedRevalidate,
}, },
{ fetchCache: true, fetchUrl, fetchIdx, tags } {
fetchCache: true,
fetchUrl,
fetchIdx,
tags,
isImplicitBuildTimeCache,
}
) )
await handleUnlock() await handleUnlock()
...@@ -716,7 +730,13 @@ export function createPatchedFetcher( ...@@ -716,7 +730,13 @@ export function createPatchedFetcher(
data: fetchedData, data: fetchedData,
revalidate: normalizedRevalidate, revalidate: normalizedRevalidate,
}, },
{ fetchCache: true, fetchUrl, fetchIdx, tags } {
fetchCache: true,
fetchUrl,
fetchIdx,
tags,
isImplicitBuildTimeCache,
}
) )
} }
}) })
......
...@@ -226,6 +226,7 @@ export interface SetIncrementalFetchCacheContext { ...@@ -226,6 +226,7 @@ export interface SetIncrementalFetchCacheContext {
fetchUrl?: string fetchUrl?: string
fetchIdx?: number fetchIdx?: number
tags?: string[] tags?: string[]
isImplicitBuildTimeCache?: boolean
} }
export interface SetIncrementalResponseCacheContext { export interface SetIncrementalResponseCacheContext {
......
...@@ -25,7 +25,10 @@ describe('app-fetch-deduping', () => { ...@@ -25,7 +25,10 @@ describe('app-fetch-deduping', () => {
successfulRequests.push(req.url) successfulRequests.push(req.url)
} }
res.end(`Request ${req.url} received at ${Date.now()}`) // Generate a response with more than two MB of data.
res.end(
`Request ${req.url} received at ${Date.now()}\n\n${'a'.repeat(2_000_000)}`
)
}) })
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
...@@ -37,24 +40,26 @@ describe('app-fetch-deduping', () => { ...@@ -37,24 +40,26 @@ describe('app-fetch-deduping', () => {
reject(err) reject(err)
}) })
}) })
})
beforeEach(() => {
successfulRequests = []
})
afterAll(() => externalServer.close())
it('dedupes requests amongst static workers', async () => {
await next.patchFile( await next.patchFile(
'next.config.js', 'next.config.js',
`module.exports = { `module.exports = {
env: { TEST_SERVER_PORT: "${externalServerPort}" }, env: { TEST_SERVER_PORT: "${externalServerPort}" },
}` }`
) )
await next.build() await next.build()
})
afterAll(() => externalServer.close())
it('dedupes requests amongst static workers', async () => {
expect(successfulRequests.length).toBe(1) expect(successfulRequests.length).toBe(1)
}) })
it('does not print a fetch cache size limit warning', async () => {
expect(next.cliOutput).not.toInclude('Failed to set Next.js data cache')
})
}) })
} else if (isNextDev) { } else if (isNextDev) {
describe('during next dev', () => { describe('during next dev', () => {
......
export const fetchCache = 'default-cache'
export default function Layout({ children }) { export default function Layout({ children }) {
return ( return (
<html lang="en"> <html lang="en">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment