@clerk/nextjs
Version:
Clerk SDK for NextJS
1 lines • 7.54 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/app-router/client/ClerkProvider.tsx"],"sourcesContent":["'use client';\nimport { ClerkProvider as ReactClerkProvider } from '@clerk/clerk-react';\nimport { inBrowser } from '@clerk/shared/browser';\nimport { logger } from '@clerk/shared/logger';\nimport dynamic from 'next/dynamic';\nimport { useRouter } from 'next/navigation';\nimport nextPackage from 'next/package.json';\nimport React, { useEffect, useTransition } from 'react';\n\nimport { useSafeLayoutEffect } from '../../client-boundary/hooks/useSafeLayoutEffect';\nimport { ClerkNextOptionsProvider, useClerkNextOptions } from '../../client-boundary/NextOptionsContext';\nimport type { NextClerkProviderProps } from '../../types';\nimport { ClerkJSScript } from '../../utils/clerk-js-script';\nimport { canUseKeyless } from '../../utils/feature-flags';\nimport { mergeNextClerkPropsWithEnv } from '../../utils/mergeNextClerkPropsWithEnv';\nimport { isNextWithUnstableServerActions } from '../../utils/sdk-versions';\nimport { invalidateCacheAction } from '../server-actions';\nimport { useAwaitablePush } from './useAwaitablePush';\nimport { useAwaitableReplace } from './useAwaitableReplace';\n\n/**\n * LazyCreateKeylessApplication should only be loaded if the conditions below are met.\n * Note: Using lazy() with Suspense instead of dynamic is not possible as React will throw a hydration error when `ClerkProvider` wraps `<html><body>...`\n */\nconst LazyCreateKeylessApplication = dynamic(() =>\n import('./keyless-creator-reader.js').then(m => m.KeylessCreatorOrReader),\n);\n\nconst NextClientClerkProvider = (props: NextClerkProviderProps) => {\n if (isNextWithUnstableServerActions) {\n const deprecationWarning = `Clerk:\\nYour current Next.js version (${nextPackage.version}) will be deprecated in the next major release of \"@clerk/nextjs\". Please upgrade to next@14.1.0 or later.`;\n if (inBrowser()) {\n logger.warnOnce(deprecationWarning);\n } else {\n logger.logOnce(`\\n\\x1b[43m----------\\n${deprecationWarning}\\n----------\\x1b[0m\\n`);\n }\n }\n\n const { __unstable_invokeMiddlewareOnAuthStateChange = true, children } = props;\n const router = useRouter();\n const push = useAwaitablePush();\n const replace = useAwaitableReplace();\n const [isPending, startTransition] = useTransition();\n\n // Avoid rendering nested ClerkProviders by checking for the existence of the ClerkNextOptions context provider\n const isNested = Boolean(useClerkNextOptions());\n if (isNested) {\n return props.children;\n }\n\n useEffect(() => {\n if (!isPending) {\n window.__clerk_internal_invalidateCachePromise?.();\n }\n }, [isPending]);\n\n useSafeLayoutEffect(() => {\n window.__unstable__onBeforeSetActive = () => {\n /**\n * We need to invalidate the cache in case the user is navigating to a page that\n * was previously cached using the auth state that was active at the time.\n *\n * We also need to await for the invalidation to happen before we navigate,\n * otherwise the navigation will use the cached page.\n *\n * For example, if we did not invalidate the flow, the following scenario would be broken:\n * - The middleware is configured in such a way that it redirects you back to the same page if a certain condition is true (eg, you need to pick an org)\n * - The user has a <Link href=/> component in the page\n * - The UB is mounted with afterSignOutUrl=/\n * - The user clicks the Link. A nav to / happens, a 307 to the current page is returned so a navigation does not take place. The / navigation is now cached as a 307 to the current page\n * - The user clicks sign out\n * - We call router.refresh()\n * - We navigate to / but its cached and instead, we 'redirect' to the current page\n *\n * For more information on cache invalidation, see:\n * https://nextjs.org/docs/app/building-your-application/caching#invalidation-1\n */\n return new Promise(res => {\n window.__clerk_internal_invalidateCachePromise = res;\n\n // NOTE: the following code will allow `useReverification()` to work properly when `handlerReverification` is called inside `startTransition`\n if (window.next?.version && typeof window.next.version === 'string' && window.next.version.startsWith('13')) {\n startTransition(() => {\n router.refresh();\n });\n } else {\n void invalidateCacheAction().then(() => res());\n }\n });\n };\n\n window.__unstable__onAfterSetActive = () => {\n if (__unstable_invokeMiddlewareOnAuthStateChange) {\n return router.refresh();\n }\n };\n }, []);\n\n const mergedProps = mergeNextClerkPropsWithEnv({\n ...props,\n routerPush: push,\n routerReplace: replace,\n });\n\n return (\n <ClerkNextOptionsProvider options={mergedProps}>\n <ReactClerkProvider {...mergedProps}>\n <ClerkJSScript router='app' />\n {children}\n </ReactClerkProvider>\n </ClerkNextOptionsProvider>\n );\n};\n\nexport const ClientClerkProvider = (props: NextClerkProviderProps & { disableKeyless?: boolean }) => {\n const { children, disableKeyless = false, ...rest } = props;\n const safePublishableKey = mergeNextClerkPropsWithEnv(rest).publishableKey;\n\n if (safePublishableKey || !canUseKeyless || disableKeyless) {\n return <NextClientClerkProvider {...rest}>{children}</NextClientClerkProvider>;\n }\n\n return (\n <LazyCreateKeylessApplication>\n <NextClientClerkProvider {...rest}>{children}</NextClientClerkProvider>\n </LazyCreateKeylessApplication>\n );\n};\n"],"mappings":";;AACA,SAAS,iBAAiB,0BAA0B;AACpD,SAAS,iBAAiB;AAC1B,SAAS,cAAc;AACvB,OAAO,aAAa;AACpB,SAAS,iBAAiB;AAC1B,OAAO,iBAAiB;AACxB,OAAO,SAAS,WAAW,qBAAqB;AAEhD,SAAS,2BAA2B;AACpC,SAAS,0BAA0B,2BAA2B;AAE9D,SAAS,qBAAqB;AAC9B,SAAS,qBAAqB;AAC9B,SAAS,kCAAkC;AAC3C,SAAS,uCAAuC;AAChD,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AACjC,SAAS,2BAA2B;AAMpC,MAAM,+BAA+B;AAAA,EAAQ,MAC3C,OAAO,6BAA6B,EAAE,KAAK,OAAK,EAAE,sBAAsB;AAC1E;AAEA,MAAM,0BAA0B,CAAC,UAAkC;AACjE,MAAI,iCAAiC;AACnC,UAAM,qBAAqB;AAAA,gCAAyC,YAAY,OAAO;AACvF,QAAI,UAAU,GAAG;AACf,aAAO,SAAS,kBAAkB;AAAA,IACpC,OAAO;AACL,aAAO,QAAQ;AAAA;AAAA,EAAyB,kBAAkB;AAAA;AAAA,CAAuB;AAAA,IACnF;AAAA,EACF;AAEA,QAAM,EAAE,+CAA+C,MAAM,SAAS,IAAI;AAC1E,QAAM,SAAS,UAAU;AACzB,QAAM,OAAO,iBAAiB;AAC9B,QAAM,UAAU,oBAAoB;AACpC,QAAM,CAAC,WAAW,eAAe,IAAI,cAAc;AAGnD,QAAM,WAAW,QAAQ,oBA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