Migrate your Content SDK 1.2 Pages Router app to App Router
This topic takes you through the steps required to migrate your Pages Router application to App Router. The topic assumes that you're using Content SDK Pages Router application on version 1.2.
-
Ensure that you're using Pages Router on Content SDK 1.2 before following this guide.
-
If you haven't already upgraded to Content SDK 1.2, follow the upgrade guide.
This topic covers the following subjects:
Create a template App Router application
To simplify the migration process as much as possible, create a Content SDK Next.js App Router application. You can then copy some files from this template app into your existing app.
To scaffold the new App Router template app, run the following command:
Update the Next.js template files in your existing app
The following sections explain how to synchronize files in your existing Content SDK application with corresponding files from the template App Router app.
Update core Content SDK files
This sections updates your existing app's core files to match the new Content SDK app.
To update your core Content SDK files:
-
Start the migration by moving your logic into the new file structure. Copy the
src/appfolder from the sample App Router app into the app you are upgrading. Check the table below and take note of any custom logic in these files and the places this custom logic should move to:Existing file
New file
pages/_app.tsxapp/layout.tsx/app/[site]/layout.tsxIf
_app.tsxhas been importing CSS rules previously, move the import intoapp/globals.cssIf you’ve been using Tailwind CSS, consider enabling it via PostCSS in the App Router app.
The Skate Park starter sample in the xmcloud-starter-js repository contains an example on how to do it.
pages/[[…path]].tsxapp/[site]/[…path]/page.tsxpages/500.tsxapp/global-error.tsxpages/404.tsxapp/[site]/[…path]/not-found.tsxpages/api/%path%/%api-route%.tsxapp/api/%path%/%api-route%/route.tsIf there’s no custom logic, remove the old files and keep the files from the newly scaffolded app into your existing app.
-
If you haven't customized
src/Bootstrap.tsx, replace it with the one from the sample App Router app. Otherwise, make the following changes:-
Add the following at the start of the file:
-
Remove the following import statement:
-
Find the following:
-
Replace it with the following:
-
Find the following
useEffectportion: -
Replace it with the following:
-
-
If you haven't customized
src/Layout.tsx, replace it with the one from sample App Router app. Otherwise, make the following changes:-
Find the following imports:
-
Replace them with the following imports:
-
Find the
RouteFieldsinterface and make it exportable as shown: -
Remove the following lines from the
Layoutfunction implementation, if present: -
Find the
DesignLibraryrendering: -
Replace it with the following:
-
Replace all the
Placeholderusages withAppPlaceholder. For example, forheadless-headerplaceholder, find the following: -
Replace it with the following:
-
-
If you haven't customized
src/Scripts.tsx, replace it with the one from the sample App Router app. Otherwise, make the following changes:-
Add the following at the start of the file:
-
Remove the following import:
-
Remove the following line:
-
-
Remove
src/NotFound.tsxif present. Move any custom logic toapp/not-found.tsx. -
Copy
src/DesignLibraryLayout.tsxandsrc/Providers.tsxfrom the sample App Router app into your existing application. -
In the app's
tsconfig.jsonfile:-
Set
targetincompilerOptionstoES2017. -
In the
pluginssection, add thenextplugin as shown: -
In the
pathssection, add the.sitecorepath as shown:
-
Migrate next.config.js to TypeScript
Rename the next.config.js file to next.config.ts and ensure the following is present in the file in addition to any customizations you may have done:
Update localization
Pages Router Content SDK uses the I18nProvider imported from the 'next-localization' library out of the box to enable localization. App Router uses the next-intl package instead.
To migrate to the new implementation:
-
In your existing app's
package.jsonfile, replace the"next-localization": "^0.12.0",dependency with"next-intl": "^4.3.5",. -
Copy the
src/i18nfolder from the sample App Router app into your existing app. -
Use
NextIntlClientProviderinstead ofI18nProviderby making the following changes:-
Find the following import:
-
Replace it with the following:
-
Use
NextIntlClientProvideras a wrapper context provider for your pages and components.
-
-
Use routing imported from
'src/i18n/routing'to access routing informatin for your pages.
Check src\app\[site]\[locale]\[[...path]]\page.tsx in the sample App Router app to see an example of the new localization implementation.
Update SitecoreProvider
Built-in Sitecore contexts were previously applied via two providers (ComponentPropsContext and SitecoreProvider) as shown:
Content SDK 1.2.0 streamlines this with the introduction of the src/Providers.tsx file. You can apply required contexts by importing Providers as shown:
To use them where both providers where applied before:
If you need to apply individual providers, you can still do so.
Update BYOC functionality
If you're using BYOC, make the following changes:
-
In the
src/byoc/index.client.tsxfile, and the following at the top of the file: -
In the
src/byoc/index.tsfile:-
Find the following lines:
-
Replace it with the following:
-
Update placeholders
You must replace Placeholder components throughout your app with AppPlaceholder.
To replace it for the headless-header placeholder in Layout.tsx:
-
Find the following:
-
Replace it with the following:
ImportantEnsure that you import the component map from
'.sitecore/component-map';or your custom component-map location. You must also get thepageprop either from the props passed into the server component or from the Sitecore Context viauseSitecore()call in client components (const { page } = useSitecore();).
Migrate API routes
If you don’t have any custom routes or modifications to the out-of-the-box Content SDK API routes located under pages/api, copy the /src/app/api folder and all the files under it from the sample App Router application into your existing application.
For any custom routes or customizations to the out-of-the-box routes, migrate them to the new route structure introduced by App Router.
Update components
App Router provides the ability to fine tune your application by converting your components into server or client ones. To mark the built-in components as client components:
-
For all components under
src/components/content-sdk, add the following at the start of the file: -
You should mark client components appropriately in
component-map.tsor add them tocomponent-map.client.ts. This is only applicable if you're managing your own component-maps.To mark your client components in the component-map, use the following example:
-
Contexts, such as Sitecore Context, applied via
SitecoreProviderand other React providers, will be unavailable in server components. Ensure you pass required data as props into server components. -
You can only pass serializable props from server components into client components.
-
The
getServerSidePropsfunction can still work with App Router, but we strongly recommend you utilize Server Functions or server logic instead.