Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/nuxtPublic

Commitf3df555

Browse files
committed
feat(nuxt): addrefresh option to useCookie (#33811)
1 parent9a62708 commitf3df555

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

‎packages/nuxt/src/app/composables/cookie.ts‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export interface CookieOptions<T = any> extends _CookieOptions {
2121
default?:()=>T|Ref<T>
2222
watch?:boolean|'shallow'
2323
readonly?:boolean
24+
// refresh cookie expiration even when the value remains unchanged after writing
25+
refresh?:boolean
2426
}
2527

2628
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
@@ -31,6 +33,7 @@ const CookieDefaults = {
3133
watch:true,
3234
decode:val=>destr(decodeURIComponent(val)),
3335
encode:val=>encodeURIComponent(typeofval==='string' ?val :JSON.stringify(val)),
36+
refresh:false,
3437
}satisfiesCookieOptions<any>
3538

3639
// we use globalThis to avoid crashes in web workers
@@ -57,10 +60,10 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
5760
constshouldSetInitialClientCookie=import.meta.client&&(hasExpired||cookies[name]===undefined||cookies[name]===null)
5861
constcookieValue=klona(hasExpired ?undefined :(cookies[name]asany)??opts.default?.())
5962

60-
// use a custom ref to expire the cookie on client side otherwise usebasic ref
63+
// use a custom ref to expire the cookie on client side otherwise usecookieServerRef
6164
constcookie=import.meta.client&&delay&&!hasExpired
6265
?cookieRef<T|undefined>(cookieValue,delay,opts.watch&&opts.watch!=='shallow')
63-
:ref<T|undefined>(cookieValue)
66+
:cookieServerRef<T|undefined>(name,cookieValue)
6467

6568
if(import.meta.dev&&hasExpired){
6669
console.warn(`[nuxt] not setting cookie \`${name}\` as it has already expired.`)
@@ -131,7 +134,7 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
131134
if(opts.watch){
132135
watch(cookie,()=>{
133136
if(watchPaused){return}
134-
callback()
137+
callback(opts.refresh)
135138
},
136139
{deep:opts.watch!=='shallow'})
137140
}
@@ -142,7 +145,16 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
142145
}elseif(import.meta.server){
143146
constnuxtApp=useNuxtApp()
144147
constwriteFinalCookieValue=()=>{
145-
if(opts.readonly||isEqual(cookie.value,cookies[name])){return}
148+
if(
149+
opts.readonly
150+
||(isEqual(cookie.value,cookies[name])&&!opts.refresh)
151+
){return}
152+
153+
nuxtApp._cookiesChanged||={}
154+
if(opts.refresh&&!nuxtApp._cookiesChanged[name]){
155+
return
156+
}
157+
146158
nuxtApp._cookies||={}
147159
if(nameinnuxtApp._cookies){
148160
// do not append a second `set-cookie` header
@@ -259,3 +271,25 @@ function cookieRef<T> (value: T | undefined, delay: number, shouldWatch: boolean
259271
}
260272
})
261273
}
274+
275+
// custom ref that will track explicit cookie writes on the server (used for `refresh` option)
276+
functioncookieServerRef<T>(name:string,value:T|undefined){
277+
constinternalRef=ref(value)
278+
279+
returncustomRef((track,trigger)=>{
280+
return{
281+
get(){
282+
track()
283+
returninternalRef.value
284+
},
285+
set(newValue){
286+
constnuxtApp=useNuxtApp()
287+
nuxtApp._cookiesChanged||={}
288+
nuxtApp._cookiesChanged[name]=true
289+
290+
internalRef.value=newValue
291+
trigger()
292+
},
293+
}
294+
})
295+
}

‎packages/nuxt/src/app/nuxt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ interface _NuxtApp {
111111

112112
/**@internal */
113113
_cookies?:Record<string,unknown>
114+
_cookiesChanged?:Record<string,unknown>
114115
/**
115116
* The id of the Nuxt application.
116117
*@internal */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp