Upgrade JSS 22.1 Angular apps to version 22.3 for SitecoreAI
The release of JSS 22.3 includes the first edition of an Angular starter kit for SitecoreAI. Using this kit and the underlying add-ons, Sitecore developers can now integrate their Angular apps with SitecoreAI to take advantage of features such as metadata editing in Pages, page personalization, component A/B/n testing, internationalization (i18n), and more.
This topic covers most of the changes you must make to your existing JSS 22.1 Angular applications for Experience Platform (XP) to integrate those apps with SitecoreAI. However, because of the nature of JavaScript and Angular application development, this topic doesn't account for all the customization you might have in your existing application.
A JSS 22.3 application built using the XM Cloud Angular starter kit uses the following templates and add-ons:
-
angular -
angular-xmcloud
You'll also need to include the Node XM Cloud proxy; the method for doing so is explained in this guide.
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
-
If you haven't already done so, upgrade your app to JSS 22.1.
-
Next, follow the 22.3 upgrade guide for XP, which contains changes required in addition to the SitecoreAI specific ones described in this topic. You can do this afterwards instead if you prefer.
-
Familiarize yourself with the changelog. If your application is heavily customized, the changelog can provide guidance on what additional changes you need that are not covered in this topic.
This topic describes how to:
Create a JSS 22.3 Angular application
To simplify the upgrade process as much as possible, create a JSS 22.3 Angular application with the XM Cloud add-on. You can then copy some files from this app into your existing app during the next section of the guide.
To create a JSS 22.3 Angular application with the XM Cloud add-on included:
-
In a console, run the following command:
-
If prompted to install the
[email protected]package, answer y. -
Enter the folder path for the JSS 22.3 Next.js app. For example, enter
./jss223, to create the app folder in your current working directory. -
When asked if you are building for Sitecore XM Cloud, answer y to install the XM Cloud add-on.
-
When asked where you want the JSS XM Cloud Node proxy app to be created, a subdirectory in the parent directory of your Angular app is suggested by default. We recommend you choose this option, as the proxy is best placed in a directory adjacent to your Angular app.
The script then installs the application dependencies.
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
Update the Angular template files in your existing app
This explains how to synchronize files in your existing application with corresponding files from your new JSS 22.3 app.
To update the Angular template files:
-
If your existing app's parent directory does not already contain a copy of the JSS XM Cloud Node proxy app, such as the one installed when you created an Angular starter app in the previous section, make a copy in that parent directory, such as:
-
Replace the contents of your app's
componentsfolder with those ofsrc/app/componentsfrom the sample app. -
If you’re using the plugins architecture for
generate-component-factoryandconfig, you need to pull the Angular XM Cloud specific plugins from theangular-xmcloudadd-on.XM Cloud is now SitecoreAISome code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
-
In
package.json, do the following:-
Update the
build:clientscript to explicitly use theproductionconfiguration, such as: -
Add dependencies for the Cloud SDK:
-
-
If you've not customized the
/scripts/bootstrap.tsfile, replace it with the 22.3 version. Otherwise, add the following statement to generate an integration for metadata-based editing. This assumes your app already has agenerate-metadata.tsfile from theangular-xmcloudadd-on: -
If you are not using plugins architecture for
generate-component-factory, make the following change:-
If you've not customized the
/scripts/generate-component-factory.tsfile, replace it with the 22.3 version. Otherwise, implement the following to generate a list of component names for metadata-based editing:
-
-
If you've not customized the
/src/app/lib/grapqhl-client-factory.tsfile, replace it with the/src/app/lib/grapqhl-client-factoryfolder from the 22.3 sample app. Otherwise, make the following changes:NoteThese changes are necessary to separate the GraphQL client factory configuration from the client factory itself, so you have a single source of GraphQL endpoint resolution that can be used in places such as
node-xmcloud-proxy,scripts/update-graphql-fragment-data.ts, and so on.-
Create a new folder named
/src/app/lib/grapqhl-client-factory. Within this folder, you need to restructure your existing/src/app/lib/grapqhl-client-factory.tsinto two new files namedconfig.tsandindex.ts. -
Your
/src/app/lib/graphql-client-factory/config.tsfile must expose thegetGraphQLClientFactoryConfigthat returns the configuration object for the GraphQL client factory. An example of this forconnectedanddevsetup might look like this:NoteFor a full overview of this, you can look at the full code snippet with Edge endpoint initialization in the the
angular-xmcloudadd-on. -
Your
/src/app/lib/graphql-client-factory/index.tsfile must contain the default export that returns the GraphQL client factory. For example: -
Make sure that variables are being imported from
@sitecore-jss/sitecore-jss-angular/cjs, and not@sitecore-jss/sitecore-jss-angular.
ImportantWhether you restructured your existing file or copied files from the sample app, update all references to the GraphQL client factory in your application to use this new structure.
-
-
If you've not customized the
/scripts/update-graphql-fragment-data.tsfile, replace it with the 22.3 version. Otherwise, make the following changes:-
Update the file to use the GraphQL client factory and client factory configuration. You can see an example of this in your 22.3 sample app.
-
Remove the
isomorphic-fetchimport statement, and remove the same dependency from yourpackage.json.
-
-
If you've not customized the
/scripts/generate-config.tsfile, replace it with the 22.3 version. Otherwise, add the following import statement: -
If you've not customized the
/server.bundle.tsfile, replace it with the 22.3 version. Otherwise, you need to expose new properties in one of the following two ways:-
If you want to expose these new properties in the existing file, add them there:
-
Alternatively, we recommend you create a separate
/server.exports.tsfile, which you can use to import and export these properties and then re-export them from/server.bundle.ts. For an example of how to implement this, refer to your new 22.3 sample app.
-
-
Angular JSS apps for SitecoreAI support only GraphQL for data fetching, so make sure that your app's
FETCH_WITHenvironment variable is set toGraphQLinstead ofREST. Alternatively, because GraphQL is the default method for Angular SitecoreAI apps, you can delete this environment variable. -
If you've not customized the
/src/app/lib/dictionary-service-factory.tsfile, replace it with the 22.3 version. Otherwise, add the newuseSiteQueryproperty as shown in the following example: -
To enable metadata editing integration, make sure your Layout renders a new
sc-editing-scriptscomponent (which is exposed byJssModule), as shown in the following example:NoteYou might need to introduce a separate
Scriptsmodule to achieve this; you can find an example of how to do this in thesrc/app/routing/scripts/scripts.module.tsfile in your 22.3 sample app. -
To use SitecoreAI forms, you need to register a
Formcomponent and add the required configuration. If you've not customized thescripts/generate-component-factory/plugins/packages.tsfile, replace it with the 22.3 version. Otherwise, make the following changes:-
Register the
Formcomponent. For example:ImportantDon't push components like this to the
declarationslist, because the related module is part of theimportslist. -
In the
providerslist, add an injection token usingEDGE_CONFIGto allowFormcomponents to fetch data from Experience Edge. For example:
-
-
In SitecoreAI, client-side event tracking is done via the Cloud SDK, so you need to make sure that it's initialized before any events are sent.
-
The following example shows a component that is correctly configured to do this:
ImportantFor another example, refer to
cloud-sdk-init.component.tsin your 22.3 sample app. -
Add the component to
scripts.component.htmlbefore any other scripts that use it.
-
-
To track page view events in SitecoreAI, add a component that executes the page view event, and then render it in the scripts section. In SitecoreAI, client-side event tracking is done via the Cloud SDK, so you need to make sure that it is initialized before any tracked event occurs.
-
The following example shows a component that is correctly configured to do this:
ImportantFor another example, refer to
cdp-page-view.component.tsin your 22.3 sample app. -
Add the component to
scripts.component.htmlafter theCloudSdkinitializer component. For example:
-
Update the Node headless SSR proxy app
To update your Node headless SSR proxy app:
-
Update the import statement in
/src/index.tsas follows.NoteThis makes
middleware,ProxyConfig, andServerBundleavailable to your app via theheadlessProxyobject. -
expressis now a peer dependency, so set it to the required version^4.19.2. -
If your current proxy app doesn't already have a
.gitignorefile, copy the one from your sample proxy app.
Next steps
To finalize the upgrade process, make sure you resolve any errors and warnings you encounter. Enable debug logging for JSS specific issues to assist you if necessary.