Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): better equality check for json cookies #6352

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function useCookie <T=string> (name: string, _opts?: CookieOptions<T>): C
} else if (process.server) {
const nuxtApp = useNuxtApp()
const writeFinalCookieValue = () => {
if (cookie.value !== cookies[name]) {
if (!isEqualValue(cookie.value, cookies[name])) {
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts)
}
}
Expand Down Expand Up @@ -72,3 +72,12 @@ function writeServerCookie (event: CompatibilityEvent, name: string, value: any,
appendHeader(event, 'Set-Cookie', serializeCookie(name, value, opts))
}
}

function isEqualValue (val1: unknown, val2: unknown) {
try {
if (typeof val1 === 'object' && typeof val2 === 'object') {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return JSON.stringify(val1) === JSON.stringify(val2)
}
} catch {}
return val1 === val2
}