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

feat(nuxt): addrefresh option to useCookie#33814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
riabinkovihor wants to merge7 commits intonuxt:main
base:main
Choose a base branch
Loading
fromriabinkovihor:feat/cookie-refresh-option

Conversation

@riabinkovihor
Copy link

@riabinkovihorriabinkovihor commentedDec 3, 2025
edited
Loading

🔗 Linked issue

resolves#33811

📚 Description

Add arefresh option touseCookie to extend the cookie's expiration even when its value is unchanged.

Example:

constsession=useCookie('session-id',{maxAge:60*60,refresh:true})// Will now extend expiration each time, even with same valuesession.value=session.value

AlexDordiuk and kecoma1 reacted with thumbs up emoji
@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz CodeflowRun & review this pull request inStackBlitz Codeflow.

@pkg-pr-new
Copy link

pkg-pr-newbot commentedDec 3, 2025
edited
Loading

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@33814

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@33814

nuxt

npm i https://pkg.pr.new/nuxt@33814

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@33814

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@33814

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@33814

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@33814

commit:585c8e1

@coderabbitai
Copy link

coderabbitaibot commentedDec 3, 2025
edited
Loading

Walkthrough

Adds arefresh?: boolean option toCookieOptions<T> and exposes it in the cookie composable. Introduces server-side tracking via acookieServerRef and adds_cookiesChanged?: Record<string, boolean> to the Nuxt app interface to record explicit server writes. Client-side watch callbacks are invoked with therefresh flag so refresh-triggered writes can occur even if the value is unchanged. Server-side logic now skips redundant writes when readonly or when value is unchanged unlessrefresh is requested, and coordinates deduplicated or deferred header emission for SSR responses.

Possibly related PRs

  • nuxt/nuxt PR 31517: Modifies the sameuseCookie/cookie composable logic to accept a force/refresh-like flag and adjusts client- and server-side cookie write/refresh behaviour, touching cookie write and tracking code.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check nameStatusExplanationResolution
Docstring Coverage⚠️ WarningDocstring coverage is 66.67% which is insufficient. The required threshold is 80.00%.You can run@coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check nameStatusExplanation
Description check✅ PassedThe description accurately explains the feature by providing context, rationale, and a practical code example showing how therefresh option enables cookie expiration extension with unchanged values.
Linked Issues check✅ PassedThe implementation fully addresses the linked issue#33811 by introducing arefresh boolean option touseCookie that extends cookie expiration on explicit writes regardless of value changes, with proper server and client-side integration.
Out of Scope Changes check✅ PassedAll changes are scoped to implementing therefresh feature: cookie composable logic, Nuxt app state tracking, and documentation updates directly support the stated objective without extraneous modifications.
Title check✅ PassedThe title 'feat(nuxt): addrefresh option to useCookie' accurately and clearly summarises the main change—adding a new refresh option to the useCookie composable.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for usingCodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment@coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitaicoderabbitaibot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
packages/nuxt/src/app/composables/cookie.ts (2)

82-90:refresh currently bypasses thereadonly guard in the client callback

On the client,callback and the watcher now behave as:

constcallback=(force=false)=>{if(!force){if(opts.readonly||isEqual(cookie.value,cookies[name])){return}}writeClientCookie(...)}if(opts.watch){watch(cookie,()=>{if(watchPaused){return}callback(opts.refresh)},{deep:opts.watch!=='shallow'})}

Whenrefresh: true, the watcher always callscallback(true), which skips theopts.readonly branch and allows writes even for cookies declared asreadonly. Whilereadonly is mostly a TypeScript‑level contract, it is still surprising that enablingrefresh changes the runtime behaviour from “never write” to “may write”.

If you wantreadonly to remain a hard guarantee, you could separate the two concerns:

-    const callback = (force = false) => {-      if (!force) {-        if (opts.readonly || isEqual(cookie.value, cookies[name])) { return }-      }+    const callback = (force = false) => {+      if (opts.readonly) { return }+      if (!force && isEqual(cookie.value, cookies[name])) { return }       writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)       …     }    if (opts.watch) {      watch(cookie, () => {        if (watchPaused) { return }-        callback(opts.refresh)+        callback(opts.refresh)      }, { deep: opts.watch !== 'shallow' })    }

This preserves the “force refresh even when the value is unchanged” behaviour while ensuringreadonly cookies are never written from the client.

Also applies to: 134-140


92-99:BroadcastChannelrefresh handling is coherent, but consider the source tab’s behaviour

The newhandleChange signature:

consthandleChange=(data:{value?:any,refresh?:boolean})=>{constvalue=data.refresh ?readRawCookies(opts)?.[name] :opts.decode(data.value)}

allows tabs that receive{ refresh: true } (viarefreshCookie) to reread the cookie fromdocument.cookie, which is sensible: they do not rely on another tab’s encoded payload and instead use their own view of the cookie jar.

One nuance worth double‑checking is thatrefreshCookie(name) on the current tab only posts a message; it doesnot itself call the localcallback that writes the cookie again with a freshmaxAge. That means:

  • The tab that callsrefreshCookie does not extend the cookie’s expiry by itself.
  • Only tabs that have auseCookie watcher and receive the broadcast will update their local ref to whatever is already indocument.cookie.

If the intent is thatrefreshCookie should actively extend the cookie TTL in the caller as well, you may want to either call the same write path locally, or document thatrefreshCookie is purely a cross‑tab synchronisation helper and relies on some other code path to perform the actual refresh.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between9a62708 andf3df555.

📒 Files selected for processing (2)
  • packages/nuxt/src/app/composables/cookie.ts (6 hunks)
  • packages/nuxt/src/app/nuxt.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Follow standard TypeScript conventions and best practices

Files:

  • packages/nuxt/src/app/composables/cookie.ts
  • packages/nuxt/src/app/nuxt.ts
**/*.{ts,tsx,js,jsx,vue}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.{ts,tsx,js,jsx,vue}: Use clear, descriptive variable and function names
Add comments only to explain complex logic or non-obvious implementations
Keep functions focused and manageable (generally under 50 lines), and extract complex logic into separate domain-specific files
Remove code that is not used or needed
Use error handling patterns consistently

Files:

  • packages/nuxt/src/app/composables/cookie.ts
  • packages/nuxt/src/app/nuxt.ts
🧬 Code graph analysis (1)
packages/nuxt/src/app/composables/cookie.ts (1)
packages/nuxt/src/app/nuxt.ts (1)
  • useNuxtApp (552-565)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, default, manifest-on, json, lts/-1)
  • GitHub Check: code
🔇 Additional comments (2)
packages/nuxt/src/app/composables/cookie.ts (2)

18-26:refresh option and default integrate cleanly into existing API

The addition ofrefresh?: boolean toCookieOptions and the correspondingrefresh: false default inCookieDefaults look consistent and backwards compatible. The comment clearly documents the intent and the option is opt‑in, so existing callers are unaffected.

No changes requested here.

Also applies to: 31-37


231-273:cookieRef expiry refactor remains sound withrefresh changes

The customcookieRef that tracks cookie expiry client‑side still correctly:

  • Resets its internal timeout on everyset.
  • Handles large delays by chunking intoMAX_TIMEOUT_DELAY.
  • Clears timeouts and watchers on scope dispose.

The newrefresh option uses the existing watcher/callback mechanism, so this ref continues to behave as before and does not appear to introduce new edge cases.

No changes requested here.

@codspeed-hq
Copy link

codspeed-hqbot commentedDec 3, 2025
edited
Loading

CodSpeed Performance Report

Merging#33814 willimprove performances by 16.63%

Comparingriabinkovihor:feat/cookie-refresh-option (585c8e1) withmain (2a91221)

Summary

⚡ 1 improvement
✅ 9 untouched

Benchmarks breakdown

BenchmarkBASEHEADChange
writeTypes in the basic-types fixture94.1 ms80.7 ms+16.63%

returninternalRef.value
},
set(newValue){
constnuxtApp=useNuxtApp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think we should callusenxtApp at line 294.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Agreed, I’ve moved useNuxtApp.

Copy link

@coderabbitaicoderabbitaibot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between77d0a54 and1685e51.

📒 Files selected for processing (1)
  • docs/4.api/2.composables/use-cookie.md (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: autofix.ci
docs/4.api/2.composables/use-cookie.md

[error] 64-64: markdownlint MD060: Table column style [Table pipe does not align with heading for style "aligned"]. Command failed: markdownlint ./docs --fix && case-police 'docs/**/*.md' *.md --fix

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: test-fixtures (windows-latest, built, rspack, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (windows-latest, dev, vite, default, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (windows-latest, dev, vite-env-api, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, async, manifest-on, js, lts/-1)
  • GitHub Check: test-fixtures (windows-latest, dev, vite, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite-env-api, default, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, built, webpack, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite-env-api, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, async, manifest-off, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite, default, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, built, vite, default, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite, async, manifest-on, js, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite-env-api, async, manifest-on, json, lts/-1)
  • GitHub Check: test-fixtures (ubuntu-latest, dev, vite-env-api, default, manifest-on, json, lts/-1)
  • GitHub Check: release-pkg-pr-new
  • GitHub Check: test-size
  • GitHub Check: typecheck (ubuntu-latest, bundler)
  • GitHub Check: typecheck (windows-latest, bundler)
  • GitHub Check: code
🔇 Additional comments (1)
docs/4.api/2.composables/use-cookie.md (1)

168-188:Example section is well-structured and demonstrates the feature effectively.

The "Refreshing Cookies" example clearly shows therefresh option in action, with helpful comments explaining that the expiration updates even when the value remains unchanged. This provides good practical guidance for users implementing session-like cookie behaviour.

Copy link

@coderabbitaicoderabbitaibot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
docs/4.api/2.composables/use-cookie.md (1)

64-64:Markdown table alignment issue (duplicate of previous review).

The MD060 markdownlint error regarding pipe alignment on this line was previously flagged. Ensure the table pipes are consistently aligned with the heading row. Runmarkdownlint ./docs --fix to auto-correct the alignment.

🧹 Nitpick comments (1)
docs/4.api/2.composables/use-cookie.md (1)

168-188:Example could better demonstrate the intended use case.

The example shows a single assignment in<script setup>, which doesn't fully illustrate the intended use case of refreshing cookie expiration on repeated activity. Consider enhancing the example to show multiple assignments (e.g., triggered by user interactions) to clarify when and whyrefresh: true is beneficial.

For instance, the example could demonstrate assigning the same value in response to user activity (like button clicks) to extend the session expiration, which is the primary motivation for this feature.

Would you like me to suggest an enhanced version of this example that better demonstrates the activity-based refresh pattern?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between1685e51 and585c8e1.

📒 Files selected for processing (1)
  • docs/4.api/2.composables/use-cookie.md (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: codeql (javascript-typescript)
  • GitHub Check: build
  • GitHub Check: code
🔇 Additional comments (1)
docs/4.api/2.composables/use-cookie.md (1)

39-39:Interface property addition is correct.

The newrefresh?: boolean option follows the established pattern and is properly integrated into theCookieOptions<T> interface.

@danielroedanielroe added this to the4.3 milestoneDec 16, 2025
@OrbisKOrbisK changed the titlefeat(nuxt): addrefresh option to useCookie (#33811)feat(nuxt): addrefresh option to useCookieDec 16, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@huang-julienhuang-julienhuang-julien left review comments

@coderabbitaicoderabbitai[bot]coderabbitai[bot] left review comments

@danielroedanielroeAwaiting requested review from danielroedanielroe is a code owner

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

Addrefresh option to useCookie() to extend cookie expiration

3 participants

@riabinkovihor@huang-julien@danielroe

[8]ページ先頭

©2009-2025 Movatter.jp