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

[DOCS] Possible race condition on clean Shopify Checkout initialization #103

Closed
mnlmaier opened this issue Jan 9, 2022 · 5 comments
Closed

Comments

@mnlmaier
Copy link

mnlmaier commented Jan 9, 2022

Hi @jackabox,

thanks for your nice work on the plugin. Using it regularly! :)

I noticed a possible race condition which I was consistently able to reproduce in one of my shops where I followed the Frontend JS Snippets included in the docs. It's easy to reproduce: Just delete the LocalStorage key and refresh the Shop Overview. It's not initializing, still, the checkoutId is correctly set in Local Storage.

After another refresh (ie after the CheckoutId has been stored in LS), the Shop starts working (as is expected).

The culprit seems to be this snippet in the frontend:

if (!shopifyCheckout) {
    client.checkout.create().then(checkout => {
      localStorage.setItem('statamic.shopify.cart.id', checkout.id)
      shopifyCheckout = checkout.id
    })
  }

I have re-written this to the following:

if (!shopifyCheckout) {
    const { id } = await client.checkout.create()
    localStorage.setItem('statamic.shopify.cart.id', id)
    shopifyCheckout = id
}

and obviously adjusted some imports. Now everything is working as expected. This is caused by the script not properly waiting for the checkout init, which is an asynchronous task.

Am I the only person with this issue? If somebody can reproduce, I will gladly clean this up and turn this into a PR for @jackabox to have a closer look at.

Have a nice rest of the weekend :)

@mnlmaier mnlmaier changed the title Probable race condition on clean Shopify Checkout initialization Possible race condition on clean Shopify Checkout initialization Jan 9, 2022
@mnlmaier mnlmaier changed the title Possible race condition on clean Shopify Checkout initialization [DOCS] Possible race condition on clean Shopify Checkout initialization Jan 9, 2022
@klickreflex
Copy link

I've had the issue of the cart not being emptied after successful checkout, and I think there was a JS error thrown mentioning something about shopifyCheckout. Could this be related?

@jackabox
Copy link
Collaborator

@mnlmaier Thanks for this, feel free to open a PR and I'll get it merged in at the next changes 👍🏻

@klickreflex
Copy link

PS: I coud reproduce this now, PR would be appreciated.

@jackabox
Copy link
Collaborator

Added this into the core. If you've published the JS to make changes, you may need to adjust your JS (checkout.js) as needed.

@mnlmaier
Copy link
Author

awesome! thanks a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants