Skip to Content
Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev

Upgrading @clerk/nextjs to Core 2

Core 2 is included in the Next.js SDK starting with version 5.0.0. This new version ships with an improved design and UX for its built-in components, no "flash of white page" when authenticating, a substantially improved middleware import, and a variety of smaller DX improvements and housekeeping items. Each of the potentially breaking changes are detailed in this guide, below.

By the end of this guide, you’ll have successfully upgraded your Next.js project to use @clerk/nextjs v5. You’ll learn how to update your dependencies, resolve breaking changes, and find deprecations. Step-by-step instructions will lead you through the process.

Preparing to upgrade

Before uprading, it's highly recommended that you update your Clerk SDKs to the latest Core 1 version (npm i @clerk/nextjs@4). Some changes required for Core 2 SDKs can be applied incrementally to the v5 release, which should contribute to a smoother upgrading experience. After updating, look out for deprecation messages in your terminal and browser console. By resolving these deprecations you'll be able to skip many breaking changes from Core 2.

Additionally, some of the minumum version requirements for some base dependencies have been updated such that versions that are no longer supported or are at end-of-life are no longer guaranteed to work correctly with Clerk.

Updating Node.js

You need to have Node.js 18.17.0 or later installed. Last year, Node.js 16 entered EOL (End of life) status, so support for this version has been removed across Clerk SDKs. You can check your Node.js version by running node -v in your terminal. Learn more about how to update and install Node.js(opens in a new tab).

Updating React

All react-dependent Clerk SDKs now require you to use React 18 or higher. You can update your project by installing the latest version of react and react-dom.

terminal
npm install react@latest react-dom@latest
terminal
yarn add react@latest react-dom@latest
terminal
pnpm add react@latest react-dom@latest

If you are upgrading from React 17 or lower, make sure to learn about how to upgrade your React version to 18(opens in a new tab) as well.

Updating Next.js

@clerk/nextjs now requires you to use Next.js version 13.0.4 or later. Check out Next's upgrade guides for more guidance if you have not yet upgraded to Next.js 13:

Updating to Core 2

Whenever you feel ready, go ahead and install the latest version of any Clerk SDKs you are using. Make sure that you are prepared to patch some breaking changes before your app will work properly, however. The commands below demonstrate how to install the latest version.

terminal
npm install @clerk/nextjs
terminal
yarn add @clerk/nextjs
terminal
pnpm add @clerk/nextjs

CLI upgrade helper

Clerk now provides a @clerk/upgrade CLI tool that you can use to ease the upgrade process. The tool will scan your codebase and produce a list of changes you'll need to apply to your project. It should catch the vast majority of the changes needed for a successful upgrade to any SDK including Core 2. This can save you a lot of time reading through changes that don't apply to your project.

To run the CLI tool, navigate to your project and run it in the terminal:

terminal
npx @clerk/upgrade
terminal
yarn dlx @clerk/upgrade
terminal
pnpm dlx @clerk/upgrade

If you are having trouble with npx, it's also possible to install directly with npm i @clerk/upgrade -g, and can then be run with the clerk-upgrade command.

Breaking Changes

Component design adjustments

The new version ships with improved design and UX across all of Clerk's UI components. If you have used the appearance prop or tokens for a custom theme, you will likely need to make some adjustments to ensure your styling is still looking great. If you're using the localization prop you will likely need to make adjustments to account for added or removed localization keys.

More detail on these changes »

New Middleware architecture

User and customer feedback about authMiddleware() has been clear in that Middleware logic was a often friction point. As such, in v5 you will find a completely new Middleware helper called clerkMiddleware() that should alleviate the issues folks had with authMiddleware().

The primary change from the previous authMiddleware() is that clerkMiddleware() does not protect any routes by default, instead requiring the developer to add routes they would like to be protected by auth. This is a substantial contrast to the previous authMiddleware(), which protected all routes by default, requiring the developer to add exceptions. The API was also substantially simplified, and it has become easier to combine with other Middleware helpers smoothly as well.

Here's an example that demonstrates route protection based on both authentication and authorization:

middleware.ts
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'; const isDashboardRoute = createRouteMatcher(['/dashboard(.*)']); const isAdminRoute = createRouteMatcher(['/admin(.*)']); export default clerkMiddleware((auth, req) => { // Restrict admin route to users with specific role if (isAdminRoute(req)) auth().protect({ role: 'org:admin' }); // Restrict dashboard routes to logged in users if (isDashboardRoute(req)) auth().protect(); }); export const config = { matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'], };

A couple things to note here:

  • The createRouteMatcher helper makes it easy to define route groups that you can leverage inside the Middleware function and check in whichever order you'd like. Note that it can take an array of routes as well.
  • With clerkMiddleware, you're defining the routes you want to be protected, rather than the routes you don't want to be protected.
  • The auth().protect()(opens in a new tab) helper is utilized heavily here, check out its docs for more info.

See the clerkMiddleware() docs for more information and detailed usage examples.

Migrating to clerkMiddleware()

Clerk strongly recommends migrating to the new clerkMiddleware() for an improved DX and access to all present and upcoming features, but also want to note that authMiddleware(), while deprecated, will continue to work in v5 and will not be removed until the next major version, so you do not need to make any changes to your Middleware setup this version.

The most basic migration will be updating the import and changing out the default export, then mirroring the previous behavior of protecting all routes as such:

- import { authMiddleware } from "@clerk/nextjs/server" + import { clerkMiddleware } from '@clerk/nextjs/server' - export default authMiddleware() + export default clerkMiddleware((auth) => auth().protect()) export const config = { matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"], }

Of course, in most cases you'll have a more complicated setup than this. You can find some examples below for how to migrate a few common use cases. Be sure to review the clerkMiddleware() documentation if your specific use case is not mentioned.

Changes to top-level exports

As part of this release, some of the top-level exports of @clerk/nextjs have been changed in order to improve bundle size and tree-shaking efficiency. These changes have resulted in a ~75% reduction in build size for middleware bundles. However, you will likely need to make some changes to import paths as a result.

Use the CLI tool to automatically find occurences of imports that need to be changed.

After sign up/in/out default value change

Defining redirect URLs for after sign up, in, and/or out via the Clerk dashboard has been removed in Core 2. In your Clerk dashboard, under "paths", there is a section called "Component paths", where URLs could be defined that had a deprecation warning. In Core 2, this functionality has been removed, and specifying redirect paths via the dashboard will no longer work. If you need to pass a redirect URL for after sign in/up/out, there are a few different ways this can be done(opens in a new tab), from environment variables to middleware to supplying them directly to the relevant components.

As part of this change, the default URL for each of these props has been set to /, so if you are passing / explicitly to any one of the above props, that line is no longer necessary and can be removed.

- <UserButton afterSignOutUrl='/' /> + <UserButton />

afterSignXUrl changes

Some changes are being made to the way that "after sign up/in url"s and redirect url props are handled as part of this new version, in order to make behavior more clear and predictable.

We will refer to these urls as afterSignXUrl where X could be Up or In depending on the context.

Previously, setting afterSignInUrl or afterSignOutUrl would only actually redirect some of the time. If the user clicks on any form of link that takes them to a sign up/in page, Clerk automatically sets redirect_url in the querystring such that after the sign up or in, the user is returned back to the page they were on before. This is a common pattern for sign up/in flows, as it leads to the least interruption of the user's navigation through your app. When a redirect_url is present, any value passed to afterSignInUrl or afterSignUpUrl is ignored. However, if the user navigates directly to a sign up/in page, there’s no redirect url in the querystring and in this case the afterSignInUrl or afterSignOutUrl would take effect. This behavior was not intuitive and didn't give a way to force a redirect after sign up/in, so the behavior is changing to address both of these issues.

All afterSignXUrl props and CLERK_AFTER_SIGN_X_URL environment variables have been removed, and should be replaced by one of the following options:

  • signXForceRedirectUrl / CLERK_SIGN_X_FORCE_REDIRECT_URL - if set, Clerk will always redirect to provided URL, regardless of what page the user was on before. Use this option with caution, as it will interrupt the user’s flow by taking them away from the page they were on before.
  • signXFallbackRedirectUrl / CLERK_SIGN_UP_FALLBACK_REDIRECT_URL - if set, this will mirror the previous behavior, only redirecting to the provided URL if there is no redirect_url in the querystring.

If neither value is set, Clerk will redirect to the redirect_url if present, otherwise it will redirect to /. If you'd like to retain the current behavior of your app without any changes, you can switch afterSignXUrl with signXFallbackRedirectUrl as such:

- <SignIn afterSignInUrl='/foo' /> + <SignIn signInFallbackRedirectUrl='/foo' />

Removed: orgs claim on JWT

In the previous version of Clerk's SDKs, if you decode the session token that Clerk returns from the server, you'll currently find an orgs claim on it. It lists all the orgs associated with the given user. Now, Clerk returns the org_id, org_slug, and org_role of the active organization.

The orgs claim was part of the JwtPayload. Here are a few examples of where the JwtPayload could be found.

If you would like to have your JWT return all of the user's organizations, you can create a custom JWT template in your dashboard. Add { "orgs": "user.organizations" } to it.

Path routing is now the default

On components like <SignIn /> you can define the props routing and path. routing can be set to 'hash' | 'path' | 'virtual' and describes the routing strategy that should be used. path defines where the component is mounted when routing="path" is used.

In the latest version, the default routing strategy has become 'path'. Unless you change the routing prop, you'll need to define the path prop. The affected components are:

  • <SignIn />
  • <SignUp />
  • <UserProfile />
  • <CreateOrganization />
  • <OrganizationProfile />

Here's how you'd use the components going forward:

<SignIn path="/sign-in" /> <SignUp path="/sign-up" /> <UserProfile path="/user-profile" /> <CreateOrganization path="/create-org" /> <OrganizationProfile path="/org-profile" />

If you don't define the path prop an error will be thrown. Of course, you can still use routing="hash" or routing="virtual".

<UserProfile routing="hash" /> <OrganizationProfile routing="virtual" />

For the @clerk/nextjs SDK, you can set environment variables for the sign up/in URLs and avoid needing to explicitly pass the path to the <SignIn /> and <SignUp /> components.

.env.local
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up

If you have defined both environment variables, you're able to use the <SignIn /> and <SignUp /> components without any props, as such:

<SignIn /> <SignUp />

Image URL Name Consolidation

There are a number of Clerk primitives that contain images, and previously they each had different property names, like avatarUrl, logoUrl, profileImageUrl, etc. In order to promote consistency and make it simpler for developers to know where to find associated images, all image properties are now named imageUrl. See the list below for all affected classes:

Deprecation removals & housekeeping

As part of this major version, a number of previously deprecated props, arugments, methods, etc. have been removed. Additionally there have been some changes to things that are only used internally, or only used very rarely. It's highly unlikely that any given app will encounter any of these items, but they are all breaking changes, so they have all been documented below.

For this section more than any other one, please use the CLI upgrade tool (npx @clerk/upgrade). Changes in this section are very unlikely to appear in your codebase, the tool will save time looking for them.

Deprecation removals

Other Breaking changes

What did you think of this content?

Clerk © 2024