-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextjs): Support rendering ClerkProvider in client components (#…
…1840) * feat(nextjs): Support rendering ClerkProvider in client components * chore(nextjs): Remove unintended change * chore(repo): Add changeset * chore(repo): Add tests for next build * chore(repo): Add buildOutput property on Application instead of accepting a custom log
- Loading branch information
Showing
8 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 | ||
--- | ||
|
||
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. |
This file contains 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 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 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,72 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
import type { Application } from '../models/application'; | ||
import { appConfigs } from '../presets'; | ||
|
||
test.describe('next build @nextjs', () => { | ||
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); | ||
}); | ||
}); |
This file contains 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 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 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,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; | ||
|
||
return <Provider {...props} />; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { ClerkProvider } from './pages/ClerkProvider'; | ||
export { ClerkProvider } from './client-boundary/ClerkProvider'; | ||
export { SignedIn, SignedOut } from './client-boundary/controlComponents'; |