Skip to content

Conversation

wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Sep 18, 2025

Description

During auth transitions (sign-in and sign-out), Vue shows intermediate state changes causing UI flicker and confusing redirects. For example (notice how it goes from /sign-in -> /dashboard -> sign-in -> /dashboard):

Screen.Recording.2025-09-17.at.6.43.00.PM.mov

What's happening above:

  1. User signs in with email/password
  2. Clerk redirects to fallbackRedirectUrl or forceRedirectUrl
  3. Navigation starts immediately
  4. Nuxt route middleware runs and checks auth state via useAuth().userId.value
  5. But Vue is still processing state updates (user -> null -> undefined -> user)
  6. Middleware sees null or undefined and redirects to sign-in
  7. When state finally settles to user, it redirects back to fallbackRedirectUrl or forceRedirectUrl

By adding nextTick(), it's ensuring that navigation happens after the state is fully settled, preventing route middleware from seeing intermediate states.

Fixed demo, from /sign-in -> /dashboard

Screen.Recording.2025-09-17.at.6.44.54.PM.mov

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

  • Bug Fixes

    • Improved routing stability in Nuxt to prevent unwanted redirects during sign-in/sign-out. Navigation timing is now deferred to avoid middleware reacting to intermediate navigation states, resulting in smoother and more predictable auth flows.
  • Chores

    • Added a changeset entry for a patch release of the Nuxt package, documenting the fix and ensuring proper versioning.

Copy link

changeset-bot bot commented Sep 18, 2025

🦋 Changeset detected

Latest commit: 1e4d628

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

This PR includes changesets to release 1 package
Name Type
@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 Sep 18, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
clerk-js-sandbox Ready Ready Preview Comment Sep 18, 2025 1:55am

Copy link
Contributor

coderabbitai bot commented Sep 18, 2025

Walkthrough

Introduces a changeset entry for a patch release and updates the Nuxt runtime plugin to defer router navigation by wrapping navigateTo calls in nextTick for both routerPush and routerReplace.

Changes

Cohort / File(s) Summary
Release notes / changeset
\.changeset/funny-peaches-float.md
Adds patch changeset for @clerk/nuxt documenting a fix where middleware observed intermediate navigation states during auth flows. No code changes.
Nuxt plugin navigation timing
packages/nuxt/src/runtime/plugin.ts
Wraps routerPush and routerReplace in nextTick, adds nextTick import (with ts-expect-error), and changes return values to those of nextTick callbacks instead of direct navigateTo results.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant AuthFlow as Auth Flow
  participant Plugin as Nuxt Plugin
  participant Middleware
  participant Router

  User->>AuthFlow: Trigger sign-in/sign-out
  AuthFlow->>Plugin: routerPush/routerReplace(to)
  note over Plugin: New behavior: schedule navigation on nextTick
  Plugin->>Plugin: nextTick(() => navigateTo(to))
  Plugin-->>AuthFlow: Promise (from nextTick)
  rect rgba(230,245,255,0.5)
    note right of Middleware: Middleware sees stable state (no intermediate navigation)
  end
  Plugin->>Router: navigateTo(to) (on next tick)
  Router->>Middleware: Run route middleware
  Middleware-->>Router: Continue or redirect
  Router-->>User: Final route
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I hop between ticks, a careful byte,
Delaying my jumps till timing’s right.
No halfway paths, no zigzag fright—
Next-tick moon guides my silent flight.
Patch packed, routes tight, carrots bright! 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title is concise, follows conventional commit style, and accurately summarizes the primary change: preventing Nuxt route middleware from observing intermediate navigation states during navigation (the fix implemented with nextTick). It clearly reflects the intent and main impact of the changeset and is appropriate for history scanning by teammates.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch rob/nuxt-fix-transition

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

pkg-pr-new bot commented Sep 18, 2025

Open in StackBlitz

@clerk/agent-toolkit

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

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/dev-cli

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

@clerk/elements

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

@clerk/clerk-expo

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/clerk-react

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

@clerk/react-router

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

@clerk/remix

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/themes

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

@clerk/types

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

@clerk/upgrade

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

@clerk/vue

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

commit: 1e4d628

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: 2

🧹 Nitpick comments (3)
.changeset/funny-peaches-float.md (1)

1-5: Changelog is clear; consider noting the mechanism.

Add a brief note that navigation is deferred with Vue.nextTick() so middleware won’t observe transient auth states. Helps downstream maintainers understand the fix at a glance.

 Fixed an issue where Nuxt route middleware saw intermediate states during navigation, causing unwanted redirects during sign-in/sign-out flows.
+Navigation is now deferred to the next Vue tick so middleware only runs after auth state settles.
packages/nuxt/src/runtime/plugin.ts (2)

7-9: Avoid ts-expect-error; import nextTick from vue.

Relying on #imports here forces a suppression and weakens type safety. Importing from vue works in Nuxt runtime code and removes the need for the directive.

-// @ts-expect-error: Handled by Nuxt.
-import { nextTick } from '#imports';
+import { nextTick } from 'vue';

31-40: Document why the deferral exists.

Add a short JSDoc explaining the auth-state race this mitigates and that SSR is not deferred.

-    routerPush: (to: string): ReturnType<typeof navigateTo> => {
+    /** Defer client navigation by one tick so route middleware runs after auth state settles (prevents flicker/loops).
+     *  On SSR, navigate immediately to preserve redirect semantics.
+     */
+    routerPush: (to: string): ReturnType<typeof navigateTo> => {
@@
-    routerReplace: (to: string): ReturnType<typeof navigateTo> => {
+    /** Same as routerPush but with { replace: true }. */
+    routerReplace: (to: string): ReturnType<typeof navigateTo> => {
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 8d1514a and 1e4d628.

📒 Files selected for processing (2)
  • .changeset/funny-peaches-float.md (1 hunks)
  • packages/nuxt/src/runtime/plugin.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
.changeset/**

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Automated releases must use Changesets.

Files:

  • .changeset/funny-peaches-float.md
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels

Files:

  • packages/nuxt/src/runtime/plugin.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use Prettier for consistent code formatting

Files:

  • packages/nuxt/src/runtime/plugin.ts
packages/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

TypeScript is required for all packages

Files:

  • packages/nuxt/src/runtime/plugin.ts
packages/**/*.{ts,tsx,d.ts}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Packages should export TypeScript types alongside runtime code

Files:

  • packages/nuxt/src/runtime/plugin.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use proper TypeScript error types

**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Prefer readonly for properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Use const assertions for literal types: as const
Use satisfies operator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports: import type { ... } from ...
No any types without justification
Proper error handling with typed errors
Consistent use of readonly for immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)

Files:

  • packages/nuxt/src/runtime/plugin.ts
**/*.{js,ts,tsx,jsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.

Files:

  • packages/nuxt/src/runtime/plugin.ts
⏰ 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). (25)
  • GitHub Check: Integration Tests (react-router, chrome)
  • GitHub Check: Integration Tests (custom, chrome)
  • GitHub Check: Integration Tests (nuxt, chrome)
  • GitHub Check: Integration Tests (tanstack-react-start, chrome)
  • GitHub Check: Integration Tests (billing, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 14)
  • GitHub Check: Integration Tests (machine, chrome)
  • GitHub Check: Integration Tests (nextjs, chrome, 15)
  • GitHub Check: Integration Tests (tanstack-react-router, chrome)
  • GitHub Check: Integration Tests (expo-web, chrome)
  • GitHub Check: Integration Tests (quickstart, chrome)
  • GitHub Check: Integration Tests (vue, chrome)
  • GitHub Check: Integration Tests (generic, chrome)
  • GitHub Check: Integration Tests (express, chrome)
  • GitHub Check: Integration Tests (sessions, chrome)
  • GitHub Check: Integration Tests (elements, chrome)
  • GitHub Check: Integration Tests (localhost, chrome)
  • GitHub Check: Integration Tests (astro, chrome)
  • GitHub Check: Integration Tests (ap-flows, chrome)
  • GitHub Check: Unit Tests (18, --filter=@clerk/astro --filter=@clerk/backend --filter=@clerk/express --filter=@c...
  • GitHub Check: Publish with pkg-pr-new
  • GitHub Check: Unit Tests (22, **)
  • GitHub Check: Static analysis
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (1)
packages/nuxt/src/runtime/plugin.ts (1)

31-40: No awaiting callers found — change is safe

Repo search shows occurrences only in packages/nuxt/src/runtime/plugin.ts (this change), packages/react/src/contexts/tests/ClerkProvider.test.tsx (mocked as () => {}), and packages/astro/src/integration/create-integration.ts (passes navigate); no await routerPush/await routerReplace usages or @clerk/vue callers found.

@wobsoriano wobsoriano merged commit e2dd40a into main Sep 18, 2025
42 checks passed
@wobsoriano wobsoriano deleted the rob/nuxt-fix-transition branch September 18, 2025 17:16
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