1. Sitecore JavaScript Services SDKs (JSS) 22.x

Upgrade JSS 22.3 Next.js apps to version 22.4

Version:

This topic covers most of the changes you must make to your existing JSS 22.3 applications to benefit from the latest improvements in version 22.4. 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.

The 22.4 release of JSS removes the Axios dependency from all core @sitecore-jss packages. The dependency was deeply ingrained in core JSS functionality, and its removal provides greater flexibility in how your JSS app can communicate with Sitecore, be that via native Node fetch, some alternative fetch library, or one of the latest versions of Axios including current security fixes and features.

Note

For a full summary of the changes relating to removal of Axios as the default fetcher, refer to the related changelog entry.

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.3 application included in the XM Cloud starter foundation uses the following templates and add-ons:

  • nextjs 

  • nextjs-xmcloud 

  • nextjs-sxa 

  • nextjs-multisite 

XM Cloud is now SitecoreAI

Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.

Before you begin
  • If you haven't already done so, upgrade your app to JSS 22.3.

  • 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:

Update application dependencies in your existing app

For your upgraded application to work correctly, you will need to update dependencies.

To update your dependencies:

  1. In your existing application's package.json file, update the following packages to the stated versions:

    • Update every @sitecore-jss package to version ~22.4.0.

  2. Install the dependencies with the following command:

    npm install

Update the nextjs-sxa add-on

To upgrade the nextjs-sxa add-on:

  • In src/pages/api/sitemap.ts, do the following:

    • Locate the following import statement:

      import { AxiosDataFetcher, GraphQLSitemapXmlService, AxiosResponse } from '@sitecore-jss/sitecore-jss-nextjs';
    • Delete that statement and replace it with the following:

      import { NativeDataFetcher, GraphQLSitemapXmlService } from '@sitecore-jss/sitecore-jss-nextjs';
    • Locate the following declaration:

      const sitemapApi = async (
          req: NextApiRequest,
          res: NextApiResponse
        ): Promise<NextApiResponse | void> => {
          ...
        }
    • Within that declaration, locate the following code block:

      if (sitemapPath) {
        ...  
        return new AxiosDataFetcher()
          .get(sitemapUrl, {
            responseType: 'stream',
          })
          .then((response: AxiosResponse) => {
            response.data.pipe(res);
            ...
          })
        ...
    • Replace the return statement as follows while retaining any other functionality you require within the block:

        if (sitemapPath) {
          ...
          try {
            const fetcher = new NativeDataFetcher();
            const xmlResponse = await fetcher.fetch<string>(sitemapUrl);
      
            return res.send(xmlResponse.data);
          } catch (error) {
            return res.redirect('/404');
          }
        }

Next steps

Throughout the rest of your application, replace other Axios import statements as follows:

Existing import

Replacement import

AxiosDataFetcher

NativeDataFetcher

AxiosResponse

NativeDataFetcherResponse

AxiosDataFetcherConfig

NativeDataFetcherConfig

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.

If you have suggestions for improving this article, let us know!