Skip to content

cacheInterceptor does not respect Next.js draft mode (preview mode) #919

@horai93

Description

@horai93

The cacheInterceptor in opennextjs-aws does not check for Next.js draft mode cookies
(__prerender_bypass and __next_preview_data), causing cached responses to be served even when
preview mode is active. This breaks the Next.js preview functionality for ISR pages.

Steps to Reproduce

  1. Create an ISR page with export const revalidate = 300
  2. Enable draft mode using draftMode().enable()
  3. Visit the page with preview mode active
  4. The page shows cached content instead of the latest preview content

Expected Behavior

When Next.js draft mode is active (indicated by __prerender_bypass or __next_preview_data cookies),
the cacheInterceptor should bypass the cache and allow the request to proceed to the Next.js
handler.

Actual Behavior

The cacheInterceptor serves cached content regardless of draft mode status, making preview
functionality unusable for ISR pages.

Proposed Solution

Add a check for Next.js preview mode cookies in the cacheInterceptor:

export async function cacheInterceptor(event) {
  if (Boolean(event.headers["next-action"]) ||
      Boolean(event.headers["x-prerender-revalidate"]))
      return event;

  // Check for Next.js preview mode cookies
  const cookies = event.headers["cookie"] || ""
  const hasPreviewData = cookies.includes("__prerender_bypass") ||
cookies.includes("__next_preview_data");

  if (hasPreviewData) {
      debug("Preview mode detected, passing through to handler");
      return event;
  }

  // ... rest of the function
}

Environment

  • @opennextjs/aws: 3.6.5
  • Next.js: 15.3.3
  • Deployment: Cloudflare Workers with opennextjs/opennextjs-cloudflare

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions