Skip to content

Commit e9a635c

Browse files
fix: support presentation mode with Next.js --experimental-https flag (#2594)
1 parent d0076c3 commit e9a635c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

packages/next-sanity/src/draft-mode/define-enable-draft-mode.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import type {SanityClient} from '../client'
1010
*/
1111
export interface DefineEnableDraftModeOptions {
1212
client: SanityClient
13+
/**
14+
* Force secure cookies in development mode.
15+
* Enable this when using Next.js --experimental-https flag.
16+
* This option has no effect in production (cookies are always secure).
17+
* @defaultValue false
18+
*/
19+
secureDevMode?: boolean
1320
}
1421

1522
/**
@@ -59,7 +66,11 @@ export function defineEnableDraftMode(options: DefineEnableDraftModeOptions): En
5966
draftModeStore.enable()
6067
}
6168

62-
const dev = process.env.NODE_ENV !== 'production'
69+
const isProduction = process.env.NODE_ENV === 'production'
70+
71+
// We can't auto-detect HTTPS in dev due to Next.js limitations,
72+
// so we need an explicit option
73+
const isSecure = isProduction || (options.secureDevMode ?? false)
6374

6475
// Override cookie header for draft mode for usage in live-preview
6576
// https://github.com/vercel/next.js/issues/49927
@@ -70,8 +81,8 @@ export function defineEnableDraftMode(options: DefineEnableDraftModeOptions): En
7081
value: cookie?.value,
7182
httpOnly: true,
7283
path: '/',
73-
secure: !dev,
74-
sameSite: dev ? 'lax' : 'none',
84+
secure: isSecure,
85+
sameSite: isSecure ? 'none' : 'lax',
7586
})
7687

7788
if (studioPreviewPerspective) {
@@ -80,8 +91,8 @@ export function defineEnableDraftMode(options: DefineEnableDraftModeOptions): En
8091
value: studioPreviewPerspective,
8192
httpOnly: true,
8293
path: '/',
83-
secure: !dev,
84-
sameSite: dev ? 'lax' : 'none',
94+
secure: isSecure,
95+
sameSite: isSecure ? 'none' : 'lax',
8596
})
8697
}
8798

0 commit comments

Comments
 (0)