-
Notifications
You must be signed in to change notification settings - Fork 436
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
base: main
Are you sure you want to change the base?
Changes from 17 commits
892a4d2
4bcd31c
526d114
1f9d256
141ec6e
3d14a18
5913ae9
8043384
f66bd15
1861e52
137f9f2
8dd052f
5af964a
17f7390
d6fed2a
9e46dfe
1aaf37b
0c79adb
31da6d0
79008a4
e340ce3
513e8f0
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 | ||
| --- | ||
|
|
||
| Isolate nonce fetch in Suspense boundary for PPR support and guard `React.cache` for non-RSC environments | ||
| 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} | ||
| /> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,14 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { Ui } from '@clerk/react/internal'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { InitialState, Without } from '@clerk/shared/types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { headers } from 'next/headers'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React, { Suspense } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getDynamicAuthData } from '../../server/buildClerkProps'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { NextClerkProviderProps } from '../../types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { mergeNextClerkPropsWithEnv } from '../../utils/mergeNextClerkPropsWithEnv'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ClientClerkProvider } from '../client/ClerkProvider'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { DynamicClerkScripts } from './DynamicClerkScripts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getKeylessStatus, KeylessProvider } from './keyless-provider'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { buildRequestLike, getScriptNonceFromHeader } from './utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { buildRequestLike } from './utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getDynamicClerkState = React.cache(async function getDynamicClerkState() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const request = await buildRequestLike(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -17,43 +17,59 @@ const getDynamicClerkState = React.cache(async function getDynamicClerkState() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getNonceHeaders = React.cache(async function getNonceHeaders() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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') || '') || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export async function ClerkProvider<TUi extends Ui = Ui>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| props: Without<NextClerkProviderProps<TUi>, '__internal_invokeMiddlewareOnAuthStateChange'>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { children, dynamic, ...rest } = props; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const statePromiseOrValue = dynamic ? getDynamicClerkState() : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const noncePromiseOrValue = dynamic ? getNonceHeaders() : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const propsWithEnvs = mergeNextClerkPropsWithEnv({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...rest, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Even though we always cast to InitialState here, this might still be a promise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // While not reflected in the public types, we do support this for React >= 19 for internal use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initialState: statePromiseOrValue as InitialState | undefined, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonce: await noncePromiseOrValue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { shouldRunAsKeyless, runningWithClaimedKeys } = await getKeylessStatus(propsWithEnvs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // When dynamic mode is enabled, render scripts in a Suspense boundary to isolate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // the nonce fetching (which calls headers()) from the rest of the page. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // This allows the page to remain statically renderable / use PPR. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dynamicScripts = dynamic ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Suspense> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <DynamicClerkScripts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| publishableKey={propsWithEnvs.publishableKey} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clerkJSUrl={propsWithEnvs.clerkJSUrl} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clerkJSVersion={propsWithEnvs.clerkJSVersion} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clerkUIUrl={propsWithEnvs.clerkUIUrl} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| domain={propsWithEnvs.domain} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| proxyUrl={propsWithEnvs.proxyUrl} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prefetchUI={propsWithEnvs.prefetchUI} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Suspense> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (shouldRunAsKeyless) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <KeylessProvider | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rest={propsWithEnvs} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| runningWithClaimedKeys={runningWithClaimedKeys} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| __internal_skipScripts={dynamic} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {dynamicScripts} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dynamicScripts = dynamic ? ( | |
| <Suspense> | |
| <DynamicClerkScripts | |
| publishableKey={propsWithEnvs.publishableKey} | |
| clerkJSUrl={propsWithEnvs.clerkJSUrl} | |
| clerkJSVersion={propsWithEnvs.clerkJSVersion} | |
| clerkUIUrl={propsWithEnvs.clerkUIUrl} | |
| domain={propsWithEnvs.domain} | |
| proxyUrl={propsWithEnvs.proxyUrl} | |
| prefetchUI={propsWithEnvs.prefetchUI} | |
| /> | |
| </Suspense> | |
| ) : null; | |
| if (shouldRunAsKeyless) { | |
| return ( | |
| <KeylessProvider | |
| rest={propsWithEnvs} | |
| runningWithClaimedKeys={runningWithClaimedKeys} | |
| __internal_skipScripts={dynamic} | |
| > | |
| {dynamicScripts} | |
| const scripts = dynamic ? ( | |
| <Suspense> | |
| <DynamicClerkScripts | |
| publishableKey={propsWithEnvs.publishableKey} | |
| clerkJSUrl={propsWithEnvs.clerkJSUrl} | |
| clerkJSVersion={propsWithEnvs.clerkJSVersion} | |
| clerkUIUrl={propsWithEnvs.clerkUIUrl} | |
| domain={propsWithEnvs.domain} | |
| proxyUrl={propsWithEnvs.proxyUrl} | |
| prefetchUI={propsWithEnvs.prefetchUI} | |
| /> | |
| </Suspense> | |
| ) : <ClerkScripts />; | |
| if (shouldRunAsKeyless) { | |
| return ( | |
| <KeylessProvider | |
| rest={propsWithEnvs} | |
| runningWithClaimedKeys={runningWithClaimedKeys} | |
| scriptsSlot={scripts} | |
| > |
| 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} | ||
| /> | ||
| ); | ||
| } |
| 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'); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.