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

Disable 2mb limit for custom incrementalCacheHandler #59976

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 8 additions & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class CacheHandler {
export class IncrementalCache implements IncrementalCacheType {
dev?: boolean
cacheHandler?: CacheHandler
isFetchCacherPossible: boolean
prerenderManifest: PrerenderManifest
requestHeaders: Record<string, undefined | string | string[]>
requestProtocol?: 'http' | 'https'
Expand Down Expand Up @@ -112,6 +113,8 @@ export class IncrementalCache implements IncrementalCacheType {
experimental: { ppr: boolean }
}) {
const debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
this.isFetchCacherPossible =
!CurCacheHandler || CurCacheHandler instanceof FetchCache
if (!CurCacheHandler) {
if (fs && serverDistDir) {
if (debug) {
Expand Down Expand Up @@ -556,7 +559,11 @@ export class IncrementalCache implements IncrementalCacheType {

if (this.dev && !ctx.fetchCache) return
// fetchCache has upper limit of 2MB per-entry currently
if (ctx.fetchCache && JSON.stringify(data).length > 2 * 1024 * 1024) {
if (
ctx.fetchCache &&
this.isFetchCacherPossible &&
ijjk marked this conversation as resolved.
Show resolved Hide resolved
JSON.stringify(data).length > 2 * 1024 * 1024
) {
if (this.dev) {
throw new Error(`fetch for over 2MB of data can not be cached`)
}
ijjk marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,16 @@ createNextDescribe(
expect(next.cliOutput).toContain('cache-handler get')
expect(next.cliOutput).toContain('cache-handler set')
})

it('should load large data only once when using custom cache handler and force-cache mode', async () => {
const browser = await next.browser('/force-cache/large-data')
await browser.waitForElementByCss('#content')
await browser.refresh()
await browser.waitForElementByCss('#content')
await browser.refresh()
await browser.waitForElementByCss('#content')
expect(next.cliOutput.match('Load data').length).toBe(1)
})
}
}
)
10 changes: 10 additions & 0 deletions test/e2e/app-dir/app-static/app/api/large-data/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NextResponse } from 'next/server'

export async function GET() {
console.log('Load data')
return NextResponse.json({
content: Array.from(new Array(3 * 1024 * 1024))
.map(() => 1)
.join(''),
})
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/app-static/app/force-cache/large-data/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default async function Page() {
const resp = await fetch(
`http://localhost:${process.env.PORT}/api/large-data`,
{
cache: 'force-cache',
}
)
const data = await resp.json()

return <p id="content">{data.content}</p>
}

export const dynamic = 'force-dynamic'
Loading