Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt): don't try to set cookie after redirect (#7288)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 7, 2022
1 parent f8f5771 commit 5a69f48
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { useNuxtApp } from '#app'

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
encode?(value: T): string;
encode?(value: T): string
default?: () => T | Ref<T>
}

Expand All @@ -38,8 +38,12 @@ export function useCookie <T = string> (name: string, _opts?: CookieOptions<T>):
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<T>
Expand Down

0 comments on commit 5a69f48

Please sign in to comment.