Skip to content

Commit

Permalink
feat(clerk-js,types): Add a replaceNavigate prop to ClerkProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
desiprisg committed Jul 6, 2023
1 parent bafec05 commit 2e8a973
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
HandleOAuthCallbackParams,
InstanceType,
ListenerCallback,
NavigateOptions,
OrganizationInvitationResource,
OrganizationMembershipResource,
OrganizationProfileProps,
Expand Down Expand Up @@ -635,13 +636,14 @@ export default class Clerk implements ClerkInterface {
return unsubscribe;
};

public navigate = async (to: string | undefined): Promise<unknown> => {
public navigate = async (to: string | undefined, options?: NavigateOptions): Promise<unknown> => {
if (!to || !inBrowser()) {
return;
}

const toURL = new URL(to, window.location.href);
const customNavigate = this.#options.navigate;
const customNavigate =
options?.replace && this.#options.replaceNavigate ? this.#options.replaceNavigate : this.#options.navigate;

if (toURL.origin !== window.location.origin || !customNavigate) {
windowNavigate(toURL);
Expand Down
8 changes: 4 additions & 4 deletions packages/clerk-js/src/ui/router/PathRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NavigateOptions } from '@clerk/types';
import React from 'react';

import { hasUrlInFragment, mergeFragmentIntoUrl, stripOrigin } from '../../utils';
Expand All @@ -18,12 +19,12 @@ export const PathRouter = ({ basePath, preservedParams, children }: PathRouterPr
throw new Error('Clerk: Missing navigate option.');
}

const internalNavigate = (toURL: URL | string | undefined) => {
const internalNavigate = (toURL: URL | string | undefined, options?: NavigateOptions) => {
if (!toURL) {
return;
}
// Only send the path
return navigate(stripOrigin(toURL));
return navigate(stripOrigin(toURL), options);
};

const getPath = () => {
Expand All @@ -38,8 +39,7 @@ export const PathRouter = ({ basePath, preservedParams, children }: PathRouterPr
const convertHashToPath = async () => {
if (hasUrlInFragment(window.location.hash)) {
const url = mergeFragmentIntoUrl(new URL(window.location.href));
window.history.replaceState(window.history.state, '', url.href);
await internalNavigate(url.href); // make this navigation as well since replaceState is asynchronous
await internalNavigate(url.href, { replace: true });
setStripped(true);
}
};
Expand Down
7 changes: 6 additions & 1 deletion packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,15 @@ export type BuildUrlWithAuthParams = {

// TODO: Make sure Isomorphic Clerk navigate can work with the correct type:
// (to: string) => Promise<unknown>
export type CustomNavigation = (to: string) => Promise<unknown> | void;
export type CustomNavigation = (to: string, options?: NavigateOptions) => Promise<unknown> | void;

export type ClerkThemeOptions = DeepSnakeToCamel<DeepPartial<DisplayThemeJSON>>;

export interface ClerkOptions {
appearance?: Appearance;
localization?: LocalizationResource;
navigate?: (to: string) => Promise<unknown> | unknown;
replaceNavigate?: (to: string) => Promise<unknown> | unknown;
polling?: boolean;
selectInitialSession?: (client: ClientResource) => ActiveSessionResource | null;
/** Controls if ClerkJS will load with the standard browser setup using Clerk cookies */
Expand All @@ -500,6 +501,10 @@ export interface ClerkOptions {
isSatellite?: boolean | ((url: URL) => boolean);
}

export interface NavigateOptions {
replace?: boolean;
}

export interface Resources {
client: ClientResource;
session?: ActiveSessionResource | null;
Expand Down

0 comments on commit 2e8a973

Please sign in to comment.