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

[BUG]: Elements not rendering at all after client-side navigation in Next.js #313

Closed
vincentleeuwen opened this issue Aug 14, 2022 · 5 comments
Labels
bug Something isn't working stale

Comments

@vincentleeuwen
Copy link

vincentleeuwen commented Aug 14, 2022

What happened?

TLDR: Stripe Elements is not rendering anything when using client side routing to checkout and gives no errors or clues what is wrong.

I'm trying to do something that I would imagine is a pretty standard use case in Next.js, but somehow I can't make it work. The elements are rendering fine when I hit refresh, but after a client side refresh they simply don't render at all.

Any idea what I'm doing wrong?

I have the following:

pages/checkout/pay.js

import stripe from 'stripe';

import Pay from '@components/Checkout/Pay';

const stripeApi = stripe(process.env.STRIPE_SECRET_API_KEY);

export async function getServerSideProps() {
  const amount = 1000;

  const stripeData = await stripeApi.paymentIntents.create({
    amount,
    currency: 'eur',
    automatic_payment_methods: { enabled: true },
  });

  return {
    props: {
      stripeSecret: stripeData.client_secret,
    },
  };
}

export default Pay;

/components/Pay.js:

const ElementsPay = ({ stripeSecret }) => {
  const options = {
    clientSecret: stripeSecret,
  };

  return (
    <Elements stripe={stripePromise} options={options}>
      <Pay />
    </Elements>
  );
};

const Pay = () => {
  return (
    <div style={{ height: 500 }}>
      <h1>Why no elements rendered?</h1>
      <PaymentElement />
    </div>
  );
};

Then on /pages/index.js, I put:

import { useRouter } from 'next/router';

const App = () => {
  const router = useRouter();

  const goToCheckout = () => {
    router.push(
      '/pay',
    );
  };

  return (
    <button onClick={goToCheckout}>Go to checkout</button>
  )
}

When I use the button, I see the following:

Only after a refresh, the stripe elements form wil load, but on a client side navigation it always fails with no error message whatsoever.

Client side nav:

Screenshot 2022-08-14 at 12 23 11

After refresh:

Screenshot 2022-08-14 at 12 23 20

Environment

No response

Reproduction

No response

@vincentleeuwen vincentleeuwen added the bug Something isn't working label Aug 14, 2022
@daneburns
Copy link

I'm unsure if you're on the latest version (1.10 at the time of writing), but this problem was solved for me by updating to the latest version. I believe it was caused by a previous version using useLayoutEffect which does not play nice with Next.js

@vincentleeuwen
Copy link
Author

@daneburns Thanks for responding! Unfortunately I still have the same issue after upgrading to 1.10.0. Which next version are you using?

@stale
Copy link

stale bot commented Sep 15, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 15, 2022
@jhkersul
Copy link

@vincentleeuwen did you solve this problem? 🤔

@vincentleeuwen
Copy link
Author

@jhkersul yeah I think the problem was that i was loading in stripe library client side, since the routing was client side and stripe was imported first time on that page. I think i read somewhere (had a look but can't find now) that the stripe lib has to be loaded from their servers for it to work properly. In Next terms, this means (I think) to load it on the first (server-side) render of _app.js. I moved it there and added the stripePromise to context to be fetched later when user navigates to /pay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

3 participants