Skip to content

Conversation

@panteliselef
Copy link
Member

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 as next/script need to be used inside the "document" which means inside <html/>.

More SSR Frameworks

  • Remix does not a similar component as Next.js and it will not automatically move scripts inside <head/>.
  • Gatsby seems to offer one, but it lacks the "beforeInteraction" strategy which is the one that will add the script on generated html before hydration.
    For these I would offer a dedicated <ClerkJSScript/>.

CSR React

  • Supporting a script tag in a CSR app does not improve loading times.

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot
Copy link

changeset-bot bot commented Apr 9, 2024

🦋 Changeset detected

Latest commit: 897e5b8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@clerk/nextjs Minor
@clerk/clerk-react Minor
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
gatsby-plugin-clerk Patch
@clerk/remix Patch

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

@panteliselef panteliselef requested review from brkalow, dimkl and nikosdouvlis and removed request for nikosdouvlis April 9, 2024 15:46
@panteliselef panteliselef self-assigned this Apr 10, 2024
Comment on lines +25 to +38
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);
});
});
}

Copy link
Member Author

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.

Copy link
Member

@brkalow brkalow left a 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.

Copy link
Member

@nikosdouvlis nikosdouvlis left a 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' } : {})}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{...(props.router === 'pages' ? { strategy: 'beforeInteractive' } : {})}
strategy: props.router === 'pages' ? 'beforeInteractive' : undefined

@brkalow brkalow enabled auto-merge (squash) April 12, 2024 03:04
@brkalow brkalow merged commit f98e480 into main Apr 15, 2024
@brkalow brkalow deleted the elef/sdk-1003-inject-clerk-js-script-for-ssr branch April 15, 2024 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants