-
Notifications
You must be signed in to change notification settings - Fork 394
feat(elements): Remove direct reliance on next and clerk-react #4064
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
Conversation
🦋 Changeset detectedLatest commit: 0967c8a The changes in this PR will be included in the next version bump. This PR includes changesets to release 15 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 |
cd8caf3
to
c5c5c9b
Compare
// TODO: remove reliance on next-specific variables here | ||
export const SIGN_IN_DEFAULT_BASE_PATH = safeAccess( | ||
() => process.env.CLERK_SIGN_IN_URL ?? process.env.NEXT_PUBLIC_CLERK_SIGN_IN_URL, | ||
'/sign-in', | ||
); | ||
export const SIGN_UP_DEFAULT_BASE_PATH = safeAccess( | ||
() => process.env.CLERK_SIGN_UP_URL ?? process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL, | ||
'/sign-up', | ||
); |
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 realistically should be coming from the clerk provider / core clerk object instead of deriving it from environment variables. To maintain compat, I've just made this pull the env vars in a non-erroring way.
// The version that Next added support for the window.history.pushState and replaceState APIs. | ||
// ref: https://nextjs.org/blog/next-14-1#windowhistorypushstate-and-windowhistoryreplacestate | ||
export const NEXT_WINDOW_HISTORY_SUPPORT_VERSION = '14.1.0'; | ||
|
||
/** | ||
* Clerk router integration with Next.js's router. | ||
*/ | ||
export const useNextRouter = (): ClerkHostRouter => { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
// eslint-disable-next-line react-hooks/rules-of-hooks -- The order doesn't differ between renders as we're checking the execution environment. | ||
const searchParams = typeof window === 'undefined' ? new URLSearchParams() : useSearchParams(); | ||
|
||
// The window.history APIs seem to prevent Next.js from triggering a full page re-render, allowing us to | ||
// preserve internal state between steps. | ||
const canUseWindowHistoryAPIs = | ||
typeof window !== 'undefined' && window.next && window.next.version >= NEXT_WINDOW_HISTORY_SUPPORT_VERSION; | ||
|
||
return { | ||
mode: 'path', | ||
name: 'NextRouter', | ||
push: (path: string) => router.push(path), | ||
replace: (path: string) => | ||
canUseWindowHistoryAPIs ? window.history.replaceState(null, '', path) : router.replace(path), | ||
shallowPush(path: string) { | ||
canUseWindowHistoryAPIs ? window.history.pushState(null, '', path) : router.push(path, {}); | ||
}, | ||
pathname: () => pathname, | ||
searchParams: () => searchParams, | ||
}; | ||
}; |
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.
I've shifted the useNextRouter()
hook from elements into the Next SDK.
router?: ClerkHostRouter; | ||
}) { | ||
const clerkRouter = createClerkRouter(router, basePath); | ||
const hostRouter = useClerkHostRouter(); |
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.
pull host router by default from the provider.
"@clerk/clerk-react": "^5.0.0", | ||
"@clerk/shared": "^2.0.0", | ||
"next": "^13.5.4 || ^14.0.3 || ^15.0.0-rc", |
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.
🔪
Description
Removes the direct dependency and reliance on next and clerk-react from
@cerk/elements
. This is a step to allowing elements to render in clerk-js. Also introducesClerkHostRouterContext
into@clerk/next
'sClerkProvider
. Elements will pull the router from the context.Checklist
npm test
runs as expected.npm run build
runs as expected.Type of change