Skip to content

Conversation

brkalow
Copy link
Member

@brkalow brkalow commented Aug 30, 2024

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 introduces ClerkHostRouterContext into @clerk/next's ClerkProvider. Elements will pull the router from the context.

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:

Copy link

changeset-bot bot commented Aug 30, 2024

🦋 Changeset detected

Latest commit: 0967c8a

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

This PR includes changesets to release 15 packages
Name Type
@clerk/elements Minor
@clerk/nextjs Minor
@clerk/shared Minor
@clerk/astro Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/clerk-expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/clerk-sdk-node Patch
@clerk/tanstack-start Patch
@clerk/testing 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

@brkalow brkalow marked this pull request as ready for review September 4, 2024 15:44
@brkalow brkalow force-pushed the brk.feat/elements-remove-deps branch from cd8caf3 to c5c5c9b Compare September 4, 2024 15:51
Comment on lines +7 to +15
// 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',
);
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 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.

Comment on lines +28 to +58
// 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,
};
};
Copy link
Member Author

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();
Copy link
Member Author

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.

Comment on lines -97 to -99
"@clerk/clerk-react": "^5.0.0",
"@clerk/shared": "^2.0.0",
"next": "^13.5.4 || ^14.0.3 || ^15.0.0-rc",
Copy link
Member Author

Choose a reason for hiding this comment

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

🔪

@brkalow brkalow merged commit 95ac67a into main Sep 4, 2024
20 checks passed
@brkalow brkalow deleted the brk.feat/elements-remove-deps branch September 4, 2024 20:35
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.

3 participants