Skip to content
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

fix(vue): Set initial value of clientCtx to undefined #5324

Merged
merged 3 commits into from
Mar 12, 2025

Conversation

Dorilama
Copy link
Contributor

@Dorilama Dorilama commented Mar 11, 2025

Description

Fixes UsesessionList bug: when clientCtx is defined clerk can be null and the composable throws when trying to access clerk.value.setActive.
The error can be reproduced in this project on stackblitz
Fixed by adding a condition to return the default values also when clerk.value is null.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented Mar 11, 2025

🦋 Changeset detected

Latest commit: 7517f9c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@clerk/vue Patch
@clerk/nuxt Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Mar 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 11, 2025 8:22pm

'@clerk/vue': patch
---

fix UsesessionList bug. When `clientCtx` is defined `clerk` can be `null` and throw when trying to access `clerk.value.setActive`. Added a condition to return the default values also when `clerk.value` is `null`.
Copy link
Member

Choose a reason for hiding this comment

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

@wobsoriano Clerk being null while clientCtx is defined seems like an invalid state. I'd expect the existing code to work as expected, which makes me that there is another underlying cause for this issue.

Copy link
Member

Choose a reason for hiding this comment

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

FYI the issue was client resource was initially set to an empty object. See my comment here.

This one now looks good!

@wobsoriano
Copy link
Member

wobsoriano commented Mar 11, 2025

Hi @Dorilama! Thanks for the PR and reproduction.

As @panteliselef mentioned, having clientCtx defined while clerk is null is indeed an invalid state.

While our React useSessionList() hook has the same logic as the Vue one's, it utilizes an extended Clerk class, specifically the IsomorphicClerk, which allows methods to be called lazily even when clerk is null.

In the Vue implementation, we do not use the IsomorphicClerk, so we should avoid rendering clerk.value!.setActive in useSessionList.ts if clerk.value is null. This PR makes sense, but I'll still need to investigate on why there's a possibility of clientCtx having defined while clerk is null and get back to you shortly!

@@ -35,14 +35,14 @@ export const useSessionList: UseSessionList = () => {
const { clerk, clientCtx } = useClerkContext();

const result = computed<UseSessionListReturn>(() => {
if (!clientCtx.value) {
if (!clientCtx.value || !clerk.value) {
Copy link
Member

@wobsoriano wobsoriano Mar 11, 2025

Choose a reason for hiding this comment

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

Okay, the issue is that we have the clientCtx value initially set to an empty object that's why it passes the condition even if clerk is null.

Would you be able to update the initial value to undefined and assert the type for now? (client: undefined as unknown as ClientResource). Then we can merge the PR!

This should be a safe update as we check the value of clerk in all of our composables except useSessionList().

Thanks!

@wobsoriano wobsoriano changed the title fix(vue): add check for clerk.value in UseSessionList composable fix(vue): add check for clerk.value in useSessionList() composable Mar 11, 2025
Copy link
Member

@wobsoriano wobsoriano left a comment

Choose a reason for hiding this comment

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

Thanks so much and very nice catch! 🚀

@Dorilama
Copy link
Contributor Author

thank you for all the work with the sdk :)

'@clerk/vue': patch
---

updating the initial value of `clientCtx` to `undefined` as requested [here](https://github.com/clerk/javascript/pull/5324#discussion_r1989445357)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
updating the initial value of `clientCtx` to `undefined` as requested [here](https://github.com/clerk/javascript/pull/5324#discussion_r1989445357)
Bug fix: Update the initial value of `clientCtx` to `undefined` to correctly infer that `clerk` is defined.

Comment on lines 1 to 5
---
'@clerk/vue': patch
---

fix UsesessionList bug. When `clientCtx` is defined `clerk` can be `null` and throw when trying to access `clerk.value.setActive`. Added a condition to return the default values also when `clerk.value` is `null`.
Copy link
Member

Choose a reason for hiding this comment

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

@Dorilama Can you please remove this changeset, in favour of the new one ? We should only have one changeset file per package

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, sorry about that 😅

@panteliselef panteliselef changed the title fix(vue): add check for clerk.value in useSessionList() composable fix(vue): Set initial value of clientCtx to undefined Mar 11, 2025
@nikosdouvlis nikosdouvlis merged commit ba2b00c into clerk:main Mar 12, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants