Skip to content

Commit 1d2db4a

Browse files
conico974Nicolas Dorseuil
andauthored
Fix cookie handling for AI assistant server actions (#3509)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent d270b4a commit 1d2db4a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/gitbook/src/middleware.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
normalizeVisitorAuthURL,
2525
} from '@/lib/visitors';
2626
import { serveResizedImage } from '@/routes/image';
27+
import { cookies } from 'next/headers';
2728
import type { SiteURLData } from './lib/context';
2829
export const config = {
2930
matcher: [
@@ -545,9 +546,16 @@ function appendQueryParams(url: URL, from: URLSearchParams) {
545546
/**
546547
* Write the cookies to a response.
547548
*/
548-
function writeResponseCookies<R extends NextResponse>(response: R, cookies: ResponseCookies): R {
549-
cookies.forEach((cookie) => {
550-
response.cookies.set(cookie.name, cookie.value, cookie.options);
549+
async function writeResponseCookies<R extends NextResponse>(
550+
response: R,
551+
cookiesToSet: ResponseCookies
552+
): Promise<R> {
553+
const cookiesFn = await cookies();
554+
cookiesToSet.forEach((cookie) => {
555+
// response.cookies.set(cookie.name, cookie.value, cookie.options);
556+
// For some reason we have to use the cookies function instead of response.cookies.set
557+
// Without it, it breaks the ai assistant server actions (it thinks it is a static route).
558+
cookiesFn.set(cookie.name, cookie.value, cookie.options);
551559
});
552560

553561
return response;

0 commit comments

Comments
 (0)