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 all 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
12 changes: 10 additions & 2 deletions 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
hasCustomCacheHandler: boolean
prerenderManifest: PrerenderManifest
requestHeaders: Record<string, undefined | string | string[]>
requestProtocol?: 'http' | 'https'
Expand Down Expand Up @@ -112,6 +113,7 @@ export class IncrementalCache implements IncrementalCacheType {
experimental: { ppr: boolean }
}) {
const debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
this.hasCustomCacheHandler = Boolean(CurCacheHandler)
if (!CurCacheHandler) {
if (fs && serverDistDir) {
if (debug) {
Expand Down Expand Up @@ -555,8 +557,14 @@ 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) {
// FetchCache has upper limit of 2MB per-entry currently
if (
ctx.fetchCache &&
// we don't show this error/warning when a custom cache handler is being used
// as it might not have this limit
this.hasCustomCacheHandler &&
JSON.stringify(data).length > 2 * 1024 * 1024
) {
if (this.dev) {
throw new Error(`fetch for over 2MB of data can not be cached`)
}
Expand Down
29 changes: 29 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 @@ -516,6 +516,8 @@ createNextDescribe(
'ssr-forced/page.js',
'articles/works.rsc',
'force-cache/page.js',
'force-cache/large-data/page.js',
'force-cache/large-data/page_client-reference-manifest.js',
'ssg-draft-mode.html',
'articles/works.html',
'no-store/static.rsc',
Expand All @@ -533,6 +535,7 @@ createNextDescribe(
'force-static-fetch-no-store/page_client-reference-manifest.js',
'force-static/first.rsc',
'api/draft-mode/route.js',
'api/large-data/route.js',
'blog/tim/first-post.rsc',
'force-static/first.html',
'force-static/second.rsc',
Expand Down Expand Up @@ -770,6 +773,26 @@ createNextDescribe(
"initialRevalidateSeconds": false,
"srcRoute": "/",
},
"/api/large-data": {
"dataRoute": null,
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data",
},
],
"initialHeaders": {
"content-type": "application/json",
"x-next-cache-tags": "_N_T_/layout,_N_T_/api/layout,_N_T_/api/large-data/layout,_N_T_/api/large-data/route,_N_T_/api/large-data",
},
"initialRevalidateSeconds": false,
"srcRoute": "/api/large-data",
},
"/articles/works": {
"dataRoute": "/articles/works.rsc",
"experimentalBypassFor": [
Expand Down Expand Up @@ -3053,6 +3076,12 @@ 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 () => {
await next.fetch('/force-cache/large-data')
await next.fetch('/force-cache/large-data')
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'