{"record":{"id":"1654759385b37b69","repo":"vercel/next.js","slug":"failed-to-set-next-js-data-cache-for-ctx-fetchur","errorCode":null,"errorMessage":"Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 2MB can not be cached (${itemSize} bytes)","messagePattern":"Failed to set Next\\.js data cache for (.+?), items over 2MB can not be cached \\((.+?) bytes\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"packages/next/src/server/lib/incremental-cache/index.ts","lineNumber":772,"sourceCode":"\n    pathname = this._getPathname(pathname, ctx.fetchCache)\n\n    // FetchCache has upper limit of 2MB per-entry currently\n    const itemSize = JSON.stringify(data).length\n    if (\n      ctx.fetchCache &&\n      itemSize > 2 * 1024 * 1024 &&\n      // We ignore the size limit when custom cache handler is being used, as it\n      // might not have this limit\n      !this.hasCustomCacheHandler &&\n      // We also ignore the size limit when it's an implicit build-time-only\n      // caching that the user isn't even aware of.\n      !ctx.isImplicitBuildTimeCache\n    ) {\n      const warningText = `Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 2MB can not be cached (${itemSize} bytes)`\n\n      if (this.dev) {\n        throw new Error(warningText)\n      }\n      console.warn(warningText)\n      return\n    }\n\n    try {\n      if (!ctx.fetchCache && ctx.cacheControl) {\n        this.cacheControls.set(toRoute(pathname), ctx.cacheControl)\n      }\n\n      await this.cacheHandler?.set(pathname, data, ctx)\n    } catch (error) {\n      console.warn('Failed to update prerender cache for', pathname, error)\n    }\n  }\n}\n","sourceCodeStart":754,"sourceCodeEnd":789,"githubUrl":"https://github.com/vercel/next.js/blob/0ae8c72462952df163f1b1e0726641bc5b40dc93/packages/next/src/server/lib/incremental-cache/index.ts#L754-L789","documentation":"Thrown (in dev) by the incremental cache set when a fetch-cache entry's serialized JSON exceeds 2 MB and no custom cache handler is in use. The 2 MB per-entry cap prevents unbounded cache growth; in production it only warns and skips caching, but in dev it throws to surface the issue early.","triggerScenarios":"A fetch() returns a very large JSON body that gets cached, and JSON.stringify(data).length > 2*1024*1024. In dev (and not testmode) this throws rather than silently dropping the cache entry.","commonSituations":"Fetching large API responses (big lists, full dumps) and relying on Next's fetch cache. Aggregating many records into one cached fetch.","solutions":["Reduce the payload size at the source (server-side pagination, field selection, compression) so the cached body is under 2 MB.","Split the single large fetch into multiple smaller cached fetches.","If you must cache large payloads, implement a custom cache handler (hasCustomCacheHandler bypasses the limit).","Set { cache: 'no-store' } on oversized fetches that shouldn't be cached."],"exampleFix":"// before: caching a >2MB response\nconst res = await fetch('https://api/all-products', { cache: 'force-cache' })\n\n// after: paginate to keep each cached entry small\nconst res = await fetch('https://api/products?page=1&limit=100', { cache: 'force-cache' })","handlingStrategy":"validation","validationCode":"const MAX = 2 * 1024 * 1024\nconst data = await res.json()\nif (JSON.stringify(data).length > MAX) {\n  throw new Error('Cached fetch payload exceeds 2MB; paginate or use no-store')\n}","typeGuard":"function withinFetchCacheLimit(data: unknown): boolean {\n  return JSON.stringify(data).length <= 2 * 1024 * 1024\n}","tryCatchPattern":"try {\n  await cache.set(...)\n} catch (e) {\n  if (e.message.includes('over 2MB')) {\n    // re-fetch with no-store or paginate\n  }\n}","preventionTips":["Paginate large API responses before caching.","Use a custom cache handler if large entries are unavoidable.","Set cache: 'no-store' on oversized fetches."],"tags":["cache","fetch-cache","size-limit","dev"],"analyzedSha":"0ae8c72462952df163f1b1e0726641bc5b40dc93","analyzedAt":"2026-08-06T19:44:29.143Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}