fix(nuxt): don't try to set cookie after redirect (#7288)

This commit is contained in:
Daniel Roe 2022-09-07 09:34:16 +01:00 committed by GitHub
parent f8f5771646
commit 5a69f48244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,9 +9,9 @@ import { useNuxtApp } from '#app'
type _CookieOptions = Omit<CookieSerializeOptions & CookieParseOptions, 'decode' | 'encode'> type _CookieOptions = Omit<CookieSerializeOptions & CookieParseOptions, 'decode' | 'encode'>
export interface CookieOptions<T=any> extends _CookieOptions { export interface CookieOptions<T = any> extends _CookieOptions {
decode?(value: string): T decode?(value: string): T
encode?(value: T): string; encode?(value: T): string
default?: () => T | Ref<T> default?: () => T | Ref<T>
} }
@ -38,8 +38,12 @@ export function useCookie <T = string> (name: string, _opts?: CookieOptions<T>):
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts) writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts)
} }
} }
nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue) const unhook = nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue)
nuxtApp.hooks.hookOnce('app:redirected', writeFinalCookieValue) nuxtApp.hooks.hookOnce('app:redirected', () => {
// don't write cookie subsequently when app:rendered is called
unhook()
return writeFinalCookieValue()
})
} }
return cookie as CookieRef<T> return cookie as CookieRef<T>