Upgrade JSS 21.6.4 Next.js apps to version 21.7
The JavaScript Rendering SDKs (JSS) 21.7 uses Next.js 14, improved GraphQL retries, and the Site GraphQL query for retrieving site information.
The update involves changes to 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.6.4 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.6.4 application included in the XM Cloud starter foundation uses the following templates and add-ons:
-
nextjs
-
nextjs-xmcloud
-
nextjs-sxa
-
nextjs-multisite
-
If you haven't already done so, upgrade your app to JSS 21.6.
-
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 about Next.js 14 release and keep the Next.js 14 upgrade guide handy. You might have to use it to finalize the upgrade of your application.
This topic describes how to:
Update environment variables for your existing app
The GraphQL retry functionality is now enabled by default with three retries, while still allowing you to use default or custom retry strategies when fetching data from the Edge GraphQL endpoint.
To customize the global number of retries:
-
In the JSS application's
.env
file, update the value of theGRAPH_QL_SERVICE_RETRIES
environment variable to the desired number or to 0 to disable retries.
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.json
file, do the following:-
Update the Next.js version:
RequestResponse"next": "^14.1.0",
-
Update every
@sitecore-jss
package to version~21.7.0
. -
Update the package versions in
devDependencies
to the new versions, as follows:RequestResponse"@graphql-codegen/cli": "^5.0.0", "@graphql-codegen/import-types-preset": "^3.0.0", "@graphql-codegen/plugin-helpers": "^5.0.1", "@graphql-codegen/typed-document-node": "^5.0.1", "@graphql-codegen/typescript": "^4.0.1", "@graphql-codegen/typescript-operations": "^4.0.1", "@graphql-codegen/typescript-resolvers": "^4.0.1", "@graphql-typed-document-node/core": "^3.2.0",
-
If you don't use the XM Cloud add-on, remove the following dependencies:
RequestResponse"@sitecore-feaas/clientside": "^0.5.5", "@sitecore/components": "~1.0.19",
-
If you use the XM Cloud add-on, update these packages to new versions, as follows:
RequestResponse"@sitecore/components": "^1.1.10", "@sitecore-cloudsdk/events": "^0.2.4", "@sitecore-feaas/clientside": "^0.5.17"
-
-
Install the dependencies with the following command:
RequestResponsenpm install
-
Perform any Next.js specific upgrades using the Next.js 14 upgrade guide. If necessary, use the provided codemods to complete the Next.js upgrade.
ImportantYou will need to check that version 14 of Next.js has been installed, and adjust your installation if necessary. Version 15 is not compatible with JSS 21.7.
Create a JSS 21.7 Next.js application
To simplify the upgrade process as much as possible, you must first create a JSS 21.7 Next.js application with the XM Cloud add-on. You will need to copy some files from this app into your existing app.
To create a JSS 21.7 Next.js application with the XM Cloud add-on included:
-
In a console, run the following command:
RequestResponsenpx create-sitecore-jss@^21.7.0 nextjs
-
If prompted to install the
[email protected]
package, answer y. -
Enter the folder path for the JSS 21.7 Next.js app. For example,
./jss217
, to create the app folder in your current working directory. -
Follow the remaining prompts, selecting the same options for data fetching (
GraphQL
orREST
) and prerendering (SSG
orSSR
) 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 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
In this procedure, you will synchronize files in your existing applications with corresponding files for the JSS 21.7 app.
To update the Next.js template files:
-
Copy the the
/scripts/generate-metadata.ts
file from the 21.7 app to your existing app. -
Copy the
src/lib/next-config/plugins/component-props.js
file from the 21.7 app to your existing app. -
If you have not customized the
/src/lib/dictionary-service-factory.ts
file, replace it with the 21.7 version. Otherwise, modify the file as follows:-
Locate the
retries
option in theGraphQLDictionaryService
constructor options:RequestResponseretries: (process.env.GRAPH_QL_SERVICE_RETRIES && parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) || 0,
-
Replace that code block with the following:
RequestResponseretries: (process.env.GRAPH_QL_SERVICE_RETRIES && parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) as number,
-
-
If you have not customized the
/src/lib/layout-service-factory.ts
file, replace it with the 21.7 version. Otherwise, modify the file as follows:-
Locate the
retries
option in theGraphQLLayoutService
constructor options:RequestResponseretries: (process.env.GRAPH_QL_SERVICE_RETRIES && parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) || 0,
-
Replace that code block with the following:
RequestResponseretries: (process.env.GRAPH_QL_SERVICE_RETRIES && parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) as number,
-
-
In
/scripts/fetch-graphql-introspection-data.ts
, do the following:-
Locate the following import statement:
RequestResponseimport { GraphQLRequestClient } from '@sitecore-jss/sitecore-jss-nextjs';
-
Replace that statement with the following:
RequestResponseimport { GraphQLRequestClient } from '@sitecore-jss/sitecore-jss-nextjs/graphql';
-
-
Add the following import statement to
/scripts/bootstrap.ts
:RequestResponseimport './generate-metadata';
Update the XM Cloud add-on
Updating the XM Cloud add-on requires you update some package dependencies, and replace or update some of the files in your existing application.
To update the XM Cloud add-on:
-
Replace the
/src/lib/page-props-factory/plugins/feaas-themes.ts
file with the/src/lib/page-props-factory/plugins/component-themes.ts
file from the 21.7 application. -
Replace the
/src/lib/page-props-factory/plugins/content-styles.ts
file with the 21.7 version. -
Copy the following files from the 21.7 application into your existing app:
-
/src/components/FEAASScripts.tsx
-
/src/pages/api/editing/config.ts
-
src/pages/api/editing/feaas/render.ts
-
src/pages/feaas/render.tsx
-
-
In the
/src/middleware.ts
file, update thematcher
pattern to includefeaas-render
. -
In the
src/Scripts.tsx
file:-
Import the
FEEASScripts
component:RequestResponseimport FEAASScripts from 'components/FEAASScripts';
-
Add the
FEEASScripts
component after existing components:RequestResponse<BYOC /> <CdpPageView /> <FEAASScripts />
-
Update SXA add-on
To upgrade the SXA add-on:
-
Replace the following files with the 21.7 version:
-
src/assets/basic/_container.scss
-
src/assets/sass/_app.scss
-
-
Update the
/src/components/Image.tsx
as follows:-
Import the
CSSProperties
type from thereact
package:RequestResponseimport React, { CSSProperties } from 'react';
-
Update the
backgroundStyle
constant definition to:RequestResponseconst backgroundStyle = (props?.fields?.Image?.value?.src && { backgroundImage: `url('${props.fields.Image.value.src}')`, }) as CSSProperties;
-
Update the Multisite add-on
XM Cloud deployments use the GraphQL Site query to fetch site information. This query takes the values valid for the environment when resolving available sites in a multisite environment.
The Multisite add-on has been updated to support the Site query.
To update the Multisite add-on:
-
In the
/scripts/config/plugins/multisite.ts
, update theGraphQLSiteInfoService
constructor call options to include theuseSiteQuery
option with atrue
value:RequestResponseconst siteInfoService = new GraphQLSiteInfoService({ clientFactory: createGraphQLClientFactory(config), // enable site query for the service. Only works on XMCloud currently useSiteQuery: true, });
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.
We recommend you evaluate your needs in relation to GraphQL retries and use GraphQL request retry strategies as needed to mitigate rate limits in GraphQL endpoints.