-
Notifications
You must be signed in to change notification settings - Fork 435
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
Open
jacekradko
wants to merge
11
commits into
main
Choose a base branch
from
jacek/user-4607-do-not-await-nonce-in-the-clerkprovider
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
11 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 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
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { ClerkScriptTags } from '../../utils/clerk-script-tags'; | ||
| import { getNonce } from './utils'; | ||
|
|
||
| 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} | ||
| /> | ||
| ); | ||
| } |
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
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,59 @@ | ||
| import { buildClerkJSScriptAttributes, clerkJSScriptUrl, clerkUIScriptUrl } from '@clerk/shared/loadClerkJsScript'; | ||
| import React from 'react'; | ||
|
|
||
| type ClerkScriptTagsProps = { | ||
| publishableKey: string; | ||
| clerkJSUrl?: string; | ||
| clerkJSVersion?: string; | ||
| clerkUIUrl?: string; | ||
| nonce?: string; | ||
| domain?: string; | ||
| proxyUrl?: string; | ||
| prefetchUI?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Pure component that renders the Clerk script tags. | ||
| * Shared between `ClerkScripts` (client, app router) and `DynamicClerkScripts` (server). | ||
| * No hooks or client-only imports — safe for both server and client components. | ||
| */ | ||
| export function ClerkScriptTags(props: ClerkScriptTagsProps) { | ||
| const { publishableKey, clerkJSUrl, clerkJSVersion, clerkUIUrl, nonce, domain, proxyUrl, prefetchUI } = props; | ||
|
|
||
| const opts = { | ||
| publishableKey, | ||
| clerkJSUrl, | ||
| clerkJSVersion, | ||
| clerkUIUrl, | ||
| nonce, | ||
| domain, | ||
| proxyUrl, | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <script | ||
| src={clerkJSScriptUrl(opts)} | ||
| data-clerk-js-script | ||
| async | ||
| crossOrigin='anonymous' | ||
| {...buildClerkJSScriptAttributes(opts)} | ||
| /> | ||
| {/* Use <link rel='preload'> instead of <script> for the UI bundle. | ||
| This tells the browser to download the resource immediately (high priority) | ||
| but doesn't execute it, avoiding race conditions with __clerkSharedModules | ||
| registration (which happens when React code runs @clerk/ui/register). | ||
| When loadClerkUIScript() later adds a <script> tag, the browser uses the | ||
| cached resource and executes it without re-downloading. */} | ||
| {prefetchUI !== false && ( | ||
| <link | ||
| rel='preload' | ||
| href={clerkUIScriptUrl(opts)} | ||
| as='script' | ||
| crossOrigin='anonymous' | ||
| nonce={nonce} | ||
| /> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
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.
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.
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.
Add automated coverage for getNonce behavior.
No tests are included for the new nonce retrieval/caching paths; please add tests covering X-Nonce presence, CSP fallback, and prerendering bailout handling.
As per coding guidelines, “If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes.”
🤖 Prompt for AI Agents