-
Notifications
You must be signed in to change notification settings - Fork 298
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 3 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,19 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/compat/router'; | ||
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.
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 comment
The 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 comment
The 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.