Skip to content

Commit 3f0073b

Browse files
fix(storage): improve setCookie to prevent duplicated cookies (#718)
Search for cookies with the same name and remove the cookie with old value.
1 parent 73fd1fe commit 3f0073b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/core/storage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ export default class Storage {
239239
document.cookie = serializedCookie
240240
} else if (process.server && this.ctx.res) {
241241
// Send Set-Cookie header from server side
242-
const prevCookies = this.ctx.res.getHeader('Set-Cookie')
243-
this.ctx.res.setHeader('Set-Cookie', [].concat(prevCookies, serializedCookie).filter(v => v))
242+
const cookies = this.ctx.res.getHeader('Set-Cookie') || []
243+
cookies.unshift(serializedCookie)
244+
this.ctx.res.setHeader('Set-Cookie', cookies
245+
.filter((v, i, arr) => arr.findIndex(val => val.startsWith(v.substr(0, v.indexOf('=')))) === i))
244246
}
245247

246248
return value

0 commit comments

Comments
 (0)