-
Notifications
You must be signed in to change notification settings - Fork 279
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
feat(clerk-react,nextjs): Speed up clerk-js loading by using a <script/>
tag
#3156
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
61e439a
feat(clerk-react,nextjs): Inject script tag into document to speed up…
panteliselef f8e0f59
feat(clerk-react,nextjs): Use native script for App Router
panteliselef 0510334
chore(clerk-react): Cleanup
panteliselef db2be7d
chore(clerk-react): Add changelog
panteliselef 3003b5c
chore(clerk-react,nextjs): Move clerk-script component directly insid…
panteliselef ffa9071
Merge branch 'main' into elef/sdk-1003-inject-clerk-js-script-for-ssr
BRKalow f492f7e
chore(nextjs): Address PR comments
panteliselef 897e5b8
Merge branch 'main' into elef/sdk-1003-inject-clerk-js-script-for-ssr
panteliselef 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 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,9 @@ | ||
--- | ||
'@clerk/nextjs': minor | ||
'@clerk/clerk-react': minor | ||
--- | ||
|
||
Speed up loading of clerk-js by using a `<script/>` tag when html is generated. | ||
This is supported during SSR, SSG in | ||
- Next.js Pages Router | ||
- Next.js App Router |
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,47 @@ | ||
import { useClerk } from '@clerk/clerk-react'; | ||
import { buildClerkJsScriptAttributes, clerkJsScriptUrl } from '@clerk/clerk-react/internal'; | ||
import NextScript from 'next/script'; | ||
import React from 'react'; | ||
|
||
import { useClerkNextOptions } from '../client-boundary/NextOptionsContext'; | ||
|
||
type ClerkJSScriptProps = { | ||
router: 'app' | 'pages'; | ||
}; | ||
|
||
function ClerkJSScript(props: ClerkJSScriptProps) { | ||
const { publishableKey, clerkJSUrl, clerkJSVersion, clerkJSVariant } = useClerkNextOptions(); | ||
const { domain, proxyUrl } = useClerk(); | ||
const options = { | ||
domain, | ||
proxyUrl, | ||
publishableKey: publishableKey!, | ||
clerkJSUrl, | ||
clerkJSVersion, | ||
clerkJSVariant, | ||
}; | ||
const scriptUrl = clerkJsScriptUrl(options); | ||
|
||
/** | ||
* Notes: | ||
* `next/script` in 13.x.x when used with App Router will fail to pass any of our `data-*` attributes, resulting in errors | ||
* Nextjs App Router will automatically move inline scripts inside `<head/>` | ||
* Using the `nextjs/script` for App Router with the `beforeInteractive` strategy will throw an error because our custom script will be mounted outside the `html` tag. | ||
*/ | ||
const Script = props.router === 'app' ? 'script' : NextScript; | ||
|
||
return ( | ||
<Script | ||
src={scriptUrl} | ||
data-clerk-js-script | ||
async | ||
// `nextjs/script` will add defer by default and does not get removed when we async is true | ||
defer={props.router === 'pages' ? false : undefined} | ||
crossOrigin='anonymous' | ||
strategy={props.router === 'pages' ? 'beforeInteractive' : undefined} | ||
{...buildClerkJsScriptAttributes(options)} | ||
/> | ||
); | ||
} | ||
|
||
export { ClerkJSScript }; |
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,3 +1,5 @@ | ||
export { setErrorThrowerOptions } from './errors/errorThrower'; | ||
export { MultisessionAppSupport } from './components/controlComponents'; | ||
export { useRoutingProps } from './hooks/useRoutingProps'; | ||
|
||
export { clerkJsScriptUrl, buildClerkJsScriptAttributes } from './utils/loadClerkJsScript'; |
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
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.
This also allows anyone to add a script without waiting for an official support.