-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(nuxt): add refresh option to useCookie (#33811)
#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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
WalkthroughAdds a Possibly related PRs
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Tip ✨ Issue Enrichment is now available for GitHub issues!CodeRabbit can now help you manage issues more effectively:
Disable automatic issue enrichmentTo disable automatic issue enrichment, add the following to your issue_enrichment:
auto_enrich:
enabled: falseThanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this 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:refreshcurrently bypasses thereadonlyguard in the client callbackOn the client,
callbackand the watcher now behave as:const callback = (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' }) }When
refresh: true, the watcher always callscallback(true), which skips theopts.readonlybranch and allows writes even for cookies declared asreadonly. Whilereadonlyis mostly a TypeScript‑level contract, it is still surprising that enablingrefreshchanges the runtime behaviour from “never write” to “may write”.If you want
readonlyto 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 ensuring
readonlycookies are never written from the client.Also applies to: 134-140
92-99: BroadcastChannelrefreshhandling is coherent, but consider the source tab’s behaviourThe new
handleChangesignature:const handleChange = (data: { value?: any, refresh?: boolean }) => { const value = 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 that
refreshCookie(name)on the current tab only posts a message; it does not itself call the localcallbackthat writes the cookie again with a freshmaxAge. That means:
- The tab that calls
refreshCookiedoes not extend the cookie’s expiry by itself.- Only tabs that have a
useCookiewatcher and receive the broadcast will update their local ref to whatever is already indocument.cookie.If the intent is that
refreshCookieshould actively extend the cookie TTL in the caller as well, you may want to either call the same write path locally, or document thatrefreshCookieis 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
📒 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.tspackages/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.tspackages/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:refreshoption and default integrate cleanly into existing APIThe addition of
refresh?: booleantoCookieOptionsand the correspondingrefresh: falsedefault inCookieDefaultslook 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:cookieRefexpiry refactor remains sound withrefreshchangesThe custom
cookieRefthat tracks cookie expiry client‑side still correctly:
- Resets its internal timeout on every
set.- Handles large delays by chunking into
MAX_TIMEOUT_DELAY.- Clears timeouts and watchers on scope dispose.
The new
refreshoption 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 Performance ReportMerging #33814 will improve performances by 16.63%Comparing Summary
Benchmarks breakdown
|
| return internalRef.value | ||
| }, | ||
| set (newValue) { | ||
| const nuxtApp = useNuxtApp() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should call usenxtApp at line 294.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
📒 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 the
refreshoption 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.
There was a problem hiding this 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. Run
markdownlint ./docs --fixto 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: trueis 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
📒 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 new
refresh?: booleanoption follows the established pattern and is properly integrated into theCookieOptions<T>interface.
🔗 Linked issue
resolves #33811
📚 Description
Add a
refreshoption touseCookieto extend the cookie's expiration even when its value is unchanged.Example: