Skip to content

Commit ae2d1a9

Browse files
authored
fix(clerk-js): Invalidate cached checkout (#5602)
1 parent 431a821 commit ae2d1a9

6 files changed

Lines changed: 30 additions & 9 deletions

File tree

.changeset/violet-jobs-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Bug fix: Invalidate cached checkout on checkout drawer unmount

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"files": [
33
{ "path": "./dist/clerk.js", "maxSize": "590kB" },
4-
{ "path": "./dist/clerk.browser.js", "maxSize": "73.64KB" },
4+
{ "path": "./dist/clerk.browser.js", "maxSize": "73.67KB" },
55
{ "path": "./dist/clerk.headless*.js", "maxSize": "55KB" },
66
{ "path": "./dist/ui-common*.js", "maxSize": "99KB" },
77
{ "path": "./dist/vendors*.js", "maxSize": "36KB" },

packages/clerk-js/src/core/modules/commerce/CommerceBilling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ export class __experimental_CommerceBilling implements __experimental_CommerceBi
6161
})
6262
)?.response as unknown as __experimental_CommerceCheckoutJSON;
6363

64-
return new __experimental_CommerceCheckout(json);
64+
return new __experimental_CommerceCheckout(json, orgId);
6565
};
6666
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export class __experimental_CommerceCheckout extends BaseResource implements __e
2727
subscription?: __experimental_CommerceSubscription;
2828
totals!: __experimental_CommerceTotals;
2929

30-
constructor(data: __experimental_CommerceCheckoutJSON) {
30+
constructor(data: __experimental_CommerceCheckoutJSON, orgId?: string) {
3131
super();
3232
this.fromJSON(data);
33+
this.pathRoot = orgId ? `/organizations/${orgId}/commerce/checkouts` : `/me/commerce/checkouts`;
3334
}
3435

3536
protected fromJSON(data: __experimental_CommerceCheckoutJSON | null): this {

packages/clerk-js/src/ui/components/Checkout/CheckoutPage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { __experimental_CheckoutProps, __experimental_CommerceCheckoutResource } from '@clerk/types';
2+
import { useEffect } from 'react';
23

34
import { Alert, Spinner } from '../../customizables';
45
import { useCheckout } from '../../hooks';
@@ -8,12 +9,16 @@ import { CheckoutForm } from './CheckoutForm';
89
export const CheckoutPage = (props: __experimental_CheckoutProps) => {
910
const { planId, planPeriod, subscriberType, onSubscriptionComplete } = props;
1011

11-
const { checkout, updateCheckout, isLoading } = useCheckout({
12+
const { checkout, isLoading, invalidate, updateCheckout } = useCheckout({
1213
planId,
1314
planPeriod,
1415
subscriberType,
1516
});
1617

18+
useEffect(() => {
19+
return invalidate;
20+
}, []);
21+
1722
const onCheckoutComplete = (newCheckout: __experimental_CommerceCheckoutResource) => {
1823
updateCheckout(newCheckout);
1924
onSubscriptionComplete?.();

packages/clerk-js/src/ui/hooks/useCheckout.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ export const useCheckout = (props: __experimental_CheckoutProps) => {
1010
const { organization } = useOrganization();
1111
const [currentCheckout, setCurrentCheckout] = useState<__experimental_CommerceCheckoutResource | null>(null);
1212

13-
const { data: initialCheckout, isLoading } = useFetch(__experimental_commerce?.__experimental_billing.startCheckout, {
14-
planId,
15-
planPeriod,
16-
...(subscriberType === 'org' ? { orgId: organization?.id } : {}),
17-
});
13+
const {
14+
data: initialCheckout,
15+
isLoading,
16+
invalidate,
17+
} = useFetch(
18+
__experimental_commerce?.__experimental_billing.startCheckout,
19+
{
20+
planId,
21+
planPeriod,
22+
...(subscriberType === 'org' ? { orgId: organization?.id } : {}),
23+
},
24+
undefined,
25+
'commerce-checkout',
26+
);
1827

1928
useEffect(() => {
2029
if (initialCheckout && !currentCheckout) {
@@ -30,5 +39,6 @@ export const useCheckout = (props: __experimental_CheckoutProps) => {
3039
checkout: currentCheckout || initialCheckout,
3140
updateCheckout,
3241
isLoading,
42+
invalidate,
3343
};
3444
};

0 commit comments

Comments
 (0)