diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index 4ab82673ac..bbedc9e636 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -9,9 +9,9 @@ import { useNuxtApp } from '#app' type _CookieOptions = Omit -export interface CookieOptions extends _CookieOptions { +export interface CookieOptions extends _CookieOptions { decode?(value: string): T - encode?(value: T): string; + encode?(value: T): string default?: () => T | Ref } @@ -38,8 +38,12 @@ export function useCookie (name: string, _opts?: CookieOptions): writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts) } } - nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue) - nuxtApp.hooks.hookOnce('app:redirected', writeFinalCookieValue) + const unhook = nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue) + nuxtApp.hooks.hookOnce('app:redirected', () => { + // don't write cookie subsequently when app:rendered is called + unhook() + return writeFinalCookieValue() + }) } return cookie as CookieRef