Upgrade from Cloud SDK version 0.3 to 0.4
Cloud SDK version 0.4 introduces new initialization logic. If you're upgrading from version 0.3 or earlier, follow this walkthrough to upgrade your initialization code. The old, version 0.3 initialization logic is deprecated in version 0.4 and removed in version 0.5.
The majority of your upgrade work involves installing the latest Cloud SDK packages and replacing old init function calls with new CloudSDK ones. You'll also set the sc_{SitecoreEdgeContextId}_personalize cookie manually, which previous SDK versions set automatically if you opted in to setting cookies.
This walkthrough assumes that you have Cloud SDK version 0.3 or earlier installed in your JSS Next.js application.
This walkthrough describes how to:
- Install the latest @sitecore-cloudsdk packages
- Import the CloudSDK function and other SDK modules
- Rework the init function into CloudSDK code
- In your code editor, open the root folder of your web app.
Install the latest @sitecore-cloudsdk packages
@sitecore-cloudsdk packagesThe first step to upgrading is to install the latest @sitecore-cloudsdk packages, including a new package called core. The core package contains the new initialization logic. You'll set up the new logic in a later procedure.
To install the latest packages:
-
In your code editor, in the root folder of your application where your app dependencies are installed, open the terminal.
-
To install version 0.4 of the
corepackage and any other Cloud SDK packages you currently use in your app, run the following commands: -
In
package.json, check that the Cloud SDK packages are now version0.4.
Import the CloudSDK function and other SDK modules
CloudSDK function and other SDK modulesThe core package contains a browser and a server module. Each module contains a new CloudSDK function. This function initializes the SDK when you run its initialize method, and the function also allows you to initialize other SDK packages. The initialize method runs the initialization logic and sets cookies.
Depending on your application requirements, use the browser module to initialize the SDK on the browser side, or use the server module to initialize it on the server.
On the browser side, initialize the SDK once, in a centralized place, on app initialization. For example, in a Next.js App Router app, initialize the SDK in an Effect Hook in a component, then add that component to layout.tsx. This ensures that the initialization runs only once, centrally.
To make the necessary imports on the browser side:
-
In a centralized place, on app initialization, for example, in
components/CloudSDKComponent.tsx, importuseEffectfromreact,CloudSDKfrom thebrowsermodule of thecorepackage, and other SDK modules your app uses:
On the server side, initialize the SDK in every file that is intended to run Cloud SDK code. Depending on your application, these files might contain a page request, an API request, or a middleware request.
To make the necessary imports on the server side:
-
Depending on your server-side logic, in the files where you want to run Cloud SDK code, for example, in
middleware.ts, importNextRequestandNextResponsefromnext/server,CloudSDKfrom theservermodule of thecorepackage, and other SDK modules your app uses:
Rework the init function into CloudSDK code
init function into CloudSDK codeAfter making the necessary imports, your app still runs instances of the old init function, usually one for each Cloud SDK module your app uses. For example, if your app uses the events/browser, events/server , and personalize/browser modules, your application code contains three instances of init. In this procedure, you replace all init code with new code that uses the CloudSDK function.
On the browser side, you must replace all instances of init with a single instance of the CloudSDK function. You must run the CloudSDK function once, in a centralized place, on app initialization, so that the rest of your app can run Cloud SDK code.
To rework init into CloudSDK code on the browser side:
-
In your application code, find all instances of your
initcode.Example:
This code runs the old
initfunction to initializepersonalize/browser. -
Comment out all instances of
initcode from your application code, and remove allinitimports.For the code shown in step 1:
-
In the file where you previously imported
CloudSDKand other SDK modules, for example, inCloudSDKComponent.tsx, in an Effect hook, run theCloudSDKfunction to initialize the Cloud SDK:Replace the placeholder values with the required details you collected when you prepared for development.
This code runs the
CloudSDKfunction, which initializes the Cloud SDK using your Context ID and site name, and it sets cookies from the browser side. It also initializes theeventsand thepersonalizepackages, and it enables Sitecore Personalize web personalizations. If you don't want to enable web personalizations, removewebPersonalization: true.TipThis initialization logic uses the Dynamic Prototype Pattern.
The component is now ready to be used elsewhere in your app.
-
Add the component you just created to
layout.tsx, directly inside the<body>tag, before{children}, then save your changes:This code initializes the SDK once, on app initialization. Because this code runs in the app's root layout, the SDK code will be available globally, on all routes of your app.
-
Start your app by entering the following command in your terminal:
-
In your web browser, navigate to your JSS Next.js app, typically at
http://localhost:3000. -
In your web browser console, enter
window.scCloudSDK. If an object returns, you have successfully initialized the SDK.TipThis object lists all the packages you have initialized and their version. Use this in any future implementations to quickly verify that the SDK is correctly initialized.
-
Optionally, in your web browser's developer tools, in Cookies, find at least one cookie that starts with
sc_. This is another way to verify that you've successfully initialized the Cloud SDK.You can now run other browser-side SDK code in the rest of your app. For example, you can start setting up tracking by capturing
VIEWevents.
On the server side, you must replace each instance of init with an instance of the CloudSDK function. You must run the CloudSDK function on every page request, API request, or middleware request, depending on your server-side logic. The CloudSDK function has to run on every request because every request will have its own cookies. In addition, to ensure correct execution order, the CloudSDK initialization code has to run before any other Cloud SDK functions.
To rework init into CloudSDK code on the server side:
-
In your application code, find all instances of your
initcode.Example:
This code runs the old
initfunction to initializepersonalize/server. -
Comment out all instances of
initcode from your application code, and remove allinitimports.For the code shown in step 1:
-
In the file where you previously imported
CloudSDKand other SDK modules, for example, inmiddleware.ts, run theCloudSDKfunction to initialize the Cloud SDK:Replace the placeholder values with the required details you collected when you prepared for development.
This code runs the
CloudSDKfunction, which initializes the Cloud SDK using your Context ID and site name, and it sets cookies from the server side. It also initializes theeventsand thepersonalizepackages.TipThis initialization logic uses the Dynamic Prototype Pattern.
-
Start your app by entering the following command in your terminal:
-
In your web browser, navigate to your Next.js app, typically at
http://localhost:3000. -
In your web browser's developer tools, in Cookies, find at least one cookie that starts with
sc_. If such a cookie is present, you've successfully initialized the Cloud SDK.You can now run other server-side SDK code in this file. For example, you can start setting up tracking by capturing
VIEWevents.To run SDK code in other server-side files that handle page requests, API requests, or middleware requests, you'll have to import
CloudSDKand the other SDK package modules into those files, and you'll have to run theCloudSDKfunction and initialize the SDK packages, depending on your application requirements.
Next steps
You've now successfully upgraded to the Cloud SDK version 0.4 initialization logic. You can now start your app to check that the Cloud SDK runs. You can also remove the code related to the old init function that you have previously commented out.
Next, you can:
- Learn more about the SDK initialization process.
- Explore the reference documentation for the
corepackage. - Learn more about what changed in Cloud SDK version 0.4 in the release notes.