-
Notifications
You must be signed in to change notification settings - Fork 437
fix(nextjs): make @clerk/nextjs ESM-safe for non-Node.js runtimes #7954
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
Open
nikosdouvlis
wants to merge
5
commits into
main
Choose a base branch
from
nk/fix-nextjs-esm-server-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b67aae
fix(nextjs): make server-only guard ESM-safe for vinext/Workers
nikosdouvlis e2a03c4
fix(nextjs): convert remaining CJS patterns to ESM for vinext/Workers
nikosdouvlis f166d40
chore: update changeset description
nikosdouvlis 5774982
test(integration): add vinext e2e tests for Clerk on Vite-based Next.js
nikosdouvlis ff5650a
ci: wire up vinext e2e tests in CI pipeline
nikosdouvlis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/nextjs': patch | ||
| --- | ||
|
|
||
| Make `@clerk/nextjs` ESM-safe for non-Node.js runtimes like Cloudflare Workers (vinext). Replaces `require('server-only')`, `require('node:fs')`, and `require('next/navigation')` with ESM-compatible alternatives. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,6 +304,7 @@ jobs: | |
| "vue", | ||
| "nuxt", | ||
| "react-router", | ||
| "vinext", | ||
| "custom", | ||
| ] | ||
| test-project: ["chrome"] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { applicationConfig } from '../models/applicationConfig'; | ||
| import { templates } from '../templates'; | ||
| import { linkPackage } from './utils'; | ||
|
|
||
| const app = applicationConfig() | ||
| .setName('vinext-app') | ||
| .useTemplate(templates['vinext-app']) | ||
| .setEnvFormatter('public', key => `NEXT_PUBLIC_${key}`) | ||
| .addScript('setup', 'pnpm install') | ||
| .addScript('dev', 'pnpm dev') | ||
| .addScript('build', 'pnpm build') | ||
| .addScript('serve', 'pnpm start') | ||
| .addDependency('@clerk/nextjs', linkPackage('nextjs')) | ||
| .addDependency('@clerk/shared', linkPackage('shared')); | ||
|
|
||
| export const vinext = { app } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| node_modules | ||
| .next | ||
| dist | ||
| .vinext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { auth } from '@clerk/nextjs/server'; | ||
|
|
||
| export async function GET() { | ||
| const authObj = await auth(); | ||
| return new Response( | ||
| JSON.stringify({ | ||
| userId: authObj.userId, | ||
| sessionId: authObj.sessionId, | ||
| }), | ||
| { | ||
| headers: { 'content-type': 'application/json' }, | ||
| }, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 'use client'; | ||
|
|
||
| import { SignInButton, UserButton, useAuth } from '@clerk/nextjs'; | ||
|
|
||
| export function AuthDisplay() { | ||
| const { isSignedIn } = useAuth(); | ||
|
|
||
| if (isSignedIn) { | ||
| return ( | ||
| <div> | ||
| <UserButton /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <SignInButton /> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { ClerkProvider } from '@clerk/nextjs'; | ||
|
|
||
| export default function RootLayout({ children }: { children: React.ReactNode }) { | ||
| return ( | ||
| <ClerkProvider> | ||
| <html lang='en'> | ||
| <body>{children}</body> | ||
| </html> | ||
| </ClerkProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { auth } from '@clerk/nextjs/server'; | ||
| import { AuthDisplay } from './auth-display'; | ||
|
|
||
| export default async function Home() { | ||
| const { userId } = await auth(); | ||
| return ( | ||
| <main> | ||
| <h1>vinext + Clerk</h1> | ||
| <AuthDisplay /> | ||
| <p data-clerk-user-id={userId || ''}>{userId ? `server-user-id: ${userId}` : 'server-signed-out'}</p> | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { auth } from '@clerk/nextjs/server'; | ||
|
|
||
| export default async function Protected() { | ||
| const { userId } = await auth.protect(); | ||
| return ( | ||
| <main> | ||
| <h1>Protected Page</h1> | ||
| <p>User ID: {userId}</p> | ||
| </main> | ||
| ); | ||
| } |
4 changes: 4 additions & 0 deletions
4
integration/templates/vinext-app/app/sign-in/[[...sign-in]]/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { SignIn } from '@clerk/nextjs'; | ||
| export default function Page() { | ||
| return <SignIn />; | ||
| } |
4 changes: 4 additions & 0 deletions
4
integration/templates/vinext-app/app/sign-up/[[...sign-up]]/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { SignUp } from '@clerk/nextjs'; | ||
| export default function Page() { | ||
| return <SignUp />; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { clerkMiddleware } from '@clerk/nextjs/server'; | ||
|
|
||
| export default clerkMiddleware(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "vinext-app", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "vite build", | ||
| "dev": "vite dev", | ||
| "start": "vite preview" | ||
| }, | ||
| "dependencies": { | ||
| "next": "16.1.6", | ||
| "react": "19.2.4", | ||
| "react-dom": "19.2.4" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/vite-plugin": "^1.25.6", | ||
| "@types/node": "^20", | ||
| "@types/react": "^19", | ||
| "@types/react-dom": "^19", | ||
| "react-server-dom-webpack": "^19.2.4", | ||
| "typescript": "^5", | ||
| "vinext": "^0.0.15", | ||
| "vite": "^7.3.1" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2017", | ||
| "lib": ["dom", "dom.iterable", "esnext"], | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "noEmit": true, | ||
| "esModuleInterop": true, | ||
| "module": "esnext", | ||
| "moduleResolution": "bundler", | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "jsx": "react-jsx", | ||
| "incremental": true, | ||
| "paths": { | ||
| "@/*": ["./*"] | ||
| } | ||
| }, | ||
| "include": ["**/*.ts", "**/*.tsx"], | ||
| "exclude": ["node_modules"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import vinext from 'vinext'; | ||
| import { defineConfig } from 'vite'; | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| port: parseInt(process.env.PORT || '5173'), | ||
| }, | ||
| plugins: [vinext()], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| import type { Application } from '../models/application'; | ||
| import { appConfigs } from '../presets'; | ||
| import type { FakeUser } from '../testUtils'; | ||
| import { createTestUtils } from '../testUtils'; | ||
|
|
||
| test.describe('vinext @vinext @auth-state', () => { | ||
| test.describe.configure({ mode: 'serial' }); | ||
|
|
||
| let app: Application; | ||
| let fakeUser: FakeUser; | ||
|
|
||
| test.beforeAll(async () => { | ||
| test.setTimeout(120_000); | ||
| app = await appConfigs.vinext.app.clone().commit(); | ||
| await app.setup(); | ||
| await app.withEnv(appConfigs.envs.withEmailCodes); | ||
| await app.dev(); | ||
|
|
||
| const u = createTestUtils({ app }); | ||
| fakeUser = u.services.users.createFakeUser(); | ||
| await u.services.users.createBapiUser(fakeUser); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await fakeUser.deleteIfExists(); | ||
| await app.teardown(); | ||
| }); | ||
|
|
||
| test('first visit shows signed-out state', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.page.goToAppHome(); | ||
| await u.page.waitForClerkJsLoaded(); | ||
|
|
||
| await u.po.expect.toBeSignedOut(); | ||
| await expect(u.page.getByText('server-signed-out')).toBeVisible(); | ||
| }); | ||
|
|
||
| test('page refresh preserves signed-in state', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.waitForMounted(); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
|
|
||
| await page.reload(); | ||
| await u.page.waitForClerkJsLoaded(); | ||
|
|
||
| await u.po.expect.toBeSignedIn(); | ||
| await expect(u.page.getByText(/server-user-id:/)).toBeVisible(); | ||
| }); | ||
|
|
||
| test('new tab shares auth state', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.waitForMounted(); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
|
|
||
| await u.page.goToAppHome(); | ||
| const mainUserId = await u.page.locator('p[data-clerk-user-id]').getAttribute('data-clerk-user-id'); | ||
| expect(mainUserId).toBeTruthy(); | ||
|
|
||
| await u.tabs.runInNewTab(async m => { | ||
| await m.page.goToAppHome(); | ||
| await m.page.waitForClerkJsLoaded(); | ||
|
|
||
| await m.po.expect.toBeSignedIn(); | ||
|
|
||
| const tabUserId = await m.page.locator('p[data-clerk-user-id]').getAttribute('data-clerk-user-id'); | ||
| expect(tabUserId).toBe(mainUserId); | ||
| }); | ||
| }); | ||
|
|
||
| test('sign out clears auth state', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.waitForMounted(); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
|
|
||
| await u.page.goToAppHome(); | ||
| await u.po.userButton.waitForMounted(); | ||
| await u.po.userButton.toggleTrigger(); | ||
| await u.po.userButton.waitForPopover(); | ||
| await u.po.userButton.triggerSignOut(); | ||
|
|
||
| await u.po.expect.toBeSignedOut(); | ||
|
|
||
| await page.reload(); | ||
| await u.page.waitForClerkJsLoaded(); | ||
|
|
||
| await u.po.expect.toBeSignedOut(); | ||
| await expect(u.page.getByText('server-signed-out')).toBeVisible(); | ||
| }); | ||
|
|
||
| test('server and client auth state match', async ({ page, context }) => { | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.waitForMounted(); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
|
|
||
| await u.page.goToAppHome(); | ||
| await u.page.waitForClerkJsLoaded(); | ||
|
|
||
| const serverUserId = await u.page.locator('p[data-clerk-user-id]').getAttribute('data-clerk-user-id'); | ||
| expect(serverUserId).toBeTruthy(); | ||
|
|
||
| const clientUserId = await page.evaluate(() => window.Clerk?.user?.id); | ||
| expect(clientUserId).toBeTruthy(); | ||
|
|
||
| expect(serverUserId).toBe(clientUserId); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: clerk/javascript
Length of output: 950
Add explicit return types on exported template functions.
Exported functions in these files lack explicit return type annotations:
integration/templates/vinext-app/app/api/me/route.tsline 3:GET()integration/templates/vinext-app/app/sign-in/[[...sign-in]]/page.tsxline 2:Page()integration/templates/vinext-app/app/sign-up/[[...sign-up]]/page.tsxline 2:Page()Per TypeScript guidelines, always define explicit return types for public APIs.
Proposed fixes
📝 Committable suggestion
🤖 Prompt for AI Agents