Skip to content

Commit dc0e1c3

Browse files
authored
chore(astro,clerk-js): Inject windowNavigate through router functions (#3922)
1 parent dc94c08 commit dc0e1c3

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

.changeset/sixty-bikes-visit.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@clerk/clerk-js": minor
3+
"@clerk/astro": minor
4+
"@clerk/types": minor
5+
---
6+
7+
Inject `windowNavigate` through router functions.

packages/astro/src/internal/create-clerk-instance.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,22 @@ import { runOnce } from './run-once';
99

1010
let initOptions: ClerkOptions | undefined;
1111

12-
// TODO-SHARED: copied from `clerk-js`
13-
export const CLERK_BEFORE_UNLOAD_EVENT = 'clerk:beforeunload';
14-
1512
setClerkJsLoadingErrorPackageName(PACKAGE_NAME);
1613

17-
function windowNavigate(to: URL | string): void {
18-
const toURL = new URL(to, window.location.href);
19-
window.dispatchEvent(new CustomEvent(CLERK_BEFORE_UNLOAD_EVENT));
20-
window.location.href = toURL.href;
21-
}
22-
2314
function createNavigationHandler(
2415
windowNav: typeof window.history.pushState | typeof window.history.replaceState,
2516
): Exclude<ClerkOptions['routerPush'], undefined> | Exclude<ClerkOptions['routerReplace'], undefined> {
26-
return (to, metadata) => {
27-
if (metadata?.__internal_metadata?.navigationType === 'internal') {
17+
return (to, opts) => {
18+
if (opts?.__internal_metadata?.navigationType === 'internal') {
2819
windowNav(history.state, '', to);
2920
} else {
30-
windowNavigate(to);
21+
opts?.windowNavigate(to);
3122
}
3223
};
3324
}
3425

3526
/**
36-
* Prevents firing clerk.load multiple times
27+
* Prevents firing clerk.load() multiple times
3728
*/
3829
const createClerkInstance = runOnce(createClerkInstanceInternal);
3930

packages/clerk-js/src/core/__tests__/clerk.redirects.test.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,34 +124,30 @@ describe('Clerk singleton - Redirects', () => {
124124

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

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

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

149146
it('redirects to signUpUrl for production instance', async () => {
150147
await clerkForProductionInstance.redirectToSignUp({ redirectUrl: '/example' });
151-
expect(mockNavigate).toHaveBeenCalledWith(
152-
'/sign-up#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample',
153-
undefined,
154-
);
148+
expect(mockNavigate).toHaveBeenCalledWith('/sign-up#/?redirect_url=http%3A%2F%2Ftest.host%2Fexample', {
149+
windowNavigate: expect.any(Function),
150+
});
155151
});
156152

157153
it('redirects to userProfileUrl', async () => {

packages/clerk-js/src/core/__tests__/clerk.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,9 @@ describe('Clerk singleton', () => {
784784
await waitFor(() => {
785785
expect(mockSignUpCreate).not.toHaveBeenCalledWith({ transfer: true });
786786
expect(mockSetActive).not.toHaveBeenCalled();
787-
expect(mockNavigate).toHaveBeenCalledWith('/sign-in', undefined);
787+
expect(mockNavigate).toHaveBeenCalledWith('/sign-in', {
788+
windowNavigate: expect.any(Function),
789+
});
788790
});
789791
});
790792

packages/clerk-js/src/core/clerk.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,10 @@ export class Clerk implements ClerkInterface {
828828
return;
829829
}
830830

831-
const metadata = options?.metadata ? { __internal_metadata: options?.metadata } : undefined;
831+
const metadata = {
832+
...(options?.metadata ? { __internal_metadata: options?.metadata } : {}),
833+
windowNavigate,
834+
};
832835
// React router only wants the path, search or hash portion.
833836
return await customNavigate(stripOrigin(toURL), metadata);
834837
};

packages/types/src/clerk.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,10 @@ type NavigationType =
641641

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

644-
type RouterFn = (to: string, metadata?: { __internal_metadata?: RouterMetadata }) => Promise<unknown> | unknown;
644+
type RouterFn = (
645+
to: string,
646+
metadata?: { __internal_metadata?: RouterMetadata; windowNavigate: (to: URL | string) => void },
647+
) => Promise<unknown> | unknown;
645648

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

0 commit comments

Comments
 (0)