Upgrade Content SDK 0.2.0 Next.js apps to version 0.3.0
This guide covers most of the changes you must make to your existing Content SDK 0.2.0 applications to take advantage of the improvements in version 0.3.0.
This version of the Content SDK updates references to replace mentions of jss, updates the scaffolding script, and renames the SitecoreContext component, related APIs, and some environment variables to align with common conventions. This version also introduces the option to automatically generate component maps.
Because of the nature of JavaScript and Next.js application development, this guide doesn't account for all the customization you might have in your existing application.
-
If you have a JSS app that you want to convert into a Content SDK app, follow the migration guide. Then, follow the upgrade guide to upgrade from 0.1.0 to 0.2.0, before you upgrade from 0.2.0 to 0.3.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 covered in this topic.
This topic describes how to:
Update references to JSS
This version of the Content SDK replaces all mentions of jss in class names, IDs, and package scopes with Content SDK-centric names. There're no changes to the run time behaviour, so your application will work as before after updating the names.
To update references to jss:
-
Rename references to the following:
Old
New
Type
sc-jss-editing-errorsc-content-sdk-editing-errorCSS class
sc-jss-placeholder-errorsc-content-sdk-placeholder-errorCSS class
ReactJssComponentReactContentSdkComponenttype
NextjsJssComponentNextjsContentSdkComponenttype
JssLayoutQueryContentSdkLayoutQueryGraphQL layout query
getJssPagesClientData()getContentSdkPagesClientData()utility function
SitecoreTemplateId.JssAppSitecoreTemplateId.ContentSdkApptemplate ID constant
-
Rename references of the
coredebug namespace tocontent-sdk.For example: Rename
DEBUG=core:...toDEBUG=content-sdk:....
Update environment variables
Update references to the following environment variables in your .env files, deployment configurations, and CI/CD pipelines.
|
Old |
New |
|---|---|
|
|
|
|
|
|
|
|
|
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:-
Update every
@sitecore-content-sdkpackage to version0.3.0. -
Update the
@sitecore-feaas/clientsidepackage to version^0.6.0. -
Update the
@sitecore/componentspackage to version~2.1.0.
-
-
Install the dependencies with the following command:
RequestResponsenpm install
Create a template 0.3.0 Content SDK application for Next.js
To simplify the upgrade process as much as possible, create a Content SDK 0.3.0 Next.js application. You can then copy some files from this app into your existing app.
To create a 0.3.0 Content SDK Next.js application:
-
In a console, run the following command:
RequestResponsenpx [email protected] nextjs -
If prompted to install the
[email protected]package, answery. -
Enter the folder path for the new app. For example, enter
./content-sdk-030, to create the app folder in your current working directory. -
When prompted, choose the same prerendering method (SSG or SSR) as used in your existing Content SDK application.
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 Content SDK 0.3.0 template app.
To update the Next.js template files:
-
Update references and imports for the following component, interfaces, hook, higher-order component, and properties:
Old
New
Type
SitecoreContextSitecoreProvidercomponent
SitecoreContextPropsSitecoreProviderPropsinterface
SitecoreContextStateSitecoreProviderStateinterface
SitecoreContextValueSitecoreProviderPageContextinterface
SitecoreContextReactContextSitecoreProviderReactContextinterface
WithSitecoreContextOptionsWithSitecoreOptionsinterface
WithSitecoreContextPropsWithSitecorePropsinterface
WithSitecoreContextHocPropsWithSitecoreHocPropsinterface
useSitecoreContext()useSitecore()hook
withSitecoreContext()withSitecore()higher-order component
contextpageContextproperty (the page context provided by
SitecoreProvider)updateSitecoreContextupdateContextproperty
sitecoreContextpageContextproperty
-
Remove the
sitecoreEdgeUrlproperty from theEditingScriptscomponent. The component now uses the Edge URL configuration fromSitecoreProvider. -
Update references to the component-level
getServerSidePropsandgetStaticPropsmethods togetComponentServerPropswithout changing the existing method signature.If your component contains code that's executed based on static or server side Next.js context, import the
isServerSidePropsContexthelper method and implement it based on the following example:RequestResponseimport { isServerSidePropsContext } from '@sitecore-content-sdk/nextjs/utils'; ... export const getComponentServerProps: GetComponentServerProps = async ( rendering, layoutData, context ) => { if (isServerSidePropsContext(context)) { // function implementation } }; -
Create a folder named
content-sdkinsrc/components. Move the following components into thecontent-sdkfolder and update their imports:Moved file
Affects
Updated import
SitecoreStyles.tsxsrc/Layout.tsxRequestResponseimport SitecoreStyles from 'src/components/content-sdk/SitecoreStyles';CdpPageView.tsxsrc/Scripts.tsxRequestResponseimport CdpPageView from 'components/content-sdk/CdpPageView';FEAASScripts.tsxsrc/Scripts.tsxRequestResponseimport FEAASScripts from 'components/content-sdk/FEAASScripts'; -
Create another folder named
sxainsrc/components. Move the following components into thesxafolder and update their imports:-
ColumnSplitter.tsx -
Container.tsx -
ContentBlock.tsx -
Image.tsx -
LinkList.tsx -
Navigation.tsx -
PageContent.tsx -
PartialDesignDynamicPlaceholder.tsx -
Promo.tsx -
RichText.tsx -
RowSplitter.tsx -
Title.tsx
-
-
This version of the Content SDK removes the GraphQL introspection data (
GraphQLIntrospectionResult.json). If you're using the Next.js starter, you must remove thetsconfig.scripts.jsonfile,/scriptsand/src/tempfolders. Then, make the following changes to yourpackage.json:-
In
devDependencies, remove the following dependencies:RequestResponse"chalk": "~4.1.2", "chokidar": "~4.0.3", "graphql": "~16.11.0", -
Update the lint script with the following:
RequestResponse"lint": "eslint ./src/**/*.tsx ./src/**/*.ts,NoteIf you wish to set up automatic GraphQL code generation for your Content SDK app, see Implement GraphQL code generation for more information.
-
-
In your existing application's
sitecore.cli.config.tsfile:-
Locate the following
defineCliConfigimport statement:RequestResponseimport { defineCliConfig } from '@sitecore-content-sdk/nextjs/config'; -
Replace it with the following:
RequestResponseimport { defineCliConfig } from '@sitecore-content-sdk/nextjs/config-cli';
-
Use automatic component map generation
You can optionally set up automatic component map generation for your workspace. If you wish to manage the component-map.ts file yourself, you can skip this section.
To set up automatic component map generation:
-
In your existing application's
sitecore.cli.config.tsfile, indefineCliConfig, add the following section below thebuildsection:RequestResponsebuild: { ... }, // Add this section componentMap: { paths: ['src/components'], // Exclude content-sdk auxillary components exclude: ['src/components/content-sdk/*'], }, -
Move
src/lib/component-map.tsfile into the.sitecorefolder.Modify all the imports to use
.sitecore/component-mapinstead ofsrc/lib/component-map. -
In your app’s
package.jsonfile:-
In the
scriptssection, add the following:RequestResponse"sitecore-tools:generate-map": "sitecore-tools project component generate-map", "sitecore-tools:generate-map:watch": "sitecore-tools project component generate-map --watch", -
Modify the
buildscript to include asitecore-tools:generate-mapcall:RequestResponse"build": "cross-env NODE_ENV=production npm-run-all --serial sitecore-tools:generate-map sitecore-tools:build next:build", -
Modify the
devscript to include asitecore-tools:generate-map:watchcall:RequestResponse"dev": "cross-env NODE_ENV=development npm-run-all --serial sitecore-tools:build --parallel next:dev sitecore-tools:generate-map:watch",
-
Next steps
To finalize the upgrade process, make sure you resolve any errors and warnings you encounter. Enable debug logging for Content SDK specific issues to assist you if necessary.