Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): stringify cookie values before broadcasting them #23449

Merged
merged 8 commits into from
Sep 29, 2023
9 changes: 6 additions & 3 deletions packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Ref } from 'vue'
import type { MaybeRef, Ref } from 'vue'
import { getCurrentInstance, nextTick, onUnmounted, ref, toRaw, watch } from 'vue'
import type { CookieParseOptions, CookieSerializeOptions } from 'cookie-es'
import { parse, serialize } from 'cookie-es'
import { deleteCookie, getCookie, getRequestHeader, setCookie } from 'h3'
import type { H3Event } from 'h3'
import destr from 'destr'
import { isEqual } from 'ohash'
danielroe marked this conversation as resolved.
Show resolved Hide resolved
import { useNuxtApp } from '../nuxt'
import { useRequestEvent } from './ssr'
Expand All @@ -23,7 +22,7 @@
const CookieDefaults: CookieOptions<any> = {
path: '/',
watch: true,
decode: val => destr(decodeURIComponent(val)),

Check failure on line 25 in packages/nuxt/src/app/composables/cookie.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'destr'.
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
}

Expand All @@ -39,7 +38,7 @@

const callback = () => {
writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)
channel?.postMessage(toRaw(cookie.value))
channel?.postMessage(prepareStructuredClone(cookie.value))
}

let watchPaused = false
Expand Down Expand Up @@ -114,3 +113,7 @@
// else ignore if cookie doesn't exist in browser and value is null/undefined
}
}

function prepareStructuredClone (value: MaybeRef<any>): any {
return JSON.parse(JSON.stringify(toRaw(value)))
}
danielroe marked this conversation as resolved.
Show resolved Hide resolved