-
Notifications
You must be signed in to change notification settings - Fork 280
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
Conversation
🦋 Changeset detectedLatest commit: 897e5b8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…e the next package
const existingScript = document.querySelector<HTMLScriptElement>('script[data-clerk-js-script]'); | ||
|
||
if (existingScript) { | ||
return new Promise((resolve, reject) => { | ||
existingScript.addEventListener('load', () => { | ||
resolve(existingScript); | ||
}); | ||
|
||
existingScript.addEventListener('error', () => { | ||
reject(FAILED_TO_LOAD_ERROR); | ||
}); | ||
}); | ||
} | ||
|
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.
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.
One comment about the strategy passed to next/script
, which I think we should leave as the default. Otherwise this lgtm. Nice work.
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.
👏🏻
async | ||
crossOrigin='anonymous' | ||
{...buildClerkJsScriptAttributes(options)} | ||
{...(props.router === 'pages' ? { strategy: 'beforeInteractive' } : {})} |
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.
{...(props.router === 'pages' ? { strategy: 'beforeInteractive' } : {})} | |
strategy: props.router === 'pages' ? 'beforeInteractive' : undefined |
Description
Metrics (Fast 3G)
Previous 4120ms
New Nextjs 2907ms
-30% in loading time
Metrics (Cable)
Previous 613ms
New Nextjs 617ms
Almost no change
Nextjs
Benefits of using the
next/script
is that it moves scripts inside<head/>
in Pages router. It also has build it cache to avoid loading 2 scripts at the same time.Using
next/script
with our ClerkProvide that targets the App Router was not possible asnext/script
need to be used inside the "document" which means inside<html/>
.More SSR Frameworks
<head/>
.For these I would offer a dedicated
<ClerkJSScript/>
.CSR React
Checklist
npm test
runs as expected.npm run build
runs as expected.Type of change