vercel/next.js · error · Error

Preview data is limited to 2KB currently, reduce how much da

Error message

Preview data is limited to 2KB currently, reduce how much data you are storing as preview data to continue

What it means

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.

Source

Thrown at packages/next/src/server/api-utils/node/api-resolver.ts:206

    {
      data: encryptWithSecret(
        Buffer.from(options.previewModeEncryptionKey),
        JSON.stringify(data)
      ),
    },
    options.previewModeSigningKey,
    {
      algorithm: 'HS256',
      ...(options.maxAge !== undefined
        ? { expiresIn: options.maxAge }
        : undefined),
    }
  )

  // limit preview mode cookie to 2KB since we shouldn't store too much
  // data here and browsers drop cookies over 4KB
  if (payload.length > 2048) {
    throw new Error(
      `Preview data is limited to 2KB currently, reduce how much data you are storing as preview data to continue`
    )
  }

  const { serialize } =
    require('next/dist/compiled/cookie') as typeof import('next/dist/compiled/cookie')
  const previous = res.getHeader('Set-Cookie')
  res.setHeader(`Set-Cookie`, [
    ...(typeof previous === 'string'
      ? [previous]
      : Array.isArray(previous)
        ? previous
        : []),
    serialize(COOKIE_NAME_PRERENDER_BYPASS, options.previewModeId, {
      httpOnly: true,
      sameSite: process.env.NODE_ENV !== 'development' ? 'none' : 'lax',
      secure: process.env.NODE_ENV !== 'development',
      path: '/',

View on GitHub (pinned to 0ae8c72462)

Solutions

  1. Reduce the data stored in setPreviewData to a small identifier (e.g. a record ID) and fetch the full data server-side.
  2. Move large preview state to a database/KV keyed by a short token stored in the cookie.
  3. Avoid storing nested objects or arrays; store only scalars needed for preview branching.

Example fix

// before
res.setPreviewData({ user: fullUserObject, cart: hugeCartArray })
// after - store an id, fetch the rest
res.setPreviewData({ draftId: '123' })
Defensive patterns

Strategy: validation

Validate before calling

const PREVIEW_LIMIT = 1800 // leave headroom under 2048 for JWT overhead
function validatePreviewData(data: unknown) {
  const len = JSON.stringify(data).length
  if (len > PREVIEW_LIMIT) throw new Error(`Preview data ${len}B exceeds safe limit; store an id instead`)
}

Try / catch

try {
  res.setPreviewData(data)
} catch (err) {
  if (err.message.includes('limited to 2KB')) {
    res.setPreviewData({ refId: data.id }) // store a reference
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of vercel/next.js@0ae8c72462 (2026-08-06). Data as JSON: /api/errors/c51c6ef000398775. Report an issue: GitHub.