Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-beds-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/next": patch
---

Fix bug with preview mode not being handled properly. Fixes #932
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@
"turbo-windows-arm64": "^2.0.3"
},
"packageManager": "npm@10.5.0"
}
}
4 changes: 2 additions & 2 deletions packages/next/src/rsc/actions/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { COOKIE_NAME } from '../handlers/previewRouteHandler';
export async function disableDraftMode() {
const headersObject = await headers();
const currentUrl = headersObject.get('x-headstartwp-current-url') ?? '/';
const { disable } = await draftMode();
await disable();
const draft = await draftMode();
await draft.disable();
const cookiesObject = await cookies();
cookiesObject.delete(COOKIE_NAME);
redirect(currentUrl);
Expand Down
7 changes: 4 additions & 3 deletions packages/next/src/rsc/data/queries/__tests__/queryPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { queryPost } from '../queryPost';
import { COOKIE_NAME } from '../../../handlers/previewRouteHandler';

jest.mock('next/headers', () => ({
draftMode: jest.fn(() => ({ isEnabled: false })),
draftMode: jest.fn(() => ({ isEnabled: false, disable: jest.fn() })),
cookies: jest.fn(() => ({
get: jest.fn(),
has: jest.fn(),
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('queryPosts', () => {
// set cookies

// @ts-expect-error
nextHeaders.draftMode.mockReturnValueOnce({ isEnabled: true });
nextHeaders.draftMode.mockReturnValueOnce({ isEnabled: true, disable: jest.fn() });

const previewDataPayload = JSON.stringify({
id: DRAFT_POST_ID,
Expand All @@ -93,7 +93,8 @@ describe('queryPosts', () => {
});

expect(nextHeaders.draftMode).toHaveBeenCalled();
expect(nextHeaders.draftMode).toHaveReturnedWith({ isEnabled: true });
const draftModeResult = nextHeaders.draftMode.mock.results.at(-1)?.value;
expect(draftModeResult?.isEnabled).toBe(true);
expect(nextHeaders.cookies).toHaveBeenCalled();
expect(nextHeaders.cookies().has).toHaveBeenCalledWith(COOKIE_NAME);
expect(nextHeaders.cookies().get).toHaveReturnedWith({
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/rsc/data/queries/queryPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export async function queryPost<
const { config, handleError, ...query } = prepareQuery<P>(nextQuery, _config);

try {
const { isEnabled, disable } = await draftMode();
const draft = await draftMode();
const cookiesObject = await cookies();

if (isEnabled) {
if (draft.isEnabled) {
// the cookie will only exist for the previewed path
if (cookiesObject.has(COOKIE_NAME)) {
const previewData: PreviewData = JSON.parse(
Expand All @@ -50,7 +50,7 @@ export async function queryPost<
}
} else {
// if draft mode was enabled but no preview cookie was found, disable draft mode
await disable();
await draft.disable();
}
}

Expand Down
2 changes: 1 addition & 1 deletion projects/wp-multisite-i18n-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
"clsx": "^1.1.1",
"next": "^15.5.7",
"next": "^15.5.9",
"nprogress": "^0.2.0",
"prop-types": "^15.7.2",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-multisite-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "^15.5.7",
"next": "^15.5.9",
"@headstartwp/core": "^1.6.0",
"@headstartwp/next": "^1.5.1"
},
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-multisite-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
"clsx": "^1.1.1",
"next": "^15.5.7",
"next": "^15.5.9",
"nprogress": "^0.2.0",
"prop-types": "^15.7.2",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "^15.5.7",
"next": "^15.5.9",
"@headstartwp/core": "^1.6.0",
"@headstartwp/next": "^1.5.1",
"@10up/next-redis-cache-provider": "^2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
"clsx": "^1.1.1",
"next": "^15.5.7",
"next": "^15.5.9",
"nprogress": "^0.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-polylang-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "^15.5.7",
"next": "^15.5.9",
"@headstartwp/core": "^1.6.0",
"@headstartwp/next": "^1.5.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test-projects/wp-nextjs-epio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "^15.5.7",
"next": "^15.5.9",
"@headstartwp/core": "^1.6.0",
"@headstartwp/next": "^1.5.1",
"@headstartwp/epio-search": "file:../epio-search"
Expand Down
2 changes: 1 addition & 1 deletion test-projects/wp-nextjs-universal-blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "^15.5.7",
"next": "^15.5.9",
"@headstartwp/component-library": "*",
"@headstartwp/block-primitives": "0.1.0",
"@headstartwp/core": "^1.6.0",
Expand Down
Loading