Skip to content

Conversation

@panteliselef
Copy link
Member

@panteliselef panteliselef commented Jan 23, 2025

Description

When the middleware.ts file is mislocated, Clerk will throw a related error as clerkMiddeware needs to run for the server-side helpers to work.

This PR aims to improve the error by showing actionable steps, as we identified that 1 out of 3 users will misplace the middleware file when using Next for the first time.

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:

@changeset-bot
Copy link

changeset-bot bot commented Jan 23, 2025

🦋 Changeset detected

Latest commit: 47d3f31

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

This PR includes changesets to release 1 package
Name Type
@clerk/nextjs 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 Jan 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ⬜️ Skipped (Inspect) Jan 27, 2025 1:18pm


throw new Error(
`Clerk: auth() and currentUser() are only supported in App Router (/app directory).\nIf you're using /pages, try getAuth() instead.\nOriginal error: ${e}`,
`Clerk: auth(), currentUser() and clerkClient(), are only supported in App Router (/app directory).\nIf you're using /pages, try getAuth() instead.\nOriginal error: ${e}`,
Copy link
Member Author

Choose a reason for hiding this comment

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

Our relatively new clerkClient() also depends on headers()

Copy link
Member

Choose a reason for hiding this comment

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

Good catch

Comment on lines 45 to 66
export const createGetAuthAsync = ({
debugLoggerName,
noAuthStatusMessage,
}: {
debugLoggerName: string;
noAuthStatusMessage: string;
}) =>
createGetAuth({
debugLoggerName: debugLoggerName,
transformer: async ({ request, options, logger }) => {
if (!detectClerkMiddleware(request)) {
// Keep the same behaviour for versions that may have issues with bundling `node:fs`
if (isNextWithUnstableServerActions) {
assertAuthStatus(request, noAuthStatusMessage);
}

const missConfiguredMiddlewareLocation = await import('./keyless-node.js')
.then(m => m.suggestMiddlewareLocation())
.catch(() => undefined);

if (missConfiguredMiddlewareLocation) {
throw new Error(missConfiguredMiddlewareLocation);
Copy link
Member Author

Choose a reason for hiding this comment

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

So the refactor was necessary for using await import() without breaking the synchronous getAuth() for pages router.

From my previous experience with keyless using require() would not prevent issues when accessing node:fs

To remind folks we've hit issues with Next 13 and 14 where node:fs would leak to the browser, when code was accessing the module from a server action, causing the app to break.

});

export const getAuth = createGetAuth({
export const createGetAuthAsync = ({
Copy link
Member

@jacekradko jacekradko Jan 23, 2025

Choose a reason for hiding this comment

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

I don't think it really makes sense to rename methods based on async unless we are going to support both versions sync/async. Do we need both?

Copy link
Member Author

Choose a reason for hiding this comment

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

getAuth is a publicly consumed api for the pages router. making it async will be a breaking change.

Copy link
Member

@jacekradko jacekradko left a comment

Choose a reason for hiding this comment

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

The code looks good. 👍🏼

As a team, I think we should talk about how to go forward with more async things creeping into the SDK and this duality of sync/async will become more and more problematic.

Copy link
Member

@alexcarpenter alexcarpenter left a comment

Choose a reason for hiding this comment

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

Testing this I am seeing server logs that it can't detect clerkMiddleware(), when I do have it in place.

Screenshot 2025-01-24 at 11 14 37 AM

@panteliselef
Copy link
Member Author

@alexcarpenter I cannot reproduce, are you sure the error in your terminal are not from before ?

@alexcarpenter
Copy link
Member

!snapshot

@clerk-cookie
Copy link
Collaborator

Hey @alexcarpenter - the snapshot version command generated the following package versions:

Package Version
@clerk/astro 2.1.16-snapshot.v20250124164546
@clerk/backend 1.23.8-snapshot.v20250124164546
@clerk/chrome-extension 2.2.2-snapshot.v20250124164546
@clerk/clerk-js 5.49.1-snapshot.v20250124164546
@clerk/elements 0.22.16-snapshot.v20250124164546
@clerk/clerk-expo 2.7.0-snapshot.v20250124164546
@clerk/expo-passkeys 0.1.15-snapshot.v20250124164546
@clerk/express 1.3.43-snapshot.v20250124164546
@clerk/fastify 2.1.16-snapshot.v20250124164546
@clerk/localizations 3.10.1-snapshot.v20250124164546
@clerk/nextjs 6.10.3-snapshot.v20250124164546
@clerk/nuxt 1.0.12-snapshot.v20250124164546
@clerk/clerk-react 5.22.7-snapshot.v20250124164546
@clerk/react-router 1.0.2-snapshot.v20250124164546
@clerk/remix 4.4.18-snapshot.v20250124164546
@clerk/shared 2.20.15-snapshot.v20250124164546
@clerk/tanstack-start 0.8.17-snapshot.v20250124164546
@clerk/testing 1.4.16-snapshot.v20250124164546
@clerk/themes 2.2.13-snapshot.v20250124164546
@clerk/types 4.44.1-snapshot.v20250124164546
@clerk/ui 0.3.17-snapshot.v20250124164546
@clerk/vue 1.1.7-snapshot.v20250124164546

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/astro

npm i @clerk/astro@2.1.16-snapshot.v20250124164546 --save-exact

@clerk/backend

npm i @clerk/backend@1.23.8-snapshot.v20250124164546 --save-exact

@clerk/chrome-extension

npm i @clerk/chrome-extension@2.2.2-snapshot.v20250124164546 --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@5.49.1-snapshot.v20250124164546 --save-exact

@clerk/elements

npm i @clerk/elements@0.22.16-snapshot.v20250124164546 --save-exact

@clerk/clerk-expo

npm i @clerk/clerk-expo@2.7.0-snapshot.v20250124164546 --save-exact

@clerk/expo-passkeys

npm i @clerk/expo-passkeys@0.1.15-snapshot.v20250124164546 --save-exact

@clerk/express

npm i @clerk/express@1.3.43-snapshot.v20250124164546 --save-exact

@clerk/fastify

npm i @clerk/fastify@2.1.16-snapshot.v20250124164546 --save-exact

@clerk/localizations

npm i @clerk/localizations@3.10.1-snapshot.v20250124164546 --save-exact

@clerk/nextjs

npm i @clerk/nextjs@6.10.3-snapshot.v20250124164546 --save-exact

@clerk/nuxt

npm i @clerk/nuxt@1.0.12-snapshot.v20250124164546 --save-exact

@clerk/clerk-react

npm i @clerk/clerk-react@5.22.7-snapshot.v20250124164546 --save-exact

@clerk/react-router

npm i @clerk/react-router@1.0.2-snapshot.v20250124164546 --save-exact

@clerk/remix

npm i @clerk/remix@4.4.18-snapshot.v20250124164546 --save-exact

@clerk/shared

npm i @clerk/shared@2.20.15-snapshot.v20250124164546 --save-exact

@clerk/tanstack-start

npm i @clerk/tanstack-start@0.8.17-snapshot.v20250124164546 --save-exact

@clerk/testing

npm i @clerk/testing@1.4.16-snapshot.v20250124164546 --save-exact

@clerk/themes

npm i @clerk/themes@2.2.13-snapshot.v20250124164546 --save-exact

@clerk/types

npm i @clerk/types@4.44.1-snapshot.v20250124164546 --save-exact

@clerk/ui

npm i @clerk/ui@0.3.17-snapshot.v20250124164546 --save-exact

@clerk/vue

npm i @clerk/vue@1.1.7-snapshot.v20250124164546 --save-exact

Copy link
Member

@nikosdouvlis nikosdouvlis left a comment

Choose a reason for hiding this comment

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

@panteliselef good job identifying that so many people hit this (~33% of new installs!!!). This is an easy fix but the impact is huge in this case 👏🏻


throw new Error(
`Clerk: auth() and currentUser() are only supported in App Router (/app directory).\nIf you're using /pages, try getAuth() instead.\nOriginal error: ${e}`,
`Clerk: auth(), currentUser() and clerkClient(), are only supported in App Router (/app directory).\nIf you're using /pages, try getAuth() instead.\nOriginal error: ${e}`,
Copy link
Member

Choose a reason for hiding this comment

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

Good catch

const { existsSync } = safeNodeRuntimeFs();
const path = safeNodeRuntimePath();

const projectWithAppSrc = path.join(process.cwd(), 'src', 'app');
Copy link
Member

Choose a reason for hiding this comment

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

Minor, but I would probably write this one like:

Suggested change
const projectWithAppSrc = path.join(process.cwd(), 'src', 'app');
const projectWithAppSrc = existsSync(path.join(process.cwd(), 'src', 'app'));

simply because projectWithAppSrc sounds like a boolean. Feel free to ignore of course

// Utility type to determine if a type is a Promise
type IsPromise<T> = T extends Promise<any> ? true : false;

const createGetAuth = <
Copy link
Member

Choose a reason for hiding this comment

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

Good job building the conditional types used here.
It looks like the majority of the logic is with each transformer function. Would it be simpler to drop the transformer abstraction altogether, drop createGetAuth and end up with 2 methods:

  • createSyncGetAuth
  • createAsyncGetAuth
    (notice I moved the 'sync'/'async' adjective before the 'getAuth' part, which is a minor detail as the adjective describes the returned function and not the builder.

Duplicating a few lines would probably be worth it if we could remove an abstraction layer. What do you think?

Copy link
Member

@anagstef anagstef left a comment

Choose a reason for hiding this comment

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

Great improvement! 🚀

Co-authored-by: Stefanos Anagnostou <anagstef@users.noreply.github.com>
Co-authored-by: Nikos Douvlis <nikosdouvlis@gmail.com>
@panteliselef panteliselef merged commit 3e932b4 into main Jan 27, 2025
29 checks passed
@panteliselef panteliselef deleted the elef/actls-63-look-at-improving-error-message-when-middlewarets-is-mis branch January 27, 2025 13:29
panteliselef added a commit that referenced this pull request Jan 28, 2025
panteliselef added a commit that referenced this pull request Jan 28, 2025
panteliselef added a commit that referenced this pull request Jan 28, 2025
wobsoriano pushed a commit that referenced this pull request Feb 8, 2025
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.

6 participants