-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathclerk-js-script.tsx
56 lines (48 loc) · 1.72 KB
/
clerk-js-script.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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, nonce } = useClerkNextOptions();
const { domain, proxyUrl } = useClerk();
/**
* If no publishable key, avoid appending an invalid script in the DOM.
*/
if (!publishableKey) {
return null;
}
const options = {
domain,
proxyUrl,
publishableKey,
clerkJSUrl,
clerkJSVersion,
clerkJSVariant,
nonce,
};
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 };