Skip to content

Commit 97b03f2

Browse files
authored
Fix preloading in sample app (#353)
1 parent aa71c6d commit 97b03f2

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

sample/src/App.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ function AppWithContext({children}: PropsWithChildren) {
235235
config={{
236236
colorScheme:
237237
checkoutKitConfigDefaults.colorScheme ?? ColorScheme.automatic,
238-
enablePreloading: checkoutKitConfigDefaults.preloading ?? true,
239238
prefillBuyerInformation: false,
240239
customerAuthenticated: false,
241240
}}>
@@ -364,7 +363,6 @@ function AppWithCheckoutKit({children}: PropsWithChildren) {
364363
return {
365364
...checkoutKitConfigDefaults,
366365
...checkoutKitThemeConfig,
367-
preloading: appConfig.enablePreloading,
368366
acceleratedCheckouts: {
369367
storefrontDomain: env.STOREFRONT_DOMAIN!,
370368
storefrontAccessToken: env.STOREFRONT_ACCESS_TOKEN!,

sample/src/context/Cart.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
111111
}
112112
}, [cartId, fetchCart, setTotalQuantity]);
113113

114+
const preloadCheckout = useCallback(
115+
async (checkoutURL: string) => {
116+
if (checkoutURL) {
117+
const config = await shopify.getConfig();
118+
if (config?.preloading) {
119+
shopify.preload(checkoutURL);
120+
}
121+
}
122+
},
123+
[shopify],
124+
);
125+
114126
const addToCart = useCallback(
115127
async (variantId: string) => {
116128
let id = cartId;
@@ -140,7 +152,7 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
140152
setTotalQuantity(data.cartLinesAdd.cart.totalQuantity);
141153

142154
if (data.cartLinesAdd.cart.checkoutUrl) {
143-
shopify.preload(data.cartLinesAdd.cart.checkoutUrl);
155+
await preloadCheckout(data.cartLinesAdd.cart.checkoutUrl);
144156
}
145157

146158
if (id) {
@@ -159,8 +171,8 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
159171
appConfig,
160172
createCart,
161173
setCartId,
162-
shopify,
163174
fetchCart,
175+
preloadCheckout,
164176
],
165177
);
166178

@@ -183,7 +195,7 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
183195
setTotalQuantity(data.cartLinesRemove.cart.totalQuantity);
184196

185197
if (checkoutURL) {
186-
shopify.preload(checkoutURL);
198+
await preloadCheckout(checkoutURL);
187199
}
188200

189201
if (cartId) {
@@ -202,7 +214,7 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
202214
setCheckoutURL,
203215
setTotalQuantity,
204216
checkoutURL,
205-
shopify,
217+
preloadCheckout,
206218
fetchCart,
207219
],
208220
);

sample/src/context/Config.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {useTheme} from './Theme';
1111

1212
export interface AppConfig {
1313
colorScheme: ColorScheme;
14-
enablePreloading: boolean;
1514
prefillBuyerInformation: boolean;
1615
customerAuthenticated: boolean;
1716
}
@@ -23,7 +22,6 @@ interface Context {
2322

2423
const defaultAppConfig: AppConfig = {
2524
colorScheme: ColorScheme.automatic,
26-
enablePreloading: true,
2725
prefillBuyerInformation: false,
2826
customerAuthenticated: false,
2927
};
@@ -45,6 +43,9 @@ export const ConfigProvider: React.FC<
4543
}, [config, setColorScheme]);
4644

4745
const setAppConfig = useCallback((config: AppConfig) => {
46+
console.groupCollapsed('APP CONFIG UPDATE');
47+
console.log(config);
48+
console.groupEnd();
4849
setInternalAppConfig(config);
4950
}, []);
5051

@@ -56,10 +57,6 @@ export const ConfigProvider: React.FC<
5657
[appConfig, setAppConfig],
5758
);
5859

59-
console.groupCollapsed('APP CONFIG UPDATE');
60-
console.log(appConfig);
61-
console.groupEnd();
62-
6360
return (
6461
<ConfigContext.Provider value={value}>{children}</ConfigContext.Provider>
6562
);

sample/src/screens/SettingsScreen.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
2323

24-
import React, {useCallback, useMemo} from 'react';
24+
import React, {useCallback, useMemo, useEffect, useState} from 'react';
2525
import {
2626
Pressable,
2727
SafeAreaView,
@@ -87,11 +87,20 @@ interface SectionData {
8787
}
8888

8989
function SettingsScreen() {
90-
const ShopifyCheckout = useShopifyCheckoutSheet();
90+
const shopify = useShopifyCheckoutSheet();
9191
const {clearCart} = useCart();
9292
const {appConfig, setAppConfig} = useConfig();
9393
const {colors, setColorScheme} = useTheme();
9494
const styles = createStyles(colors);
95+
const [preloadingEnabled, setPreloadingEnabled] = useState(false);
96+
97+
useEffect(() => {
98+
async function loadConfig() {
99+
const config = await shopify.getConfig();
100+
setPreloadingEnabled(config?.preloading ?? false);
101+
}
102+
loadConfig();
103+
}, [shopify]);
95104

96105
const handleColorSchemeChange = useCallback(
97106
(item: SingleSelectItem) => {
@@ -104,12 +113,15 @@ function SettingsScreen() {
104113
[appConfig, setAppConfig, setColorScheme],
105114
);
106115

107-
const handleTogglePreloading = useCallback(() => {
108-
setAppConfig({
109-
...appConfig,
110-
enablePreloading: !appConfig.enablePreloading,
116+
const handleTogglePreloading = useCallback(async () => {
117+
const currentConfig = await shopify.getConfig();
118+
const newPreloadingValue = !currentConfig?.preloading;
119+
shopify.setConfig({
120+
...currentConfig,
121+
preloading: newPreloadingValue,
111122
});
112-
}, [appConfig, setAppConfig]);
123+
setPreloadingEnabled(newPreloadingValue);
124+
}, [shopify]);
113125

114126
const handleTogglePrefill = useCallback(() => {
115127
clearCart();
@@ -132,7 +144,7 @@ function SettingsScreen() {
132144
{
133145
title: 'Preload checkout',
134146
type: SectionType.Switch,
135-
value: appConfig.enablePreloading,
147+
value: preloadingEnabled,
136148
handler: handleTogglePreloading,
137149
},
138150
{
@@ -152,6 +164,7 @@ function SettingsScreen() {
152164
],
153165
[
154166
appConfig,
167+
preloadingEnabled,
155168
handleTogglePrefill,
156169
handleTogglePreloading,
157170
handleToggleCustomerAuthenticated,
@@ -193,7 +206,7 @@ function SettingsScreen() {
193206
{
194207
title: 'SDK version',
195208
type: SectionType.Text,
196-
value: ShopifyCheckout.version,
209+
value: shopify.version,
197210
},
198211
{
199212
title: 'App version',
@@ -206,7 +219,7 @@ function SettingsScreen() {
206219
value: Config.STOREFRONT_DOMAIN || 'undefined',
207220
},
208221
],
209-
[ShopifyCheckout.version],
222+
[shopify.version],
210223
);
211224

212225
const sections: SectionData[] = useMemo(

0 commit comments

Comments
 (0)