Upgrade JSS 22.0 Next.js apps to version 22.1
This topic covers most of the changes you must make to your existing JSS 22.0.0 applications to benefit from the latest improvements in version 22.1. 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.
While upgrading, 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 22.0.0 application might use the following templates and add-ons:
-
nextjs -
nextjs-sxa -
nextjs-multisite
-
If you haven't already done so, upgrade your app to JSS 22.0.0.
-
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 be covered in this topic.
This topic describes how to:
Update application dependencies in your existing app
For your upgraded application to work correctly, you must update dependencies.
To update your dependencies:
-
In your existing application's
package.jsonfile, do the following:-
Update every
@sitecore-jsspackage to version~22.1. -
Update the
@types/nodepackage to version^20.14.2.
-
-
Install the dependencies with the following command:
RequestResponsenpm install -
We highly recommend you install version
0.32.6of thesharpimage processing library using the following command. This is to improve memory usage of the image optimization feature:RequestResponsenpm i [email protected]
Create a JSS 22.1 Next.js application
To simplify the upgrade process as much as possible, create a JSS 22.1 Next.js application. You can then copy some files from this app into your existing app.
To create a JSS 22.1 Next.js application:
-
In a console, run the following command:
RequestResponsenpx [email protected] nextjs -
If prompted to install the
[email protected]package, answer y. -
Enter the folder path for the JSS 22.1 Next.js app. For example, enter
./jss221, to create the app folder in your current working directory. -
Follow the remaining prompts, selecting the same options for data fetching (
GraphQLorREST) and prerendering (SSGorSSR) as in your existing application. -
When asked if you are building for Sitecore XM Cloud, answer n to skip installing the XM Cloud add-on.
-
Select other add-ons used by your existing application and press Enter.
The script then installs the application dependencies.
Update the Next.js template files in your existing app
This explains how to synchronize files in your existing application with corresponding files from your new JSS 22.1 app.
To update the Next.js template files:
-
If you have not customized the
scripts/generate-config.tsfile, replace it with the 22.1 version. Otherwise, modify your file as follows:-
In the
configTextprop assignment, add atrimmethod call afterconfig[prop]?, such as:RequestResponseconfig[prop]?.trim() -
At the end of that assignment, replace the comma before the newline character with a semicolon:
RequestResponse// Before }',\n`; // After }';\n`;
Here's an example of the updated assignment suitable for JSS 22.1:
RequestResponseconfigText += `config.${prop} = process.env.${constantCase(prop)} || '${config[prop]?.trim()}';\n`; -
-
If you have not customized the the
lib/next-config/plugins/cors-headerplugin, replace it with the 22.1 version. Otherwise, modify your file as follows:-
Because cross-origin resource sharing (CORS) is now handled by API handlers or middleware, remove the following CORS header for API endpoints:
RequestResponse{ source: '/api/:path*', headers: [ { key: 'Access-Control-Allow-Origin', value: config.sitecoreApiHost.replace(/\/$/, ''), }, ], },
-
-
Because
ComponentConsumerPropshas been removed, if you have anyimportstatements that reference it, update them all to useWithSitecoreContextPropsinstead. -
Because the
resolvePageUrlfunction now accepts an object, and becauseserverUrlis now optional, the latter is omitted if the metadata edit mode for Pages is enabled. If you've customized thepages/api/editing/render.tsfile and you currently pass theresolvePageUrlfunction intoEditingRenderMiddleware, modify your file as follows:-
Replace the existing handler initialization with the following:
RequestResponseconst handler = new EditingRenderMiddleware({ resolvePageUrl: ({ serverUrl, itemPath }) => `${serverUrl}${itemPath}`, }).getHandler();
NoteIf you haven't customized
pages/api/editing/render.ts, you don't need to make any new changes to it for this release. -
-
If your app contains any references to the now-deprecated
EditingComponentPlaceholderor theRenderingTypeenum, such as the following example in[[...path]].tsx, delete them:RequestResponseimport Layout from 'src/Layout'; import { RenderingType, EditingComponentPlaceholder } from '@sitecore-jss/sitecore-jss-nextjs'; ... const isComponentRendering = layoutData.sitecore.context.renderingType === RenderingType.Component; ... {isComponentRendering ? ( <EditingComponentPlaceholder rendering={layoutData.sitecore.route} /> ) : ( <Layout layoutData={layoutData} headLinks={headLinks} /> )}
Update the SXA add-on
To upgrade the SXA add-on:
-
Replace the following files with the new versions from your 22.1 app:
-
src/components/Image.tsx -
src/components/Promo.tsx -
src/components/Title.tsx -
src/assets/sass/components/title/_component-title.scss
-
Replace the import path for editing utilities
We're moving all editing utilities currently located at @sitecore-jss/sitecore-jss/utils, so if you currently refer to this path in any of your import statements you will need to update them to use the new path @sitecore-jss/sitecore-jss/editing instead.
This change affects the following utilities:
-
ChromeCommand -
DefaultEditFrameButton -
DefaultEditFrameButtonIds -
DefaultEditFrameButtons -
EditButtonTypes -
EditFrameDataSource -
ExperienceEditor -
FieldEditButton -
handleEditorAnchors -
HorizonEditor -
isEditorActive -
mapButtonToCommand -
Metadata -
resetEditorChromes -
WebEditButton
The older path will only continue to work until the next release of JSS. Therefore, we strongly recommend you make the necessary changes as soon as possible.
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.