Skip to content
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

chore(astro,clerk-js): Inject windowNavigate through router functions #3922

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/sixty-bikes-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@clerk/clerk-js": minor
"@clerk/astro": minor
"@clerk/types": minor
---

Inject `windowNavigate` through router functions.
17 changes: 4 additions & 13 deletions packages/astro/src/internal/create-clerk-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ import { waitForClerkScript } from './utils/loadClerkJSScript';

let initOptions: ClerkOptions | undefined;

// TODO-SHARED: copied from `clerk-js`
export const CLERK_BEFORE_UNLOAD_EVENT = 'clerk:beforeunload';

function windowNavigate(to: URL | string): void {
const toURL = new URL(to, window.location.href);
window.dispatchEvent(new CustomEvent(CLERK_BEFORE_UNLOAD_EVENT));
window.location.href = toURL.href;
}

function createNavigationHandler(
windowNav: typeof window.history.pushState | typeof window.history.replaceState,
): Exclude<ClerkOptions['routerPush'], undefined> | Exclude<ClerkOptions['routerReplace'], undefined> {
return (to, metadata) => {
if (metadata?.__internal_metadata?.navigationType === 'internal') {
return (to, opts) => {
if (opts?.__internal_metadata?.navigationType === 'internal') {
windowNav(history.state, '', to);
} else {
windowNavigate(to);
opts?.windowNavigate(to);
}
};
}

/**
* Prevents firing clerk.load multiple times
* Prevents firing clerk.load() multiple times
*/
const createClerkInstance = runOnce(createClerkInstanceInternal);

Expand Down
28 changes: 12 additions & 16 deletions packages/clerk-js/src/core/__tests__/clerk.redirects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,30 @@ describe('Clerk singleton - Redirects', () => {

it('redirects to signInUrl for development instance', async () => {
await clerkForDevelopmentInstance.redirectToSignIn({ redirectUrl: '/example' });
expect(mockNavigate).toHaveBeenCalledWith(
'/sign-in#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample',
undefined,
);
expect(mockNavigate).toHaveBeenCalledWith('/sign-in#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample', {
windowNavigate: expect.any(Function),
});
});

it('redirects to signInUrl for production instance', async () => {
await clerkForProductionInstance.redirectToSignIn({ redirectUrl: '/example' });
expect(mockNavigate).toHaveBeenCalledWith(
'/sign-in#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample',
undefined,
);
expect(mockNavigate).toHaveBeenCalledWith('/sign-in#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample', {
windowNavigate: expect.any(Function),
});
});

it('redirects to signUpUrl for development instance', async () => {
await clerkForDevelopmentInstance.redirectToSignUp({ redirectUrl: '/example' });
expect(mockNavigate).toHaveBeenCalledWith(
'/sign-up#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample',
undefined,
);
expect(mockNavigate).toHaveBeenCalledWith('/sign-up#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample', {
windowNavigate: expect.any(Function),
});
});

it('redirects to signUpUrl for production instance', async () => {
await clerkForProductionInstance.redirectToSignUp({ redirectUrl: '/example' });
expect(mockNavigate).toHaveBeenCalledWith(
'/sign-up#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample',
undefined,
);
expect(mockNavigate).toHaveBeenCalledWith('/sign-up#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample', {
windowNavigate: expect.any(Function),
});
});

it('redirects to userProfileUrl', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,9 @@ describe('Clerk singleton', () => {
await waitFor(() => {
expect(mockSignUpCreate).not.toHaveBeenCalledWith({ transfer: true });
expect(mockSetActive).not.toHaveBeenCalled();
expect(mockNavigate).toHaveBeenCalledWith('/sign-in', undefined);
expect(mockNavigate).toHaveBeenCalledWith('/sign-in', {
windowNavigate: expect.any(Function),
});
});
});

Expand Down
5 changes: 4 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,10 @@ export class Clerk implements ClerkInterface {
return;
}

const metadata = options?.metadata ? { __internal_metadata: options?.metadata } : undefined;
const metadata = {
...(options?.metadata ? { __internal_metadata: options?.metadata } : {}),
windowNavigate,
};
// React router only wants the path, search or hash portion.
return await customNavigate(stripOrigin(toURL), metadata);
};
Expand Down
5 changes: 4 additions & 1 deletion packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,10 @@ type NavigationType =

type RouterMetadata = { routing?: RoutingStrategy; navigationType?: NavigationType };

type RouterFn = (to: string, metadata?: { __internal_metadata?: RouterMetadata }) => Promise<unknown> | unknown;
type RouterFn = (
to: string,
metadata?: { __internal_metadata?: RouterMetadata; windowNavigate: (to: URL | string) => void },
) => Promise<unknown> | unknown;

export type WithoutRouting<T> = Omit<T, 'path' | 'routing'>;

Expand Down
Loading