{"record":{"id":"c51c6ef000398775","repo":"vercel/next.js","slug":"preview-data-is-limited-to-2kb-currently-reduce-h","errorCode":null,"errorMessage":"Preview data is limited to 2KB currently, reduce how much data you are storing as preview data to continue","messagePattern":"Preview data is limited to 2KB currently, reduce how much data you are storing as preview data to continue","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/next/src/server/api-utils/node/api-resolver.ts","lineNumber":206,"sourceCode":"    {\n      data: encryptWithSecret(\n        Buffer.from(options.previewModeEncryptionKey),\n        JSON.stringify(data)\n      ),\n    },\n    options.previewModeSigningKey,\n    {\n      algorithm: 'HS256',\n      ...(options.maxAge !== undefined\n        ? { expiresIn: options.maxAge }\n        : undefined),\n    }\n  )\n\n  // limit preview mode cookie to 2KB since we shouldn't store too much\n  // data here and browsers drop cookies over 4KB\n  if (payload.length > 2048) {\n    throw new Error(\n      `Preview data is limited to 2KB currently, reduce how much data you are storing as preview data to continue`\n    )\n  }\n\n  const { serialize } =\n    require('next/dist/compiled/cookie') as typeof import('next/dist/compiled/cookie')\n  const previous = res.getHeader('Set-Cookie')\n  res.setHeader(`Set-Cookie`, [\n    ...(typeof previous === 'string'\n      ? [previous]\n      : Array.isArray(previous)\n        ? previous\n        : []),\n    serialize(COOKIE_NAME_PRERENDER_BYPASS, options.previewModeId, {\n      httpOnly: true,\n      sameSite: process.env.NODE_ENV !== 'development' ? 'none' : 'lax',\n      secure: process.env.NODE_ENV !== 'development',\n      path: '/',","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/vercel/next.js/blob/0ae8c72462952df163f1b1e0726641bc5b40dc93/packages/next/src/server/api-utils/node/api-resolver.ts#L188-L224","documentation":"Thrown by setPreviewData() when the signed+encrypted preview-data cookie payload exceeds 2048 bytes. Preview mode stores arbitrary data in a cookie, and browsers drop cookies over ~4KB, so Next.js enforces a 2KB ceiling on the JWT payload to stay safely under that limit.","triggerScenarios":"Calling res.setPreviewData(largeObject) where JSON.stringify(data) plus JWT signing and encryption produces a payload > 2048 bytes. Common when storing large user objects, full API responses, or arrays in preview data.","commonSituations":"Storing an entire user profile or cart object in preview data; migrating from a session-based preview that held more data; draft-mode tooling that stuffs editor state into the preview cookie.","solutions":["Reduce the data stored in setPreviewData to a small identifier (e.g. a record ID) and fetch the full data server-side.","Move large preview state to a database/KV keyed by a short token stored in the cookie.","Avoid storing nested objects or arrays; store only scalars needed for preview branching."],"exampleFix":"// before\nres.setPreviewData({ user: fullUserObject, cart: hugeCartArray })\n// after - store an id, fetch the rest\nres.setPreviewData({ draftId: '123' })","handlingStrategy":"validation","validationCode":"const PREVIEW_LIMIT = 1800 // leave headroom under 2048 for JWT overhead\nfunction validatePreviewData(data: unknown) {\n  const len = JSON.stringify(data).length\n  if (len > PREVIEW_LIMIT) throw new Error(`Preview data ${len}B exceeds safe limit; store an id instead`)\n}","typeGuard":null,"tryCatchPattern":"try {\n  res.setPreviewData(data)\n} catch (err) {\n  if (err.message.includes('limited to 2KB')) {\n    res.setPreviewData({ refId: data.id }) // store a reference\n  }\n}","preventionTips":["Store only small identifiers in preview data, not full objects.","Keep large state in a database/KV keyed by a short token.","Measure JSON.stringify(data).length before calling setPreviewData."],"tags":["preview-mode","api-routes","cookies","runtime"],"analyzedSha":"0ae8c72462952df163f1b1e0726641bc5b40dc93","analyzedAt":"2026-08-06T19:44:29.143Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}