Skip to content

Commit 5dec81d

Browse files
authored
Merge pull request #393 from topcoder-platform/TCA-602_defer-auxiliary-scripts
TCA-602 - defer loading stripe.js until actually needed -> TCA-586
2 parents e1ef3d9 + 08fc4a8 commit 5dec81d

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src-ts/tools/work/work-self-service/intake-forms/review/Review.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { CardNumberElement, Elements, useElements, useStripe } from '@stripe/react-stripe-js'
2-
import { loadStripe, Stripe, StripeElements } from '@stripe/stripe-js'
2+
import { Stripe, StripeElements } from '@stripe/stripe-js'
3+
// we need to load this from submodule instead of root
4+
// @see: https://www.npmjs.com/package/@stripe/stripe-js # Importing loadStripe without side effects
5+
// tslint:disable-next-line:no-submodule-imports
6+
import { loadStripe } from '@stripe/stripe-js/pure'
37
import { Dispatch, FC, SetStateAction, useContext, useEffect, useState } from 'react'
48
import { toastr } from 'react-redux-toastr'
59
import { NavigateFunction, useNavigate, useParams } from 'react-router-dom'
@@ -269,14 +273,19 @@ const Review: FC = () => {
269273
)
270274
}
271275

272-
const stripePromise: Promise<Stripe | null> = loadStripe(EnvironmentConfig.STRIPE.API_KEY, {
273-
apiVersion: EnvironmentConfig.STRIPE.API_VERSION,
274-
})
276+
let stripePromise: Promise<Stripe | null | undefined>
275277

276-
const output: () => JSX.Element = () => (
277-
<Elements stripe={stripePromise}>
278-
<Review />
279-
</Elements>
280-
)
278+
const output: () => JSX.Element = () => {
279+
if (!stripePromise) {
280+
stripePromise = loadStripe(EnvironmentConfig.STRIPE.API_KEY, {
281+
apiVersion: EnvironmentConfig.STRIPE.API_VERSION,
282+
})
283+
}
284+
return (
285+
<Elements stripe={stripePromise as Promise<Stripe>}>
286+
<Review />
287+
</Elements>
288+
)
289+
}
281290

282291
export default output

src/routes/Review/index.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useNavigate } from "react-router-dom";
22
import { Elements, useElements, useStripe } from "@stripe/react-stripe-js";
3-
import { loadStripe } from "@stripe/stripe-js";
3+
import { loadStripe } from "@stripe/stripe-js/pure";
44
import { toastr } from "react-redux-toastr";
55
import React, { useEffect, useState } from "react";
66
import { connect, useDispatch, useSelector } from "react-redux";
@@ -40,9 +40,7 @@ import {
4040
import AboutYourProject from "./components/AboutYourProject";
4141
import styles from "./styles.module.scss";
4242

43-
const stripePromise = loadStripe(config.STRIPE.API_KEY, {
44-
apiVersion: config.STRIPE.API_VERSION,
45-
});
43+
let stripePromise;
4644

4745
/**
4846
* Review Page
@@ -303,6 +301,12 @@ const Review = ({
303301
};
304302

305303
const ReviewWrapper = (props) => {
304+
if (!stripePromise) {
305+
stripePromise = loadStripe(config.STRIPE.API_KEY, {
306+
apiVersion: config.STRIPE.API_VERSION,
307+
});
308+
}
309+
306310
return (
307311
<Elements stripe={stripePromise}>
308312
<Review {...props} />

src/routes/ReviewLegacy/index.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
22
import { connect, useDispatch, useSelector } from "react-redux";
33
import { useNavigate } from "react-router-dom";
44
import { toastr } from "react-redux-toastr";
5-
import { loadStripe } from "@stripe/stripe-js";
5+
import { loadStripe } from "@stripe/stripe-js/pure";
66
import { Elements, useElements, useStripe } from "@stripe/react-stripe-js";
77
import _ from "lodash";
88

@@ -43,9 +43,7 @@ import { Breadcrumb, OrderContractModal } from "../../../src-ts";
4343
import AboutYourProject from "../../routes/Review/components/AboutYourProject";
4444
import PageH2 from "../../components/PageElements/PageH2";
4545

46-
const stripePromise = loadStripe(config.STRIPE.API_KEY, {
47-
apiVersion: config.STRIPE.API_VERSION,
48-
});
46+
let stripePromise;
4947

5048
/**
5149
* Review Legacy Page
@@ -308,6 +306,12 @@ const ReviewLegacy = ({
308306
};
309307

310308
const ReviewWrapper = (props) => {
309+
if (!stripePromise) {
310+
stripePromise = loadStripe(config.STRIPE.API_KEY, {
311+
apiVersion: config.STRIPE.API_VERSION,
312+
});
313+
}
314+
311315
return (
312316
<Elements stripe={stripePromise}>
313317
<ReviewLegacy {...props} />

0 commit comments

Comments
 (0)