Skip to content

fix(astro): Use batched stores to avoid race conditions in auth store#4000

Merged
wobsoriano merged 3 commits intomainfrom
rob/eco-162-investigate-why-client-stores-give-different-values
Aug 22, 2024
Merged

fix(astro): Use batched stores to avoid race conditions in auth store#4000
wobsoriano merged 3 commits intomainfrom
rob/eco-162-investigate-why-client-stores-give-different-values

Conversation

@wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Aug 22, 2024

Description

This PR fixes an issue where the $authStore nanostore goes back to its initial values (null/undefined) after clerk-js has been loaded. See what's happening in the screen recording below:

Screen.Recording.2024-08-21.at.12.20.12.PM.mov

This is happening because computed stores react each time any of their dependencies get updated. In our case, the $csrStore and $initialState stores. It resets back to undefined even though we have an initial value because of how the derivedStore function works:

  1. We set the $initialState coming from SSR, we have an $authStore value available.
  2. Next, after clerk-js has been loaded, we set the isLoaded property of $csrStore to true, and derivedState will recalculate and give us the client state and not SSR state, which at this time is still undefined since it didn't reach the listener for resources yet. This is where we get undefined values again.
  3. Next, we are setting each key of the resources (client, user, org, session), and each action will trigger the computed $authStore again, giving us different values.

This is where the batched store comes in. This store will wait until the end of a tick to update itself. This is what we want for derivedState to give a correct output in SSR and CSR:

batched.mov

All other stores should work properly as they depend on the $authStore.

Fixes ECO-162

Checklist

  • npm test runs as expected.
  • npm run 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:

@changeset-bot
Copy link

changeset-bot bot commented Aug 22, 2024

🦋 Changeset detected

Latest commit: e8b3265

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

This PR includes changesets to release 1 package
Name Type
@clerk/astro 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

@wobsoriano wobsoriano changed the title fix(astro): Use batched stores to avoid race conditions in stores fix(astro): Use batched stores to avoid race conditions Aug 22, 2024
@wobsoriano wobsoriano changed the title fix(astro): Use batched stores to avoid race conditions fix(astro): Use batched stores to avoid race conditions in auth store Aug 22, 2024
Comment on lines +19 to +22
client: undefined,
user: undefined,
session: undefined,
organization: undefined,
Copy link
Member Author

Choose a reason for hiding this comment

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

See my comment here. Putting the update here as they're related

"@clerk/types": "4.14.0",
"nanoid": "5.0.7",
"nanostores": "0.10.3",
"nanostores": "0.11.2",
Copy link
Member Author

Choose a reason for hiding this comment

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

Version bump with bug fixes and perf improvements. No breaking change. See changelog

@wobsoriano wobsoriano marked this pull request as ready for review August 22, 2024 02:57
* $authStore.subscribe((auth) => console.log(auth.userId))
*/
export const $authStore = computed([$csrState, $initialState], (state, initialState) => {
export const $authStore = batched([$csrState, $initialState], (state, initialState) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this return a read only store similar to computed ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes readonly too

@wobsoriano wobsoriano merged commit 540b720 into main Aug 22, 2024
@wobsoriano wobsoriano deleted the rob/eco-162-investigate-why-client-stores-give-different-values branch August 22, 2024 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants