From 5a69f4824440f9a0b5960f580b448ce5aba7e807 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 7 Sep 2022 09:34:16 +0100 Subject: [PATCH] fix(nuxt): don't try to set cookie after redirect (#7288) --- packages/nuxt/src/app/composables/cookie.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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