-
Notifications
You must be signed in to change notification settings - Fork 471
feat(nextjs): Print clerk init notice on dev keys #9661
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
shane-kercheval
wants to merge
7
commits into
main
Choose a base branch
from
feat/nextjs-dev-key-init-hint
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
7 commits
Select commit
Hold shift + click to select a range
edaa834
feat(nextjs): Print clerk init notice on dev keys
shane-kercheval 62243b6
Merge remote-tracking branch 'origin/main' into feat/nextjs-dev-key-i…
shane-kercheval d362050
Merge branch 'main' into feat/nextjs-dev-key-init-hint
shane-kercheval 68e64e8
fix(nextjs): Pass keyless state from Pages Router provider
shane-kercheval 306ee10
docs(nextjs): Correct the dev-key notice comment
shane-kercheval 5829e49
Merge branch 'main' into feat/nextjs-dev-key-init-hint
shane-kercheval e12d970
test(shared): Derive guidance text instead of hardcoding it
shane-kercheval 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,10 @@ | ||
| --- | ||
| '@clerk/nextjs': minor | ||
| '@clerk/shared': minor | ||
| --- | ||
|
|
||
| Print a one-time notice in the server terminal when `<ClerkProvider>` renders with a development publishable key, naming `npx clerk@latest init` as the way to get working keys without a Clerk account. The notice appears once per process, so once per build worker during `next build`, and on the first server render under `next dev`. It never prints in the browser, in deployed runtimes, or when the keys came from keyless mode. It is silenced by the existing `unsafe_disableDevelopmentModeConsoleWarning` prop or `NEXT_PUBLIC_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING` env var. | ||
|
|
||
| Fix `unsafe_disableDevelopmentModeConsoleWarning` being ignored when passed as a prop to the Next.js `<ClerkProvider>`; previously only the env var took effect, so the prop did not silence the browser development-keys warning either. | ||
|
|
||
| `@clerk/shared/keys` now exports `accountlessInitGuidance`, the sentence used by this notice and by the existing missing-key errors. |
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
79 changes: 79 additions & 0 deletions
79
packages/nextjs/src/app-router/client/__tests__/ClerkProvider.test.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,79 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import React from 'react'; | ||
| import { renderToStaticMarkup } from 'react-dom/server'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { maybeShowDevelopmentKeyNotice } from '../../../utils/devKeyNotice'; | ||
| import { ClientClerkProvider } from '../ClerkProvider'; | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| useRouter: () => ({ refresh: vi.fn(), push: vi.fn(), replace: vi.fn() }), | ||
| })); | ||
| vi.mock('../useAwaitablePush', () => ({ useAwaitablePush: () => vi.fn() })); | ||
| vi.mock('../useAwaitableReplace', () => ({ useAwaitableReplace: () => vi.fn() })); | ||
| vi.mock('../../server-actions', () => ({ invalidateCacheAction: vi.fn() })); | ||
| vi.mock('../ClerkScripts', () => ({ ClerkScripts: () => null })); | ||
| vi.mock('../../../utils/router-telemetry', () => ({ RouterTelemetry: () => null })); | ||
| vi.mock('@clerk/react/internal', () => ({ | ||
| InternalClerkProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| })); | ||
| vi.mock('../../../utils/devKeyNotice', () => ({ maybeShowDevelopmentKeyNotice: vi.fn() })); | ||
|
|
||
| const notice = maybeShowDevelopmentKeyNotice as unknown as ReturnType<typeof vi.fn>; | ||
| const DEV_KEY = 'pk_test_ZmFrZS1jbGVyay5hY2NvdW50cy5kZXYk'; | ||
| const ORIGINAL_ENV = { ...process.env }; | ||
|
|
||
| describe('ClientClerkProvider (server render)', () => { | ||
| beforeEach(() => { | ||
| delete process.env.NEXT_PUBLIC_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING; | ||
| notice.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = { ...ORIGINAL_ENV }; | ||
| }); | ||
|
|
||
| it('asks for the development key notice with the resolved key', () => { | ||
| const html = renderToStaticMarkup(<ClientClerkProvider publishableKey={DEV_KEY}>child</ClientClerkProvider>); | ||
|
|
||
| expect(html).toContain('child'); | ||
| expect(notice).toHaveBeenCalledTimes(1); | ||
| expect(notice).toHaveBeenCalledWith({ publishableKey: DEV_KEY, disabled: false, keyless: false }); | ||
| }); | ||
|
|
||
| it('passes the opt-out through when set as a prop', () => { | ||
| renderToStaticMarkup( | ||
| <ClientClerkProvider | ||
| publishableKey={DEV_KEY} | ||
| unsafe_disableDevelopmentModeConsoleWarning | ||
| > | ||
| child | ||
| </ClientClerkProvider>, | ||
| ); | ||
|
|
||
| expect(notice).toHaveBeenCalledWith(expect.objectContaining({ disabled: true })); | ||
| }); | ||
|
|
||
| it('passes the opt-out through when set by env var', () => { | ||
| process.env.NEXT_PUBLIC_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING = 'true'; | ||
|
|
||
| renderToStaticMarkup(<ClientClerkProvider publishableKey={DEV_KEY}>child</ClientClerkProvider>); | ||
|
|
||
| expect(notice).toHaveBeenCalledWith(expect.objectContaining({ disabled: true })); | ||
| }); | ||
|
|
||
| it('flags keys that came from keyless mode', () => { | ||
| renderToStaticMarkup( | ||
| <ClientClerkProvider | ||
| publishableKey={DEV_KEY} | ||
| __internal_keyless_claimKeylessApplicationUrl='https://dashboard.clerk.com/claim' | ||
| > | ||
| child | ||
| </ClientClerkProvider>, | ||
| ); | ||
|
|
||
| expect(notice).toHaveBeenCalledWith(expect.objectContaining({ keyless: true })); | ||
| }); | ||
| }); |
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
70 changes: 70 additions & 0 deletions
70
packages/nextjs/src/pages/__tests__/ClerkProvider.test.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,70 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import React from 'react'; | ||
| import { renderToStaticMarkup } from 'react-dom/server'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { maybeShowDevelopmentKeyNotice } from '../../utils/devKeyNotice'; | ||
| import { ClerkProvider } from '../ClerkProvider'; | ||
|
|
||
| vi.mock('next/router', () => ({ | ||
| useRouter: () => ({ push: vi.fn(), replace: vi.fn() }), | ||
| })); | ||
| vi.mock('../ClerkScripts', () => ({ ClerkScripts: () => null })); | ||
| vi.mock('../../utils/router-telemetry', () => ({ RouterTelemetry: () => null })); | ||
| vi.mock('@clerk/react/internal', () => ({ | ||
| InternalClerkProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| setClerkJSLoadingErrorPackageName: vi.fn(), | ||
| setErrorThrowerOptions: vi.fn(), | ||
| })); | ||
| vi.mock('../../utils/devKeyNotice', () => ({ maybeShowDevelopmentKeyNotice: vi.fn() })); | ||
|
|
||
| const notice = maybeShowDevelopmentKeyNotice as unknown as ReturnType<typeof vi.fn>; | ||
| const DEV_KEY = 'pk_test_ZmFrZS1jbGVyay5hY2NvdW50cy5kZXYk'; | ||
| const ORIGINAL_ENV = { ...process.env }; | ||
|
|
||
| describe('Pages Router ClerkProvider (server render)', () => { | ||
| beforeEach(() => { | ||
| delete process.env.NEXT_PUBLIC_CLERK_UNSAFE_DISABLE_DEVELOPMENT_MODE_CONSOLE_WARNING; | ||
| notice.mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = { ...ORIGINAL_ENV }; | ||
| }); | ||
|
|
||
| it('asks for the development key notice with the resolved key and opt-out', () => { | ||
| const html = renderToStaticMarkup(<ClerkProvider publishableKey={DEV_KEY}>child</ClerkProvider>); | ||
|
|
||
| expect(html).toContain('child'); | ||
| expect(notice).toHaveBeenCalledTimes(1); | ||
| expect(notice).toHaveBeenCalledWith({ publishableKey: DEV_KEY, disabled: false, keyless: false }); | ||
| }); | ||
|
|
||
| it('flags keys that came from keyless mode', () => { | ||
| renderToStaticMarkup( | ||
| <ClerkProvider | ||
| publishableKey={DEV_KEY} | ||
| __internal_keyless_claimKeylessApplicationUrl='https://dashboard.clerk.com/claim' | ||
| > | ||
| child | ||
| </ClerkProvider>, | ||
| ); | ||
|
|
||
| expect(notice).toHaveBeenCalledWith(expect.objectContaining({ keyless: true })); | ||
| }); | ||
|
|
||
| it('passes the opt-out through when set as a prop', () => { | ||
| renderToStaticMarkup( | ||
| <ClerkProvider | ||
| publishableKey={DEV_KEY} | ||
| unsafe_disableDevelopmentModeConsoleWarning | ||
| > | ||
| child | ||
| </ClerkProvider>, | ||
| ); | ||
|
|
||
| expect(notice).toHaveBeenCalledWith(expect.objectContaining({ disabled: true })); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.