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 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
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/fetch-cache-limit/app/api/mock/route.ts
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(''),
})
}
14 changes: 14 additions & 0 deletions test/e2e/fetch-cache-limit/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}

export const dynamic = 'force-dynamic'
8 changes: 8 additions & 0 deletions test/e2e/fetch-cache-limit/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default async function Page() {
const resp = await fetch(`http://localhost:${process.env.PORT}/api/mock`, {
cache: 'force-cache',
})
const data = await resp.json()

return <p id="content">{data.content}</p>
}
19 changes: 19 additions & 0 deletions test/e2e/fetch-cache-limit/cache-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const cache = new Map()

module.exports = class CacheHandler {
constructor(options) {
this.cache = {}
this.options = options
}

async get(key) {
return cache.get(key)
}

async set(key, data) {
cache.set(key, {
value: data,
lastModified: Date.now(),
})
}
}
22 changes: 22 additions & 0 deletions test/e2e/fetch-cache-limit/fetch-cache-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createNextDescribe } from 'e2e-utils'
vordgi marked this conversation as resolved.
Show resolved Hide resolved

createNextDescribe(
'fetch-cache-limit',
{
files: __dirname,
// don't have access to runtime logs on deploy
skipDeployment: true,
},
({ next }) => {
it('when awaiting `fetch` using an unknown domain, stack traces are preserved', async () => {
const browser = await next.browser('/')
await browser.waitForElementByCss('#content')
await browser.refresh()
await browser.waitForElementByCss('#content')
await browser.refresh()
await browser.waitForElementByCss('#content')
// console.log(next.cliOutput.match('Load data'))
expect(next.cliOutput.match('Load data').length).toBe(1)
})
}
)
6 changes: 6 additions & 0 deletions test/e2e/fetch-cache-limit/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
incrementalCacheHandlerPath: require.resolve('./cache-handler.js'),
},
}
Loading