@@ -10,6 +10,13 @@ import type {SanityClient} from '../client'
10
10
*/
11
11
export interface DefineEnableDraftModeOptions {
12
12
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
13
20
}
14
21
15
22
/**
@@ -59,7 +66,11 @@ export function defineEnableDraftMode(options: DefineEnableDraftModeOptions): En
59
66
draftModeStore . enable ( )
60
67
}
61
68
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 )
63
74
64
75
// Override cookie header for draft mode for usage in live-preview
65
76
// https://github.com/vercel/next.js/issues/49927
@@ -70,8 +81,8 @@ export function defineEnableDraftMode(options: DefineEnableDraftModeOptions): En
70
81
value : cookie ?. value ,
71
82
httpOnly : true ,
72
83
path : '/' ,
73
- secure : ! dev ,
74
- sameSite : dev ? 'lax ' : 'none ' ,
84
+ secure : isSecure ,
85
+ sameSite : isSecure ? 'none ' : 'lax ' ,
75
86
} )
76
87
77
88
if ( studioPreviewPerspective ) {
@@ -80,8 +91,8 @@ export function defineEnableDraftMode(options: DefineEnableDraftModeOptions): En
80
91
value : studioPreviewPerspective ,
81
92
httpOnly : true ,
82
93
path : '/' ,
83
- secure : ! dev ,
84
- sameSite : dev ? 'lax ' : 'none ' ,
94
+ secure : isSecure ,
95
+ sameSite : isSecure ? 'none ' : 'lax ' ,
85
96
} )
86
97
}
87
98
0 commit comments