Skip to content

fix(clerk-js): Fetching custom role in OrganizationSwitcher with cache #2430

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

Merged
merged 7 commits into from
Dec 22, 2023

Conversation

panteliselef
Copy link
Member

Description

Problem

In order to display a custom role we need to fetch them, so far we didn't have a robust way of fetching resources in clerk-js. A while back i had implemented useFetch, today I'm extending it in order to use a cache in order to fetch and share resources across components more efficiently.

Before

When a component need to display the custom roles we had to fetch those each time the component was mounted.

After

We fetch the resource once, other subscribers will not trigger a refetch unless the data is stale (in cache for 2 mins).

Goal

The goal of this PR is to provide a good enough fix for the issue with an efficient solution. This is a bug in v4 as well, and a simple solution is better when backporting is necessary. If we think this solution is limiting, we should consider migrating to swr as its bundlesize is minimal and much more robust.

For tests:

  • We need to clear cache before each test

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:

Packages affected

  • @clerk/backend
  • @clerk/chrome-extension
  • @clerk/clerk-js
  • @clerk/clerk-expo
  • @clerk/fastify
  • gatsby-plugin-clerk
  • @clerk/localizations
  • @clerk/nextjs
  • @clerk/clerk-react
  • @clerk/remix
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/themes
  • @clerk/types
  • build/tooling/chore

In order to do this efficiently and to avoid requesting everytime the switcher opens, a simple cache inside useFetch was implemented.
Copy link

changeset-bot bot commented Dec 21, 2023

🦋 Changeset detected

Latest commit: 67c450c

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

This PR includes changesets to release 3 packages
Name Type
@clerk/clerk-js Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo 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

@panteliselef panteliselef self-assigned this Dec 21, 2023
@panteliselef panteliselef changed the title fix(clerk-js): Fetching custom role in OrganizationSwitcher fix(clerk-js): Fetching custom role in OrganizationSwitcher with cache Dec 21, 2023
@panteliselef panteliselef requested review from a team and tmilewski and removed request for a team and tmilewski December 21, 2023 10:59
@panteliselef panteliselef force-pushed the elef/core-1242-custom-role-missing-label branch from 2724193 to 033fba3 Compare December 21, 2023 11:25
Copy link
Member

@brkalow brkalow left a comment

Choose a reason for hiding this comment

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

We need to tweak the logic around params in useCache to ensure we're dealing with a stable cache key.

const requestStatus = useLoadingStatus({
status: 'loading',
});
const cache = useCache(params);
Copy link
Member

Choose a reason for hiding this comment

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

It appears that params is regularly an object here that is not memoized. If I'm understanding the useCache implementation correctly, we're using the raw params value as a cache key. This means that every re-render could potentially introduce a new entry in the cache. We likely need to stringify the object in a deterministic way (e.g. sorted) to get a consistent cache key (I see we're doing it below).

Additionally, the useCallback usage within useCache is not so useful for the same reason (params changing reference on each call).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for caching this @brkalow, shall i go the stringify approach ?

@panteliselef panteliselef requested a review from brkalow December 22, 2023 20:12
Copy link
Member

@brkalow brkalow left a comment

Choose a reason for hiding this comment

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

💯

@panteliselef panteliselef added this pull request to the merge queue Dec 22, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 22, 2023
@panteliselef panteliselef added this pull request to the merge queue Dec 22, 2023
Merged via the queue into main with commit 9040549 Dec 22, 2023
@panteliselef panteliselef deleted the elef/core-1242-custom-role-missing-label branch December 22, 2023 22:05
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.

3 participants