fix(nuxt): add cookies on server side fetching useCookie

This commit is contained in:
kyumoon 2023-07-05 02:11:08 +09:00
parent 5d7d4d75af
commit fd5220c6f7

View File

@ -63,6 +63,9 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
} }
} else if (process.server) { } else if (process.server) {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
if (!isEqual(cookie.value, cookies[name])) {
writeServerTempararyCookie(useRequestEvent(nuxtApp), name, cookie.value)
}
const writeFinalCookieValue = () => { const writeFinalCookieValue = () => {
if (!isEqual(cookie.value, cookies[name])) { if (!isEqual(cookie.value, cookies[name])) {
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts) writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts)
@ -99,6 +102,16 @@ function writeClientCookie (name: string, value: any, opts: CookieSerializeOptio
} }
} }
function writeServerTempararyCookie (event: H3Event, name: string, value: any) {
// add cookie to req.headers.cookie ex) cookie is added or updated in plugin
if (event) {
const cookie = event.node.req.headers.cookie || ''
event.node.req.headers.cookie = cookie
? `${cookie}; ${name}=${value}`
: `${name}=${value}`
}
}
function writeServerCookie (event: H3Event, name: string, value: any, opts: CookieSerializeOptions = {}) { function writeServerCookie (event: H3Event, name: string, value: any, opts: CookieSerializeOptions = {}) {
if (event) { if (event) {
// update if value is set // update if value is set