Skip to content

Commit

Permalink
use named export
Browse files Browse the repository at this point in the history
  • Loading branch information
dweedon-stripe committed Jan 6, 2020
1 parent 4633efa commit 85c32c0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 26 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Expand Up @@ -23,3 +23,4 @@ rules:
- ignoreRestSiblings: true
jest/no-disabled-tests: 2
jest/no-focused-tests: 2
import/prefer-default-export: 0
36 changes: 16 additions & 20 deletions README.md
Expand Up @@ -4,15 +4,15 @@ Import and use [Stripe.js](https://stripe.com/docs/stripe-js) as an ES module.
[![build status](https://img.shields.io/travis/stripe/stripe-js/master.svg?style=flat-square)](https://travis-ci.org/stripe/stripe-js)
[![npm version](https://img.shields.io/npm/v/@stripe/stripe-js.svg?style=flat-square)](https://www.npmjs.com/package/@stripe/stripe-js)

This project is a thin loading wraper around Stripe.js. Stripe.js cannot be
bundled with your code; it must be served from `https://js.stripe.com/v3`. This
module works by injecting the script for you.
Stripe.js cannot be bundled with your code; it must be served from
`https://js.stripe.com/v3`. This module injects the script for you and provides
a thin loading wraper around Stripe.js.

## Basic Usage

`Stripe` cannot be imported directly because the injected Stripe.js script loads
asynchronously. Use `loadStripe`, which will resolve once the Stripe.js script
is loaded.
`Stripe` cannot be imported directly because the injected Stripe.js script will
load asynchronously. Use `loadStripe`, which will resolve once the Stripe.js
script is loaded.

```js
import {loadStripe} from '@stripe/stripe-js';
Expand All @@ -27,38 +27,34 @@ test this code through your Stripe account.

## Ensuring Stripe.js is available everywhere

When imported, the Stripe.js module will asynchronously inject a script tag as a
side effect. To reduce fraud and costly chargebacks, make sure this script tag
is injected on every page. If you are utilizing code splitting, or if you do not
When imported, the Stripe.js module will inject an async script tag as a side
effect. To reduce fraud and costly chargebacks, make sure this script tag is
present on every page. If you are utilizing code splitting, or if you do not
include the code that imports `@stripe/stripe-js` on every page of your site,
then you will need to take extra steps to ensure Stripe.js is avaiable
everwhere.

### Code Splitting
### Import as a side effect

When using code splitting, import `@stripe/stripe-js` in your root module even
if you do not need to use Stripe.js there. This will make sure the Stripe.js
Import `@stripe/stripe-js` as a side effect in code that will be included
throughout your site (e.g. your root module). This will make sure the Stripe.js
script tag is injected right away.

```js
// Somewhere near the top of your root module
import '@stripe/stripe-js';
```

### Server rendered and static sites
### Manually include the script tag

If the code that imports `@stripe/stripe-js` is only included on part of your
site, ensure that Stripe.js loads on all pages by manually adding the Stripe.js
script tag to the `<head>` of your site.
Ensure that Stripe.js loads on all pages by manually adding the Stripe.js script
tag to the `<head>` of your site. When `loadStripe()` is called it will use the
existing script tag rather than injecting a new one.

```html
<!-- Somewhere in your site's <head> -->
<script src="https://js.stripe.com/v3" async></script>
```

The part of your site that calls `loadStripe()` will use the existing script tag
rather than injecting a new one.

## Stripe.js Documentation

- [Stripe.js Docs](https://stripe.com/docs/stripe-js)
Expand Down
2 changes: 1 addition & 1 deletion examples/parcel/src/index.js
@@ -1,5 +1,5 @@
import './styles.css';
import loadStripe from '../../../src';
import {loadStripe} from '../../../src';

(async () => {
// setup DOM
Expand Down
2 changes: 1 addition & 1 deletion examples/rollup/src/index.js
@@ -1,4 +1,4 @@
import loadStripe from '../../../src';
import {loadStripe} from '../../../src';

(async () => {
// setup DOM
Expand Down
2 changes: 1 addition & 1 deletion examples/webpack/src/index.js
@@ -1,5 +1,5 @@
import './styles.css';
import loadStripe from '../../../src';
import {loadStripe} from '../../../src';

(async () => {
// setup DOM
Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Expand Up @@ -46,6 +46,4 @@ const stripePromise = new Promise((resolve, reject) => {
});
});

const loadStripe = () => stripePromise;

export default loadStripe;
export const loadStripe = () => stripePromise;

0 comments on commit 85c32c0

Please sign in to comment.