destroy: Shut down the SDK
destroy(): voidRemoves all banners and shuts down the SDK.
- When to call: If the code that calls
init()runs again within the same page (for example, when you initialize inside a component), call it before the nextinit()or in that component's cleanup. It is not needed if you initialize only once per page load (Deciding whether to calldestroy()). - What methods do when called after
destroy()is in Calling methods afterdestroy().
Exceptions and failures
- Exceptions: None.
What keeps running, and when it stops
init() and destroy() are used as a pair, like addEventListener() and removeEventListener(), or setInterval() and clearInterval(). What init() starts keeps running until you call destroy() or the page (document) is discarded.
The CheckoutPlusSession and its banners. One CheckoutPlusSession can show any number of banners.
| What starts | Starts when | Stops when |
|---|---|---|
| Showing the banner | mount(target) | unmount(), destroy(), the page is discarded |
Monitoring whether the previous screen is shown as is (a pageshow event listener) | init() | destroy(), the page is discarded |
Calling callbacks (references to the SDK user's functions, including onError) | init() | destroy(), the page is discarded |
| The shopper's selection (the checkbox state that shows whether to add the Checkout+ product) | init() | destroy(), the page is discarded |
| Cart updates in progress | mount(), refresh(), prepareCheckout() | When they complete (results that come in after destroy() are discarded) |
Deciding whether to call destroy()
Whether the CheckoutPlusSession stops automatically depends on how your screens change.
- When the shopper moves with a full page load (regular links,
location.assign(), and so on), the browser discards the page, so theCheckoutPlusSessionstops too.destroy()is not needed. - In SPA navigation (React Router or Next.js links, and so on), the page (document) stays. The
CheckoutPlusSessionkeeps running even when the screen changes.
Lifetimes of the page and CheckoutPlusSession objects. As in B, calling init() twice within the same page leaves two CheckoutPlusSession objects running.
There is only one thing to check. Can the code that calls init() run again within the same page?
Where you call init() | Does it run again within the same page? | Where to call destroy() | Figure above |
|---|---|---|---|
| A place that runs only once per page load (a script that runs on page load, or code that runs only once at the top level of an SPA app) | No | Don't call it | A, D |
Inside a component (React useEffect, Vue onMounted, and so on) | Yes | In the cleanup (the return value of useEffect, or onBeforeUnmount) | C |
Code that recreates the CheckoutPlusSession with different values passed to init() (locale, displaySettings, and so on) | Yes | Before calling init() again | — |
When you initialize inside a component, init() runs each time the component is shown. Under React StrictMode in development, it runs twice in a row.
If you initialize inside a component that opens and closes, such as a cart drawer, the box the shopper checked is cleared each time it closes. Initialize on the page, and only call mount() and unmount() in the drawer.
A pattern that does not need destroy(): initialize once in the app
// A pattern that does not need destroy(): initialize once at the top level of the app and keep using it
// (for example, an SPA store with a cart drawer on every page)
let starting: Promise<CheckoutPlusSession | null> | undefined
// Call from code that runs in the browser. With the Next.js App Router, call it from a component marked 'use client'
// Where the banner is shown, wait for the returned Promise, then call mount on that session
export function startCheckoutPlus(): Promise<CheckoutPlusSession | null> {
// Wait for the SDK to finish loading. After the time limit, this is null (lib/checkout-plus.ts in 4-1, Step 1)
starting ??= loadCheckoutPlus().then((sdk) => {
if (sdk === null) return null // If the SDK can't load, the store works without Checkout+
const checkoutPlus = sdk.init({
storeId: '(store ID)', // Store ID ("Development Shopify store and store ID" in the integration steps)
cart: cartConnector, // Your CartConnector (Step 2 of the integration steps)
onCheckoutPlusLinesChange: () => { /* Fetch your cart again */ },
})
void checkoutPlus.refresh() // Once, right after initialization
return checkoutPlus
})
return starting // Initializes only once, however many times it is called
}
// For banners, just mount where you show them and unmount when they are no longer neededA pattern that needs destroy(): initialize inside a component
If you initialize inside React's useEffect, always call destroy() in the cleanup. In development, React (StrictMode) runs initialization, cleanup, and initialization in a row. If the cleanup shuts the SDK down, two SDKs never run at the same time.
// A pattern that needs destroy(): initialize inside a component (React useEffect)
useEffect(() => {
let checkoutPlus: CheckoutPlusSession | null = null
let cancelled = false
// Wait for the SDK to finish loading. After the time limit, this is null (lib/checkout-plus.ts in 4-1, Step 1)
void loadCheckoutPlus().then((sdk) => {
if (sdk === null || cancelled) return // Don't initialize if the SDK can't load, or if the component was discarded while waiting
checkoutPlus = sdk.init({
storeId: '(store ID)', // Store ID ("Development Shopify store and store ID" in the integration steps)
cart: cartConnector, // Your CartConnector (Step 2 of the integration steps)
locale,
onCheckoutPlusLinesChange: () => { /* Fetch your cart again */ },
})
void checkoutPlus.refresh() // Once, right after initialization
setCheckoutPlus(checkoutPlus) // Pass it to the components that show the banner, through state or context
})
// Shut down when the component is discarded, and when it is recreated because locale changed
return () => {
cancelled = true
checkoutPlus?.destroy()
setCheckoutPlus(null)
}
}, [locale])What destroy() does
What stops is in the table in What keeps running, and when it stops. In addition, it behaves as follows.
- If it is called during the first cart fetch, the cart is not updated after that.
- A
prepareCheckout()call in progress completes immediately with{ attached: false }without waiting for the timeout. Your code can navigate to checkout as usual. - It does not change the cart. Even if a Checkout+ product line remains, it is not removed. A leftover line is removed on the first fetch after the next
init().
Calling methods after destroy()
No method throws. To use the SDK again, create a new CheckoutPlusSession with init() instead of reusing the same one.
| Method | After destroy() |
|---|---|
mount(target) | Renders nothing and returns an unmount() that does nothing |
refresh() | Does nothing and completes immediately |
prepareCheckout() | Completes immediately with { attached: false } |
isCheckoutPlusLine(line) | Works as before (it makes no network requests) |
destroy() (second call) | Does nothing |