Skip to content

Commit

Permalink
fix(adapter-nextjs): duplicate response Set-Cookie headers in pages r…
Browse files Browse the repository at this point in the history
…outer
  • Loading branch information
HuiSF committed Aug 28, 2024
1 parent 798c135 commit 2fe22d9
Showing 1 changed file with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,45 @@ const createCookieStorageAdapterFromGetServerSidePropsContext = (
return allCookies;
},
set(name, value, options) {
response.appendHeader(
'Set-Cookie',
`${ensureEncodedForJSCookie(name)}=${value};${
options ? serializeSetCookieOptions(options) : ''
}`,
);
const encodedName = ensureEncodedForJSCookie(name);

const existingSetCookieValues = response.getHeader('Set-Cookie');

// if the cookies have already been set, we don't need to set them again.
if (
Array.isArray(existingSetCookieValues) &&
existingSetCookieValues.findIndex(
cookieValue =>
cookieValue.startsWith(`${encodedName}=`) &&
!cookieValue.startsWith(`${encodedName}=;`),
) > -1
) {
return;
}

const setCookieValue = `${encodedName}=${value};${
options ? serializeSetCookieOptions(options) : ''
}`;

response.appendHeader('Set-Cookie', setCookieValue);
},
delete(name) {
response.appendHeader(
'Set-Cookie',
`${ensureEncodedForJSCookie(
name,
)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`,
);
const encodedName = ensureEncodedForJSCookie(name);
const setCookieValue = `${encodedName}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`;
const existingSetCookieValues = response.getHeader('Set-Cookie');

// if the value for cookie deletion is already in the Set-Cookie header, we
// don't need to add the deletion value again.
if (
(typeof existingSetCookieValues === 'string' &&
existingSetCookieValues === setCookieValue) ||
(Array.isArray(existingSetCookieValues) &&
existingSetCookieValues.includes(setCookieValue))
) {
return;
}

response.appendHeader('Set-Cookie', setCookieValue);
},
};
};
Expand Down

0 comments on commit 2fe22d9

Please sign in to comment.