This guide helps you migrate your out-of-the-box B2C projects from version 1 to version 2 of the commercetools Frontend SDK. If you made custom changes to your project, you must also check if these need updates against the breaking changes.
For migrating B2B projects, see the migration guide for B2B projects.
Version 2 of the SDK contains breaking changes.
Migrate your project
To migrate your B2C project to version 2 of the Frontend SDK, follow these steps:
-
Update the
@commercetools/frontend-sdkpackage to the latest SDK version in one of the following ways:- Run the Yarn command:
yarn upgrade @commercetools/frontend-sdk. - Go to your project's
package.jsonfile and update the version number to the latest version listed on npm. Save the file, and then runyarn installto update theyarn.lockfile.
- Run the Yarn command:
-
Go to your project's
eventsfile atpackages/PROJECT_NAME/frontend/sdk/composable-commerce/types/events/ComposableCommerceEvents.ts. Add the following events to the exportedComposableCommerceEventstype in the file:productFetched: { product: unknown }; productsQueried: { query: unknown; result: unknown }; productCategoriesQueried: { query: unknown; result: unknown }; searchableProductAttributesFetched: { filterFields: unknown[] }; projectSettingsFetched: { projectSettings: unknown }; productAddedToCart: { product: unknown; quantity: number }; productRemovedFromCart: { product: unknown; quantity: number }; productUpdatedInCart: { product: unknown; newQuantity: number }; cartFetched: { cart: unknown }; cartUpdated: { account?: { email: string; }; shipping?: unknown; billing?: unknown; }; shippingMethodsFetched: { shippingMethods: unknown[] }; availableShippingMethodsFetched: { shippingMethods: unknown[] }; shippingMethodUpdated: { shippingMethod: unknown }; discountCodeRedeemed: { discountCode: string; cart?: unknown }; discountCodeRemoved: { discountCode: string; cart?: unknown }; cartCheckedOut: {}; accountFetched: { account: unknown }; userLoggedIn: { account: unknown }; userLoggedOut: {}; userRegistered: { account: unknown }; accountConfirmed?: { account: unknown }; accountConfirmationEmailRequested?: { email: string }; passwordChanged: {}; passwordResetRequested: {}; passwordReset: {}; accountUpdated: { account: unknown }; accountAddressAdded: { address: unknown }; accountAddressUpdated: { address: unknown }; accountAddressRemoved: { addressId: string }; defaultBillingAddressSet: { address: unknown }; defaultShippingAddressSet: { address: unknown }; wishlistFetched: { wishlist: unknown }; lineItemAddedToWishlist: { lineItem: unknown }; lineItemRemovedFromWishlist: { lineItemId: string }; wishlistLineItemUpdated: { lineItem: unknown };In version 1 of the SDK, this event was defined in the@commercetools/frontend-sdknpm package, in theStandardEvents.tsfile. -
In all
packages/PROJECT_NAME/frontend/sdk/composable-commerce/library/actions/*files, replaceskipQueuewithparallelin the options parameters of theSDK.callActionarguments. By default, all actions are executed asynchronously in version 2 of the SDK. To override this and queue actions for sequential execution, passparallel: falseto theSDK.callActionargument.Inpackages/PROJECT_NAME/frontend/frontastic/hooks/useProduct/index.ts, in theuseProducthook, remove theskipQueueparameter. Some older projects may not have this parameter. -
In
packages/PROJECT_NAME/frontend/sdk/composable-commerce/library/actions/AccountActions, replace therememberMeCookieAsyncimport withrememberMeCookie, and update all references to it.By default, all cookie management methods are asynchronous.
-
In
packages/PROJECT_NAME/frontend/sdk/composable-commerce/types/payloads/AccountPayloads.ts, change theUpdateAccountAddressPayloadtype to{ address: Address }. Then, update the following references:- In
packages/PROJECT_NAME/frontend/sdk/composable-commerce/library/actions/AccountActions.ts, within theupdateAddressfunction, replace thepayloadpassed in theaddressesAreEqualfunction topayload.address. - In
packages/PROJECT_NAME/frontend/frontastic/hooks/useAccount/index.ts, update theupdateAddresshook to pass the{ address }object. - In
packages/PROJECT_NAME/backend/commerce-commercetools/actionControllers/AccountController.ts, in theupdateAddressaction, change the response body deserialisation type to{ address: Address }and update further references if needed.
In version 2 of the Frontend SDK, you can no longer pass objects with payload types ofunknown. You can pass only AcceptedPayloadTypes objects. - In
-
If your project contains an extension for Adyen, complete this step. Otherwise, skip to step 7.
Inpackages/PROJECT_NAME/frontend/frontastic/hooks/useAdyen/index.ts, in themakePaymentmethod, wrap the payload argument in an object. Then, change the response body deserialisation type to reflect the change and update further references if needed. -
In
packages/PROJECT_NAME/frontend/types/index.d.ts, delete the following declarations:interface Page extends BasePage { sections: Record<string, Section>; } declare module '@commercetools/frontend-sdk/lib/types/api/page/PageResponse' { interface PageResponse { page: Page; pageFolder: PageFolder; data: PageViewData; } } -
In
packages/PROJECT_NAME/frontend/frontastic/renderer/index.tsx, add a non-null assertion operator to section errors. For example, updatesection.tosection!.in the sections mapping in the component return. This is to ensure compatibility and correct syntax for TypeScript. -
In
packages/PROJECT_NAME/frontend/app/[locale]/[[...slug]]/page.tsx, replace all of the file's content with the content in the latest version of thepage.tsxfile. -
If you have made custom updates to your project, check them against this guide and the list of changes and update any references or dependencies as needed.