react-stripe-elements icon indicating copy to clipboard operation
react-stripe-elements copied to clipboard

CardElement not loading

Open mulholo opened this issue 7 years ago • 18 comments

Summary

Our card element does not load, but I can only reproduce this by refreshing and gettting the bug about 1 in 50 refreshes.

This does not appear to be a network error, as reported in https://github.com/stripe/react-stripe-elements/issues/409, and throttling does not reproduce the bug more swiftly. https://github.com/stripe/react-stripe-elements/issues/35 appears as if it might be related but I am unsure.

There are no console errors and window.Stripe is defined in the console.

I suspect this might have something to do with our SSR setup (with Next.js) but I am not entirely sure what. I have followed the SSR and async instructions in the README.

Other information

  • Occurs in both Chrome and Safari
  • There appear to be differing amounts of JS loaded in the successful (first) and unsuccessful (second) cases
    • image
    • image

Correctly Loaded

Screenshot 2019-09-04 at 14 21 21

Not Loaded

Screenshot 2019-09-04 at 14 21 11

mulholo avatar Sep 04 '19 13:09 mulholo

Hi @mulholio - this sounds frustrating! Maybe we can see if it's related to #35 as you said. I know you said you're not seeing console errors, but if possible, could you confirm that when this bug happens, Stripe.js is loaded and on window before the rendering happens? Or, if you're doing the async load as you said, ensure that its either loaded or the load handler fires?

Additionally, it might help to inspect the state of the element and check to see if the state provider is rigged up with a Stripe instance or not when this bug occurs, using the React dev tools to look at the state of the element.

This may be a race condition or mis-configured async load issue, but narrowing down the options can help us troubleshoot.

Thanks!

mbarrett-stripe avatar Sep 04 '19 14:09 mbarrett-stripe

could you confirm that when this bug happens, Stripe.js is loaded and on window before the rendering happens?

I'm using SSR so assume this isn't the case

Or, if you're doing the async load as you said, ensure that its either loaded or the load handler fires?

Dev tools info below appear to show the stripe script has loaded

Here's a screenshot of my dev tools for the injectStripe-wrapped component:

image

The stripe object appears to be there

Then, the page component higher up which tracks the loading state:

image

In the hooks section, state: true corresponds to the stripeHasLoaded variable and the object above it the Stripe object itself

mulholo avatar Sep 04 '19 15:09 mulholo

Hmm, and those screenshots are from an instance of the bug appearing?

Additionally, if you're able to provide an isolated reproduction case somewhere (possibly https://codesandbox.io/) that would help immensely. We will also attempt to reproduce this issue.

mbarrett-stripe avatar Sep 04 '19 21:09 mbarrett-stripe

Yep, those are screenshots from the bug instance. Will have a shot on a reproduction for you

mulholo avatar Sep 05 '19 08:09 mulholo

Haven't quite got the reproduction yet, but was getting some warnings about using useLayoutEffect on the server (we are currently running using a hookified version of the SSR code in the README but were getting this issue with componentDidMount() also) which made me wonder if this is because my code isn't running on the client at all and that's why we're not updating the stripe input element.

The code below, as well as in the README, has a if (typeof window !== 'undefined') { check to only run this code on the client. However, I think it might be possible that this component doesn't re-render on the client and so this code doesn't get run at all (even if it never runs on the client!)

function useStripe() {
  const [stripe, setStripe] = useState<any>(null);
  const [stripeScriptHasLoaded, setStripeScriptHasLoaded] = useState<
    boolean
  >(false);

  useLayoutEffect(() => {
    if (typeof window !== 'undefined') {
      // The script might have already loaded by the time this effect
      // runs so set the load state here
      setStripeScriptHasLoaded(!!window.Stripe);

      if (stripeScriptHasLoaded) {
        setStripe(window.Stripe(publicRuntimeConfig.STRIPE_PUBLIC_KEY));
      } else {
        const stripeScriptTag = document.querySelector('#stripe-js');

        // This shouldn't ever run because it's static in _document.tsx:
        if (!stripeScriptTag) {
          throw new Error('could not find #stripe-js script tag');
        }

        stripeScriptTag.addEventListener('load', () => {
          setStripeScriptHasLoaded(true);
        });
      }
    }
  }, [setStripe, setStripeScriptHasLoaded, stripeScriptHasLoaded]);

  return {
    stripe,
    stripeScriptHasLoaded,
  };
}

This blog post seems to suggest that componentDidMount does not run on server either https://jaketrent.com/post/react-componentdidmount-not-called-server-render/

The use of useLayoutEffect here is because useEffect runs on server and client and this code needs to run server side. It is also meant to be closer to componentDidMount, as used in the original code example. (See Kent C Dodds' post here for more info on useEffect vs useLayoutEffect)

Wondering if using Next's dynamic import would fix this (https://nextjs.org/docs#with-no-ssr).

mulholo avatar Sep 09 '19 15:09 mulholo

I had similar problems with components not loading today. It seems like there's an issue with getting responses for the requests made to stripe's servers.

Here's more details about my issue: https://github.com/stripe/react-stripe-elements/issues/35#issuecomment-544478819

tuomashatakka avatar Oct 21 '19 14:10 tuomashatakka

@mulholio @mbarrett-stripe Any solutions to this? Facing the same problem.

rishiraj824 avatar Dec 02 '19 21:12 rishiraj824

@mulholio @mbarrett-stripe Also had reports from customers that this is happening but unable to reproduce. In this case the expiry date and cvc elements are loading but the card number field is not. No errors are being reported.

mattlsavage avatar Dec 04 '19 11:12 mattlsavage

Hey all, thanks for the input. Any and all info you can provide around state capture, front end error logging, etc can help us figure this issue out. As of now we have not been able to reproduce the issue, so collecting more information on setup would be helpful, and a minimum viable recreation would be excellent

mbarrett-stripe avatar Dec 04 '19 15:12 mbarrett-stripe

Running into what seems to be the same, if not a very similar, issue. This is happening 100% of the time for me in all browsers.

The iframe loads, and I see requests to the elements endpoints. I see 2(?) inputs being created, one inside the iframe, one outside, but the cardelement has no styles, and cant be typed in.

There are no errors on the page.

I'm also using Next.JS, but the page its on is not SSRed.

Logged out props.stripe before it renders:

image

cryptiklemur avatar Jan 06 '20 00:01 cryptiklemur

Happening here: https://www-dgg50pjtd.now.sh/test

Theres logs available

cryptiklemur avatar Jan 06 '20 00:01 cryptiklemur

@aequasi - It appears that the page you linked is a CSS issue. If you look at the element inspector in your browser, you can see that the element is loaded, but is at 1px width. Adding a width style to the element makes it visible.

I'd encourage folks to check on their problematic pages as well and see if this is in fact an issue with the element being incorrectly styled and not just that it isn't loading. You can tell for sure if you can dig down into the iframe tag inside the mounted element HTML in your inspector.

We'll continue to dig into this, and are already looking into potential ways to make it easier to avoid this styling issue and what causes it in the first place.

mbarrett-stripe avatar Jan 07 '20 15:01 mbarrett-stripe

@mbarrett-stripe - We are encountering the same problem. In our case I am pretty sure the problem is not related with any CSS issue. When using client side rendering, everything works fine. However, when using SSR elements are not loaded if browser caches are enabled. Disabling the caches from network tab seems to make things work but it is not a practical solution.

I suspect it is related with some of the events being fired too early or not being fired at all when inner.html and outer.html are loaded from cache.

karaarda avatar Jan 23 '20 16:01 karaarda

@mbarrett-stripe I think we experiencing a similar issue. The page is not loading at all and the weird thing that it happens only for ios Screen Shot on 2020-01-28 at 12-53-30

orest22 avatar Jan 28 '20 17:01 orest22

Same problem here and I could fix it.

In my case the form is rendered inside a modal that it's not visible initially, and I don't get iframe at all. The element wrapper is 0x0.

There were more than one modal trying to render the stripe form in the same page at the initialization, so it was kind of a race condition problem between those modals.

Stop loading the stripe form until the user open the modal made the trick for me. I hope this can help somebody :)

arqex avatar Feb 12 '20 10:02 arqex

I'm experiencing this - no styles are loading and I get a completely non-functioning experience. I have literally copied and pasted the sample React code from the docs but it's (a) got errors (React is not imported and it has class= and for= both of which are wrong for React) and (b) even after correcting these errors it does not produce the result shown in the docs.

I am loading <script src="https://js.stripe.com/v3" async></script> in the head of the page.

But what I get is this - completely default, no styling, nothing works.

image

Updated: The docs have a class called form-row applied to the containing form, which conflicts with the Bootstrap forms CSS definition .form-row. If you are using Bootstrap and experience a styling issue, that might be the cause:

image

I just renamed the containing form class and set a min-width to the form, plus a bit of padding and white background and it works:

image

codewithfeeling avatar Jun 17 '20 21:06 codewithfeeling

this problem is fixed after migrating to react-stripe-js

karaarda avatar Jul 31 '20 09:07 karaarda

Thanks for the ideas in the thread, everybody. Closing this as this project has migrated to React Stripe.js. If you believe this is still important, please re-open it there.

sashko-stripe avatar Mar 26 '21 18:03 sashko-stripe