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

lazy load script from cdn #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
34 changes: 15 additions & 19 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />

<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>

<title>React App</title>
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script src="https://cdn.checkout.com/js/framesv2.min.js"></script>
</body>

</html>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
22 changes: 19 additions & 3 deletions src/frames/Frames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ export class Frames extends Component<FramesProps> {
componentDidMount(): void {
const existingScript = document.querySelector(`script[src$="${CDN}"]`);
if (!existingScript) {
console.error(
`Checkout.com CDN not present. Perhaps you forgot to add <script src="${CDN}"></script> to your index.html file.`
);
(async () => {
try {
await this.loadScript();
this.initializeFrames();
} catch (error: any) {
throw new Error(error);
}
})();
} else {
this.initializeFrames();
}
Expand All @@ -41,6 +46,17 @@ export class Frames extends Component<FramesProps> {
return true;
}

loadScript = () => {
if (typeof window === 'undefined') return; // Don't execute this function if it's rendering on server side
return new Promise((resolve, reject) => {
const scriptTag = document.createElement('script');
scriptTag.src = CDN;
scriptTag.onload = (ev) => resolve(ev);
scriptTag.onerror = (err) => reject(err);
document.body.appendChild(scriptTag);
});
};

initializeFrames = () => {
let config = {
publicKey: this.props.config.publicKey,
Expand Down