-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-react,nextjs): Speed up clerk-js loading by using a `<scri…
…pt/>` tag (#3156) * feat(clerk-react,nextjs): Inject script tag into document to speed up clerk-js loading * feat(clerk-react,nextjs): Use native script for App Router * chore(clerk-react): Cleanup * chore(clerk-react): Add changelog * chore(clerk-react,nextjs): Move clerk-script component directly inside the next package
- Loading branch information
1 parent
e6fc58a
commit f98e480
Showing
6 changed files
with
104 additions
and
12 deletions.
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