Skip to content

Conversation

@jacekradko
Copy link
Member

@jacekradko jacekradko commented Jun 16, 2025

Description

Fixes: USER-2179

Reworked the cache key creation logic not to rely on sign_in.id, which can change after re-renders

  1. Initial state: signIn.id = "sia_...."
  2. After setActive() signIn.id changes to undefined
  3. Cache key changes: From {..., "id": "sia_...."} to {..., "id": undefined}
  4. Re-render in the host app
  5. useFetch re-triggers because the cache key changed
  6. SignIn object is invalid so the duplicate call fails

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:

Summary by CodeRabbit

Summary by CodeRabbit

  • Tests

    • Added a comprehensive test suite for the sign-in code form, covering cache key logic, API call behavior, and form rendering for different verification strategies.
  • Bug Fixes

    • Improved cache key generation for sign-in factor preparation, reducing redundant API calls and ensuring correct handling of communication channels.
    • Stabilized cache key by removing dependency on mutable sign-in ID to prevent cache inconsistencies.

@changeset-bot
Copy link

changeset-bot bot commented Jun 16, 2025

🦋 Changeset detected

Latest commit: 59828e8

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

@vercel
Copy link

vercel bot commented Jun 16, 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 Jun 16, 2025 10:39pm

@jacekradko jacekradko force-pushed the fix/remove-duplicate-prepare-first-factor-call branch from 3e2b253 to 3f91967 Compare June 16, 2025 17:06
@jacekradko jacekradko marked this pull request as ready for review June 16, 2025 17:08
@jacekradko jacekradko requested review from a team, aeliox and wobsoriano as code owners June 16, 2025 17:08
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

📝 Walkthrough

Walkthrough

The changes introduce a memoized cacheKey object in the SignInFactorOneCodeForm component, which is used as the query key for the useFetch hook that triggers the sign-in first factor preparation. The cacheKey is now based on the factor's strategy and relevant identifiers, excluding the signIn.id, to prevent unnecessary re-fetches. A comprehensive test suite for the component is also added, focusing on the new cache key logic, API call behavior, and component rendering under different scenarios.

Assessment against linked issues

Objective Addressed Explanation
Prevent /prepare_first_factor from firing twice by stabilizing the query key (USER-2179)
Remove signIn.id from the query key to avoid redundant API calls (USER-2179)
Ensure cache key includes relevant factor identifiers and communication channel (USER-2179)
Add/adjust tests to verify cache key logic and API call behavior (USER-2179)

Suggested reviewers

  • NicolasLopes7

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

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot 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

🧹 Nitpick comments (3)
packages/clerk-js/src/ui/components/SignIn/__tests__/SignInFactorOneCodeForm.spec.tsx (3)

12-18: Module mock is never restored – may leak into unrelated test files

vi.mock('../../../hooks', …) is declared at top scope, so the mocked useFetch persists for the entire test suite (including other files executed later). Wrap the mock in vi.mock + vi.importActual inside the describe or add an afterAll that calls vi.unmock to avoid cross-test pollution.


26-29: options parameter typed as any – lose IDE help & safety

Give renderWithProviders a proper generic signature instead of falling back to any, e.g.:

-const renderWithProviders = (component: React.ReactElement, options?: any) => {
+const renderWithProviders = <T extends object = unknown>(
+  component: React.ReactElement,
+  options?: T,
+) => {

Small, but it preserves type checking for future contributors.


100-166: Duplicated test scenario – consider merging to keep suite lean

The assertion “still calls prepare when factor is already prepared but verification not verified” appears in both the Cache Key Generation and shouldAvoidPrepare Logic describe blocks. Combining them in a single place will shorten execution time and avoid maintenance divergence.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 656851d and 3f91967.

📒 Files selected for processing (2)
  • packages/clerk-js/src/ui/components/SignIn/SignInFactorOneCodeForm.tsx (3 hunks)
  • packages/clerk-js/src/ui/components/SignIn/__tests__/SignInFactorOneCodeForm.spec.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript-typescript)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 16, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@6134

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@6134

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@6134

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@6134

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@6134

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@6134

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@6134

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@6134

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@6134

@clerk/express

npm i https://pkg.pr.new/@clerk/express@6134

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@6134

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@6134

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@6134

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@6134

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@6134

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@6134

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@6134

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@6134

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@6134

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@6134

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@6134

@clerk/types

npm i https://pkg.pr.new/@clerk/types@6134

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@6134

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@6134

commit: 59828e8

Copy link
Contributor

@coderabbitai coderabbitai bot 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

🧹 Nitpick comments (1)
.changeset/real-bees-post.md (1)

5-5: Enhance changeset description with context and references.

The summary is clear, but adding the issue ID (USER-2179) and explicitly calling out the removal of duplicate prepare-first-factor calls will improve traceability. For example:

“Remove duplicate prepare-first-factor calls (USER-2179) by stabilizing cache‐key generation in SignInFactorOneCodeForm.tsx—no longer relying on signIn.id.”

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 05dda3d and 59828e8.

📒 Files selected for processing (1)
  • .changeset/real-bees-post.md (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: Formatting | Dedupe | Changeset
  • GitHub Check: Build Packages
  • GitHub Check: semgrep/ci
  • GitHub Check: Analyze (javascript-typescript)

@jacekradko jacekradko merged commit e436882 into main Jun 17, 2025
63 of 64 checks passed
@jacekradko jacekradko deleted the fix/remove-duplicate-prepare-first-factor-call branch June 17, 2025 00:47
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.

4 participants