-
Notifications
You must be signed in to change notification settings - Fork 453
feat(nextjs): Isolate nonce fetch in Suspense boundary for PPR support #7773
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
Merged
jacekradko
merged 23 commits into
main
from
jacek/user-4607-do-not-await-nonce-in-the-clerkprovider
Feb 13, 2026
Merged
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
892a4d2
feat(nextjs): Isolate nonce fetch in Suspense boundary for PPR support
jacekradko 4bcd31c
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 526d114
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 1f9d256
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 141ec6e
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 3d14a18
fix(nextjs): Extract shared ClerkScriptTags and add getNonce error ha…
jacekradko 5913ae9
fix(nextjs): Import from @clerk/shared to avoid barrel side effects i…
jacekradko 8043384
fix(nextjs): Guard React.cache call for non-RSC test environments
jacekradko f66bd15
chore: Add changeset for nextjs nonce changes
jacekradko 1861e52
Merge remote-tracking branch 'origin/main' into jacek/user-4607-do-no…
jacekradko 137f9f2
fix(nextjs): Fix formatting
jacekradko 8dd052f
Merge remote-tracking branch 'origin/main' into jacek/user-4607-do-no…
jacekradko 5af964a
refactor(nextjs): move getNonce into DynamicClerkScripts and split Cl…
jacekradko 17f7390
test(nextjs): add tests for DynamicClerkScripts nonce behavior
jacekradko d6fed2a
Merge remote-tracking branch 'origin/main' into jacek/user-4607-do-no…
jacekradko 9e46dfe
fix(nextjs): resolve lint errors for import sorting and type-only imp…
jacekradko 1aaf37b
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 0c79adb
Update .changeset/thin-flies-rush.md
jacekradko 31da6d0
fix(nextjs): replace __internal_skipScripts with scriptsSlot composit…
jacekradko 79008a4
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko e340ce3
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 513e8f0
merge main
jacekradko 7d159c9
Merge branch 'main' into jacek/user-4607-do-not-await-nonce-in-the-cl…
jacekradko 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 | ||
| --- | ||
|
|
||
| Isolate nonce fetch in Suspense boundary for PPR support and guard `React.cache` for non-RSC environments | ||
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,27 @@ | ||
| import { useClerk } from '@clerk/react'; | ||
| import React from 'react'; | ||
|
|
||
| import { useClerkNextOptions } from '../../client-boundary/NextOptionsContext'; | ||
| import { ClerkScriptTags } from '../../utils/clerk-script-tags'; | ||
|
|
||
| export function ClerkScripts() { | ||
| const { publishableKey, clerkJSUrl, clerkJSVersion, clerkUIUrl, nonce, prefetchUI } = useClerkNextOptions(); | ||
| const { domain, proxyUrl } = useClerk(); | ||
|
|
||
| if (!publishableKey) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <ClerkScriptTags | ||
| publishableKey={publishableKey} | ||
| clerkJSUrl={clerkJSUrl} | ||
| clerkJSVersion={clerkJSVersion} | ||
| clerkUIUrl={clerkUIUrl} | ||
| nonce={nonce} | ||
| domain={domain} | ||
| proxyUrl={proxyUrl} | ||
| prefetchUI={prefetchUI} | ||
| /> | ||
| ); | ||
| } |
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
60 changes: 60 additions & 0 deletions
60
packages/nextjs/src/app-router/server/DynamicClerkScripts.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,60 @@ | ||
| import { headers } from 'next/headers'; | ||
| import React from 'react'; | ||
|
|
||
| import { ClerkScriptTags } from '../../utils/clerk-script-tags'; | ||
| import { getScriptNonceFromHeader, isPrerenderingBailout } from './utils'; | ||
|
|
||
| async function getNonce(): Promise<string> { | ||
| try { | ||
| const headersList = await headers(); | ||
| const nonce = headersList.get('X-Nonce'); | ||
| return nonce | ||
| ? nonce | ||
| : // Fallback to extracting from CSP header | ||
| getScriptNonceFromHeader(headersList.get('Content-Security-Policy') || '') || ''; | ||
| } catch (e) { | ||
| if (isPrerenderingBailout(e)) { | ||
| throw e; | ||
| } | ||
| // Graceful degradation — scripts load without nonce | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| type DynamicClerkScriptsProps = { | ||
| publishableKey: string; | ||
| clerkJSUrl?: string; | ||
| clerkJSVersion?: string; | ||
| clerkUIUrl?: string; | ||
| domain?: string; | ||
| proxyUrl?: string; | ||
| prefetchUI?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Server component that fetches nonce from headers and renders Clerk scripts. | ||
| * This component should be wrapped in a Suspense boundary to isolate the dynamic | ||
| * nonce fetching from the rest of the page, allowing static rendering/PPR to work. | ||
| */ | ||
| export async function DynamicClerkScripts(props: DynamicClerkScriptsProps) { | ||
| const { publishableKey, clerkJSUrl, clerkJSVersion, clerkUIUrl, domain, proxyUrl, prefetchUI } = props; | ||
|
|
||
| if (!publishableKey) { | ||
| return null; | ||
| } | ||
|
|
||
| const nonce = await getNonce(); | ||
|
|
||
| return ( | ||
| <ClerkScriptTags | ||
| publishableKey={publishableKey} | ||
| clerkJSUrl={clerkJSUrl} | ||
| clerkJSVersion={clerkJSVersion} | ||
| clerkUIUrl={clerkUIUrl} | ||
| nonce={nonce} | ||
| domain={domain} | ||
| proxyUrl={proxyUrl} | ||
| prefetchUI={prefetchUI} | ||
| /> | ||
| ); | ||
| } |
89 changes: 89 additions & 0 deletions
89
packages/nextjs/src/app-router/server/__tests__/DynamicClerkScripts.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,89 @@ | ||
| import type React from 'react'; | ||
| import { renderToStaticMarkup } from 'react-dom/server'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { DynamicClerkScripts } from '../DynamicClerkScripts'; | ||
|
|
||
| vi.mock('next/headers', () => ({ | ||
| headers: vi.fn(), | ||
| })); | ||
|
|
||
| import { headers } from 'next/headers'; | ||
|
|
||
| const mockHeaders = headers as unknown as ReturnType<typeof vi.fn>; | ||
|
|
||
| const render = async (element: Promise<React.JSX.Element | null>) => { | ||
| const resolved = await element; | ||
| if (!resolved) { | ||
| return ''; | ||
| } | ||
| return renderToStaticMarkup(resolved); | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| publishableKey: 'pk_test_123', | ||
| }; | ||
|
|
||
| describe('DynamicClerkScripts', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('returns null when publishableKey is empty', async () => { | ||
| const html = await render(DynamicClerkScripts({ publishableKey: '' })); | ||
| expect(html).toBe(''); | ||
| }); | ||
|
|
||
| it('uses X-Nonce header when present', async () => { | ||
| mockHeaders.mockResolvedValue( | ||
| new Map([ | ||
| ['X-Nonce', 'test-nonce-123'], | ||
| ['Content-Security-Policy', ''], | ||
| ]), | ||
| ); | ||
|
|
||
| const html = await render(DynamicClerkScripts(defaultProps)); | ||
| expect(html).toContain('nonce="test-nonce-123"'); | ||
| }); | ||
|
|
||
| it('falls back to CSP header when X-Nonce is absent', async () => { | ||
| mockHeaders.mockResolvedValue( | ||
| new Map([ | ||
| ['X-Nonce', null], | ||
| ['Content-Security-Policy', "script-src 'nonce-csp-nonce-456'"], | ||
| ]), | ||
| ); | ||
|
|
||
| const html = await render(DynamicClerkScripts(defaultProps)); | ||
| expect(html).toContain('nonce="csp-nonce-456"'); | ||
| }); | ||
|
|
||
| it('renders scripts without a nonce value when neither X-Nonce nor CSP header is present', async () => { | ||
| mockHeaders.mockResolvedValue( | ||
| new Map([ | ||
| ['X-Nonce', null], | ||
| ['Content-Security-Policy', ''], | ||
| ]), | ||
| ); | ||
|
|
||
| const html = await render(DynamicClerkScripts(defaultProps)); | ||
| expect(html).toContain('data-clerk-js-script'); | ||
| expect(html).not.toContain('nonce="test'); | ||
| expect(html).not.toContain('nonce="csp'); | ||
| }); | ||
|
|
||
| it('rethrows prerendering bailout errors', async () => { | ||
| mockHeaders.mockRejectedValue(new Error('Dynamic server usage: headers')); | ||
|
|
||
| await expect(render(DynamicClerkScripts(defaultProps))).rejects.toThrow('Dynamic server usage: headers'); | ||
| }); | ||
|
|
||
| it('gracefully degrades when headers() throws a non-bailout error', async () => { | ||
| mockHeaders.mockRejectedValue(new Error('some unexpected error')); | ||
|
|
||
| const html = await render(DynamicClerkScripts(defaultProps)); | ||
| expect(html).toContain('data-clerk-js-script'); | ||
| expect(html).not.toContain('nonce="test'); | ||
| expect(html).not.toContain('nonce="csp'); | ||
| }); | ||
| }); |
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
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.