Upgrade JSS 21.5 Next.js apps to version 21.6
The JavaScript Rendering SDKs (JSS) 21.6 consolidates client management for all GraphQL-based services and refactors some services and configurations.
This topic covers most of the changes you must make in your existing JSS 21.5 applications to take advantage of the new features.
Due to the nature of JavaScript and Next.js application development, this guide can not 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 might use the following templates and add-ons:
-
nextjs -
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.
This topic describes how to:
-
Update application dependencies in your existing app.
-
Update environment variables for 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 (if present).
-
Update the Multisite add-on files.
Update application dependencies in your existing app
For your existing application to work correctly and use new features, you need to update dependencies.
To update your dependencies:
-
In your existing application's
package.jsonfile:-
Update every
@sitecore-jsspackage to use version~21.6.0. -
Update
@types/reactto^18.2.22.
-
-
Install the dependencies with the following command:
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, modify your .env file as follows:
-
Rename the
JSS_APP_NAMEenvironment variable toSITECORE_SITE_NAME. This is optional.
You might need to restart the local application server for the environment variable changes to take effect.
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. 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:
-
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, to create the app folder in your current working directory, enter ./jss216.
-
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 n to skip installing the XM Cloud add-on.
-
Select the add-ons used by your existing application -
nextjs-sxaand/ornextjs-multisiteand press Enter.
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.
Update the Next.js template files as follows:
-
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 remove the import of thegetPublicUrlfunction and replace the function's 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 using the
getPublicUrlfunction to generate the value of thepublicUrlconstant, replace it with the value from theconfigobject: -
Remove the
import { getPublicUrl } from '@sitecore-jss/sitecore-jss-nextjs/utils';statement.
-
-
Remove the
/src/lib/page-props-factory/plugins/content-styles.tsfile if present.
Update SXA add-on files
To upgrade the SXA add-on (if present):
-
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:
-
-
If you have not customized the
/src/pages/api/sitemap.tsroute, replace it with the 21.6 version. Otherwise, remove the import statement for thegetPublicUrlutility function and create two new constants forreqtHostandreqProtocol. Use them to build the page locator: -
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:
Update the Multisite add-on files
To use the new GraphQL request client factory, you need to 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:
-