-
Notifications
You must be signed in to change notification settings - Fork 282
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
feat(nextjs): Support rendering ClerkProvider in client components #1840
Changes from all commits
46afdec
00624ac
e42e882
69ceeca
7fe28f7
9ebce0a
83f81fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/nextjs': patch | ||
--- | ||
|
||
Update `<ClerkProvider />` to work in client components within the app router. This allows rendering of the provider in client components, previously the pages router provider was being imported and throwing an error. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
import type { Application } from '../models/application'; | ||
import { appConfigs } from '../presets'; | ||
|
||
test.describe('next build @nextjs', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikosdouvlis Any feedback on the test? 🙏 |
||
test.describe.configure({ mode: 'parallel' }); | ||
let app: Application; | ||
const output = []; | ||
|
||
test.beforeAll(async () => { | ||
app = await appConfigs.next.appRouter | ||
.clone() | ||
.addFile( | ||
'src/app/provider.tsx', | ||
() => `'use client' | ||
import { ClerkProvider } from "@clerk/nextjs" | ||
|
||
export function Provider({ children }: { children: any }) { | ||
return ( | ||
<ClerkProvider> | ||
{children} | ||
</ClerkProvider> | ||
) | ||
}`, | ||
) | ||
.addFile( | ||
'src/app/layout.tsx', | ||
() => `import './globals.css'; | ||
import { Inter } from 'next/font/google'; | ||
import { Provider } from './provider'; | ||
|
||
const inter = Inter({ subsets: ['latin'] }); | ||
|
||
export const metadata = { | ||
title: 'Create Next App', | ||
description: 'Generated by create next app', | ||
}; | ||
|
||
export default function RootLayout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<Provider> | ||
<html lang='en'> | ||
<body className={inter.className}>{children}</body> | ||
</html> | ||
</Provider> | ||
); | ||
} | ||
`, | ||
) | ||
.commit(); | ||
await app.setup(); | ||
await app.withEnv(appConfigs.envs.withEmailCodes); | ||
await app.build(); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await app.teardown(); | ||
}); | ||
|
||
test('When <ClerkProvider /> is used as a client component, builds successfully and does not force dynamic rendering', () => { | ||
const dynamicIndicator = 'λ'; | ||
|
||
/** | ||
* Using /_not-found as it is an internal page that should statically render by default. | ||
* This is a good indicator of whether or not the entire app has been opted-in to dynamic rendering. | ||
*/ | ||
const notFoundPageLine = app.buildOutput.split('\n').find(msg => msg.includes('/_not-found')); | ||
|
||
expect(notFoundPageLine).not.toContain(dynamicIndicator); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ | |
"test:ci": "FORCE_COLOR=1 turbo test --concurrency=${TURBO_CONCURRENCY:-80%}", | ||
"test:integration:base": "DEBUG=1 npx playwright test --config integration/playwright.config.ts", | ||
"test:integration:generic": "E2E_APP_ID=react.vite.* npm run test:integration:base -- --grep @generic", | ||
"test:integration:nextjs": "E2E_APP_ID=next.appRouter.withEmailCodes npm run test:integration:base -- --grep @generic", | ||
"test:integration:nextjs": "E2E_APP_ID=next.appRouter.withEmailCodes npm run test:integration:base -- --grep \"@generic|@nextjs\"", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in the initial RFC, I added a |
||
"test:integration:remix": "echo 'placeholder'", | ||
"test:integration:deployment:nextjs": "DEBUG=1 npx playwright test --config integration/playwright.deployments.config.ts", | ||
"clean": "turbo clean", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/compat/router'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More details from the exported hook here: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/compat/router.ts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a stable export we can rely on being available? Or are we reaching into private APIs here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it is stable and reliable, based on PR: vercel/next.js#42502. |
||
import React from 'react'; | ||
|
||
import { ClientClerkProvider } from '../app-router/client/ClerkProvider'; | ||
import { ClerkProvider as PageClerkProvider } from '../pages/ClerkProvider'; | ||
import { type NextClerkProviderProps } from '../types'; | ||
|
||
/** | ||
* This is a compatibility layer to support a single ClerkProvider component in both the app and pages routers. | ||
*/ | ||
export function ClerkProvider(props: NextClerkProviderProps) { | ||
const router = useRouter(); | ||
|
||
const Provider = router ? PageClerkProvider : ClientClerkProvider; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The solution here makes perfect sense to me :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikosdouvlis possibly, the difficulty is they rely on the two different router hooks, which is why we need this layer. We could abstract the dependency on the router into a prop and use this layer to compute the prop to pass down to single provider. |
||
|
||
return <Provider {...props} />; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { ClerkProvider } from './pages/ClerkProvider'; | ||
export { ClerkProvider } from './client-boundary/ClerkProvider'; | ||
export { SignedIn, SignedOut } from './client-boundary/controlComponents'; |
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.
Captures and makes the build output available by way of
app.buildOutput
. This allows us to assert on the output.