Upgrade JSS 21.5 Next.js apps to version 21.6
The JavaScript Rendering SDKs (JSS) 21.6 brings new features and support for the Edge Context and the XM Cloud add-on for Next.js that simplifies integrating personalization features and other composable services and products. Furthermore, some functionality was consolidated into this new plugin, and we refactored some services and configurations to support the Edge Context and simplify configuration.
The update contains changes in the environment variables, configuration objects, JSS packages, services, and dependencies.
This topic covers most of the changes you must make in your existing JSS 21.5 applications to take advantage of the new features. However, because of the nature of JavaScript and Next.js application development, this topic doesn't account for all the customization you might have in your existing application.
When performing the upgrade, consider the JSS templates and add-ons you used when creating your Next.js application. You can find them in your package.json file. For example, a JSS 21.5 application included in the XM Cloud starter foundation uses the following templates and add-ons:
-
nextjs -
nextjs-personalize -
nextjs-sxa -
nextjs-multisite
-
Familiarize yourself with the changelog. If your application is heavily customized, you might need to refer to it for hints on what additional changes you need, which might not be covered in this topic.
-
Read the documentation for the JSS XM Cloud add-on. It will help you understand the changes you must make when following this topic.
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
This topic describes how to:
-
Update environment variables for your existing app.
-
Update application dependencies in your existing app.
-
Create a JSS 21.6 Next.js application.
-
Update the Next.js template files in your existing app.
-
Update SXA add-on files.
-
Migrate from the Personalize add-on to the XM Cloud add-on.
-
Update the Multisite add-on files.
-
Fix BYOC errors after upgrading.
Update environment variables for your existing app
The updated configuration scripts need some new environment variables.
To provide the new environment variables to your application, you must modify your .env file as follows:
-
Add the
SITECORE_EDGE_CONTEXT_ID. The value is your Context ID. This is required to use the Sitecore Edge Platform. -
Rename the
JSS_APP_NAMEenvironment variable toSITECORE_SITE_NAME. -
Remove the following deprecated environment variables:
-
NEXT_PUBLIC_CDP_TARGET_URL -
NEXT_PUBLIC_CDP_CLIENT_KEY -
NEXT_PUBLIC_CDP_POINTOFSALE
-
You might need to restart the local application server for the environment variable changes to take effect.
Update application dependencies in your existing app
For your upgraded application to work correctly, you must also update dependencies.
To update your dependencies:
-
In your existing application's
package.jsonfile, do the following:XM Cloud is now SitecoreAISome code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
-
If you don't use the XM Cloud/Personalize add-on, remove the following dependency:
-
If you do use the XM Cloud/Personalize add-on, remove the
"@sitecore/engage": "^1.4.1"dependency and replace it with the following: -
Update every
@sitecore-jsspackage to version~21.6.0. -
Update the
@sitecore/componentspackage to version~1.0.19. If not present, add it. -
Update
@types/reactto^18.2.22.
-
-
Install the dependencies with the following command:
Create a JSS 21.6 Next.js application
To simplify the upgrade process as much as possible, you must first create a JSS 21.6 Next.js application with the XM Cloud add-on. You will need to copy some files from the 21.6 app into your existing app.
To create a JSS 21.6 Next.js application with the XM Cloud add-on included:
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
-
In a console, run the following command:
-
If prompted to install the
[email protected]package, answer y. -
Enter the folder path for the JSS 21.6 Next.js app. For example, ./jss216, to create the app folder in your current working directory.
-
Follow the remaining prompts, selecting the same options for data fetching (
GraphQLorREST) and prerendering (SSGor SSR) as in your existing application. -
When asked if you are building for Sitecore XM Cloud, answer y to install the XM Cloud add-on.
-
Select the add-ons used by your existing application -
nextjs-sxaand/ornextjs-multisiteand press Enter.NoteThe
nextjs-personalizeplugin has been replaced by the XM Cloud add-on (nextjs-xmcloud), so you will not see it in the list of add-ons.
The script then installs the application dependencies.
Update the Next.js template files in your existing app
In this procedure, you will replace files in your existing applications with corresponding files for the JSS 21.6 app, or modify your existing app files using the JSS 21.6 app files as a guide.
To update the Next.js template files:
-
Replace the
/scripts/generate-config.tsfile in your existing application with the 21.6 version. -
Replace the
/src/lib/next-config/plugins/cors-header.jsfile in your existing application with the 21.6 version. -
Copy the the
/src/lib/graphql-client-factory/folder from the 21.6 app to your existing app. -
Copy the
/src/lib/config.tsfile from the 21.6 app to your existing app. -
If you have not customized the
/src/lib/dictionary-service-factory.tsfile, replace it with the 21.6 version. Otherwise, modify it as follows:-
Add the following import statement:
-
Locate the following
GraphQLDictionaryServiceconstructor options: -
Replace that code block with the following:
-
-
If you have not customized the
/src/lib/layout-service-factory.tsfile, replace it with the 21.6 version. Otherwise, modify it as follows:-
Add the following import statement:
-
Locate the following
GraphQLLayoutServiceconstructor options: -
Replace that code block with the following:
-
-
In the
/src/pages/_app.tsxfile, do the following:-
Add the following import statement:
-
Add the
Bootstrapcomponent to the app rendering function. For example:
-
-
In the
/next.config.jsfile, replace the import of thegetPublicUrlfunction and replace the function call withjssConfig.publicUrl. -
Replace the rest of the files in the
/scripts/configfolder with the 21.6 versions. -
In the
/src/Layout.tsxand the/src/Navigation.tsxfiles do the following:-
Import the
configobject: -
Instead of generating it with the
getPublicUrlfunction to generate the value of thepublicUrlconstant, replace it with the value from theconfigobject: -
Remove the following statement:
-
-
Replace any remaining occurrences of the config property
jssAppNamewithsitecoreSiteName.
Update SXA add-on files
To upgrade the SXA add-on:
-
Replace the
/src/assets/sassfolder with the 21.6 version. -
If you have not customized the
/src/lib/middleware/plugins/redirects.tsfile, replace it with the 21.6 version. Otherwise, modify it as follows:-
Add the following import statement:
-
Locate the following
RedirectsMiddlewareconstructor options:Replace that code block with the following:
-
-
In
/src/pages/api/sitemap.ts, do the following:-
Remove the import statement for the
getPublicUrlutility function and create two new constants forreqtHostandreqProtocol. Use them to build the page locator: -
Add the following import statement:
-
Locate the
sitemapXmlServiceinitialization and replace it with the following:
-
-
In
/src/pages/api/robots.ts, do the following:-
Locate the following import statement:
-
Replace that statement with the following:
-
Locate the
robotsServiceinitialization and replace it with the following:
-
-
If you have not customized the
/src/pages/404.tsxand/src/pages/500.tsxfiles, replace them with the 21.6 version. Otherwise, in thegetStaticPropsfunction, import and use the GraphQL request client factory, instead of theendpointandapiKeyoptions for theGraphQLErrorPagesServiceinstantiation:
Migrate from the Personalize add-on to the XM Cloud add-on
The XM Cloud add-on (nextjs-xmcloud) adds support for the unified Context to JSS Next.js apps, enabling simplified configuration.
Furthermore, the XM Cloud add-on replaces the deprecated Personalize add-on (nextjs-personalize). This is only relevant in dependencies, as the files added to your application by the old add-on still exist, but they must be updated to use new environment variables and the GraphQL request client factory.
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
To use the new simplified configuration and correctly implement Personalization features, you need to make the following changes:
-
Copy the following from the JSS 21.6 Next.js application into your existing application:
-
The
/scripts/config/plugins/edge-platform.tsfile. -
The
/src/lib/contextfolder. -
The
/src/byocfolder. -
The
/src/Bootstrap.tsxfile.
-
-
If you have not customized the
/src/lib/middleware/plugins/personalize.tsfile, replace it with the 21.6 version. Otherwise, modify it as follows:-
Import the bootstrapped
configobject and the GraphQL request client factory: -
In the
edgeConfigobject, replace theendpointandapiKeyproperties with the importedclientFactory: -
In the
cdpConfigobject, replace theendpointandclientKeyproperties withsitecoreEdgeUrlandsitecoreEdgeContextId:
-
-
If you have not customized the
/src/components/CdpPageView.tsx, replace it with the 21.6 version. Otherwise, make the following changes:-
Remove the
SiteInfo,PosResolver,init, andsiteResolverimports. -
Import the Context instance:
-
Replace the
createPageViewasynchronous function with:
-
-
In the
/src/Scripts.tsxfile, import and use the BYOC component:
If you are not using the nextjs-sxa add-on, copy the following files from the 21.6 application to your existing application:
-
/scripts/scaffold-component/plugins/byoc.ts -
/scripts/scaffold-component/plugins/next-steps-byoc.ts -
/scripts/templates/byoc-component-src.ts
These files are already present if you use the nextjs-sxa add-on.
Update the Multisite add-on files
To use the new GraphQL request client factory, you must update the GraphQL Sitemap Service provided by the JSS Next.js Multisite add-on.
To update the Sitemap service:
-
If you have not customized the
/src/lib/sitemap-fetcher/plugins/graphql-sitemap-service.tsfile, replace it with the 21.6 version. Otherwise, do the following:-
Add the following import statement:
-
Replace the
endpointandapiKeyoptions for theMultisiteGraphQLSitemapServiceinstantiation with the client factory:
-
Fix BYOC errors after upgrading
After upgrading, you might experience errors or unexpected behaviors. Here are some problems you might encounter and tips for how to solve them.
External (BYOC) component registration errors
If you’re using External (BYOC) Components in your app, you might encounter This component was not registered. errors.
You can get these errors in two situations:
-
If your BYOC components are not imported in
/src/byoc/index.client.tsor/src/byoc/index.hybrid.ts. -
If you don't have the
/src/lib/next-config/plugins/feaas.jsplugin in your application. This plugin ensures the FEEAS SDK uses Commonjs on the server, and prevents double registration of BYOC components such as client-side and server-side.
To resolve these errors:
-
Ensure that your components are imported in either
/src/byoc/index.client.tsor/src/byoc/index.hybrid.ts, depending on the context where they run. -
Copy the
/src/lib/next-config/plugins/feaas.jsfile from the 21.6 application you created at the start of the upgrade work.
External (BYOC) components do not work
If BYOC components don't work, ensure the <BYOC/> component is imported and rendered in either of these files:
-
/src/Scripts.tsx -
src/Layout.tsx