headstartwp icon indicating copy to clipboard operation
headstartwp copied to clipboard

Preview mode breaks on navigation: “Cannot read properties of undefined (reading 'disable')” when using query() in Next.js 15

Open o-lie opened this issue 1 month ago • 1 comments

Describe the bug

When entering preview mode from WordPress, the initial preview page loads correctly and previewRouteHandler works as expected. However, if I navigate to any other page without exiting preview mode, the application crashes with: TypeError: Cannot read properties of undefined (reading 'disable')

The error occurs inside Next.js internals (src/server/request/draft-mode.ts) during a query({ params }) call in a single post/page route.

This suggests that draftMode().disable() is being called somewhere without the correct this binding — which was valid in older Next.js versions but breaks in Next.js 15, where draftMode() is now asynchronous.

/api/preview/exit works correctly and exiting preview mode removes the issue. The crash happens only when navigating in a still-active preview session.

Steps to Reproduce

  1. In WordPress, open any post or page.
  2. Click Preview.
  3. The preview opens correctly in the Next.js frontend (banner displayed).
  4. Without clicking “Exit Preview”, navigate to any other route in the Next.js site (e.g. another post, etc.).
  5. The page crashes with the following error: TypeError: Cannot read properties of undefined (reading 'disable')

Screenshots, screen recording, code snippet

No response

Environment information

Device: MacBook Pro

OS: macOS Sonoma

Browser: Chrome 124, Safari 17

WordPress information

No response

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

o-lie avatar Nov 21 '25 15:11 o-lie

Thanks for the bug report. We'll look at it.

nicholasio avatar Nov 21 '25 15:11 nicholasio