---
title: "JSS 22.11のNext.jsアプリをバージョン22.12にアップグレードしてください"
slug: "upgrade-jss-22-11-next-js-apps-to-version-22-12"
abstract: "JSS 22.11のNext.jsアプリをバージョン22.12にアップグレードする方法。"
---
# JSS 22.11のNext.jsアプリをバージョン22.12にアップグレードしてください

このトピックでは、既存のJSS 22.11アプリケーションをバージョン22.12にアップグレードするために行う必要がある変更について扱います。しかし、JavaScriptやNext.jsアプリケーション開発の性質上、既存のアプリケーションで可能なすべてのカスタマイズをこのトピックではカバーしていません。

アップグレード時には、Next.jsアプリケーションを作成する際に使用したJSSテンプレートやアドオンを考慮してください。それらはpackage.jsonファイル内で見つけることができます。例えば、XM Cloudスターターファウンデーションに含まれるJSS 22.11アプリケーションは、以下のテンプレートとアドオンを使用しています。

-   nextjs
-   nextjs-styleguide または nextjs-sxa
-   nextjs-multisite

> !注**XM Cloudは現在SitecoreAIです**エンジニアリング資産が更新されている間、一部のコード例、画像、UIラベルは引き続きXM Cloudを使用する場合があります。

> !注**始める前に**
> 
> -   まだなら、[アプリをJSS 22.11にアップグレード](https://doc.sitecore.com/xp/en/developers/hd/22/sitecore-headless-development/upgrade-jss-22-10-next-js-apps-to-version-22-11.html)してください。
>     
> -   [変更ログ](https://github.com/Sitecore/jss/blob/dev/CHANGELOG.md#22120)に慣れておきましょう。アプリケーションがかなりカスタマイズされている場合、変更ログはこのトピックで扱っていない追加変更の案内を提供します。
>     

このトピックでは、以下の方法を説明します:

:::攻略

1.  [既存のアプリでアプリケーション依存関係を更新する](https://doc.sitecore.com/xp/ja/developers/hd/22/sitecore-headless-development/upgrade-jss-22-11-next-js-apps-to-version-22-12.html#update-application-dependencies-in-your-existing-app)
2.  [JSS 22.12 Next.jsアプリケーションを作成する](https://doc.sitecore.com/xp/ja/developers/hd/22/sitecore-headless-development/upgrade-jss-22-11-next-js-apps-to-version-22-12.html#create-a-jss-2212-nextjs-application)
3.  [既存のアプリでNext.jsテンプレートファイルを更新してください](https://doc.sitecore.com/xp/ja/developers/hd/22/sitecore-headless-development/upgrade-jss-22-11-next-js-apps-to-version-22-12.html#update-the-nextjs-template-files-in-your-existing-app)
4.  [SXAアドオンの更新:](https://doc.sitecore.com/xp/ja/developers/hd/22/sitecore-headless-development/upgrade-jss-22-11-next-js-apps-to-version-22-12.html#update-the-sxa-add-on)::

## 既存のアプリでアプリケーション依存関係を更新する

アップグレードしたアプリケーションを正しく動作させるには、依存関係を更新する必要があります。

依存関係を更新するには:

1.  既存のアプリケーションのpackage.jsonでは、@sitecore-jssパッケージをバージョン ~22.12.0に更新してください。
    
2.  以下の依存関係を指定されたバージョンに更新します:
    
    -   Next.jsはバージョン16にアップデートされました。ここで記載されていない変更を、[Next.js 16のアップグレードガイド](https://nextjs.org/docs/app/guides/upgrading/version-16)に基づいて適用する必要があるかもしれません。
        
        "next": "^16.1.1",
        
        > !注JSX.Elementに関連するタイプエラー(例えばCdpPageViewのようなコンポーネント)があれば、JSX.Elementを使うところに以下のインポートを追加してください:
        > 
        > import type { JSX } は 'react' から;
        
    -   Node.jsはバージョン24にアップデートされました。ここで言及されていない変更を、[Node.js 24のアップグレードガイド](https://nodejs.org/en/blog/migrations/v22-to-v24)に基づいてアプリに適用する必要があるかもしれません。
        
        "@types/node": "^24.10.4",
        
3.  以下のコマンドで依存関係をインストールします:
    
    npm install
    

## JSS 22.12 Next.jsアプリケーションを作成する

アップグレードのプロセスをできるだけ簡単にするために、JSS 22.12 Next.jsアプリケーションを作成しましょう。このアプリから既存のアプリにファイルをコピーできます。

JSS 22.12 Next.jsアプリケーションを作成するには:

1.  コンソールでは、次のコマンドを実行します:
    
    npx create-sitecore-jss@22.12 nextjs
    
2.  create-sitecore-jss@22.12パッケージのインストールを促された場合は、yに答えてください。
    
3.  JSS 22.12 Next.jsアプリのフォルダパスを入力します。例えば、./jss2212と入力すると、現在の作業ディレクトリにアプリフォルダを作成します。
    
4.  残りのプロンプトに従い、既存のアプリケーションと同じデータ取得(GraphQLまたはREST)とプリレンダリング(SSGまたはSSR)を選択してください。
    
5.  Sitecore XM Cloudのために建築しているか尋ねられたら、n答えてください。
    
6.  既存のアプリケーションで使っている他のアドオンを選択し、Enterキーを押してください。
    

スクリプトはその後、アプリケーションの依存関係をインストールします。

> !注**XM Cloudは現在SitecoreAIです**エンジニアリング資産が更新されている間、一部のコード例、画像、UIラベルは引き続きXM Cloudを使用する場合があります。

## 既存のアプリでNext.jsテンプレートファイルを更新してください

これは、既存のアプリケーション内のファイルを新しいJSS 22.12アプリの対応ファイルと同期する方法を説明しています。

Next.jsテンプレートファイルの更新:

1.  [ミドルウェアはその目的をよりよく反映するためにプロキシに改名されました。](https://nextjs.org/docs/app/getting-started/proxy)src/middleware.tssrc/proxy.ts
    -   エクスポート関数を修正して、evパラメータを受け入れなくなるようにします:
        
        export default async function proxy(req: NextRequest) {
         return middleware(req);
        }
        
    -   /src/lib/middleware/index.tsのメソッド署名を修正して、もはやNextFetchEventを使わないようにしてください:
        
        export interface MiddlewarePlugin {
         /\*\*
         \* Detect order when the plugin should be called, e.g. 0 - will be called first (can be a plugin which data is required for other plugins)
         \*/
         order: number;
         /\*\*
         \* A middleware to be called, it's required to return @type {NextResponse} for other middlewares
         \*/
         exec(req: NextRequest, res?: NextResponse): Promise<NextResponse>;
        }
        ...
        export default async function middleware(
         req: NextRequest
        ): Promise<NextResponse> {
        ...
        
        const finalRes = await (Object.values(plugins) as MiddlewarePlugin)
         .sort((p1, p2) => p1.order - p2.order)
         .reduce((p, plugin) => p.then((res) => plugin.exec(req, res)), Promise.resolve(response));
        ...
        ...
        
    -   NextFetchEventのインポートをすべて削除してください。
        
2.  プレースホルダーでReact Suspenseを無効にすることで、コンポーネントは追加のSuspenseオーバーヘッドなしにより速くレンダリングされます。しかし、アプリが怠惰にロードされたコンポーネントを <Placeholder/> 内で使用し、ロードのフォールバックが現れることを期待している場合は、アップグレード時に以下の手順を実行する必要があります:
    -   動的または怠惰なコンポーネントがレンダリングされている使用状況 <Placeholder/> 監査。
    -   サスペンスベースの読み込み状態に依存するプレースホルダーに disableSuspense={false} を追加してください。

## SXAアドオンの更新

SXAアドオンのアップデート:

1.  sass-alias依存を取り除く:
    
    "sass-alias": "^1.0.5"
    
2.  /src/lib/next-config/plugins/sass.jsを修正する:
    
    -   すべてのsass-aliasインポートを探す:
        
        const SassAlias = require('sass-alias');
        
    -   次のものに置き換えてください:
        
        const fs = require('fs');
        const { pathToFileURL, fileURLToPath } = require('url');
        
    -   createAliasImporterという新しい関数を追加します。
        
        /\*\*
         \* Custom Sass importer compatible with Next.js 16's Sass API
         \* Implements canonicalize and load methods as required by the new Sass JS API
         \*/
        function createAliasImporter(aliases) {
         return {
         canonicalize(url, context) {
         // Check if the URL matches any of our aliases
         for (const alias, aliasPath of Object.entries(aliases)) {
         if (url.startsWith(alias)) {
         // Remove the alias prefix and resolve the path
         const relativePath = url.slice(alias.length);
         // Remove leading slash if present
         const cleanPath = relativePath.startsWith('/') ? relativePath.slice(1) : relativePath;
         const fullPath = path.resolve(aliasPath, cleanPath);
        
         // Try to find the file with common extensions
         const extensions = '.scss', '.sass', '.css';
         for (const ext of extensions) {
         const filePath = fullPath + ext;
         if (fs.existsSync(filePath)) {
         return pathToFileURL(filePath);
         }
         }
        
         // Try as a directory with index file
         if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
         for (const ext of extensions) {
         const indexPath = path.join(fullPath, 'index' + ext);
         if (fs.existsSync(indexPath)) {
         return pathToFileURL(indexPath);
         }
         }
         }
        
         // Try with underscore prefix (Sass partials)
         const dir = path.dirname(fullPath);
         const basename = path.basename(fullPath);
         for (const ext of extensions) {
         const partialPath = path.join(dir, '\_' + basename + ext);
         if (fs.existsSync(partialPath)) {
         return pathToFileURL(partialPath);
         }
         }
        
         // Return the path even if file doesn't exist yet, let Sass handle the error
         return pathToFileURL(fullPath);
         }
         }
        
         // Return null if no alias matches, let other importers handle it
         return null;
         },
        
         load(canonicalUrl) {
         if (canonicalUrl.protocol === 'file:') {
         // Convert file:// URL back to file path
         const filePath = fileURLToPath(canonicalUrl);
        
         if (fs.existsSync(filePath)) {
         const contents = fs.readFileSync(filePath, 'utf8');
         // Determine syntax based on file extension
         const syntax = filePath.endsWith('.sass') ? 'indented' : 'scss';
         return {
         contents,
         syntax,
         };
         }
         }
         return null;
         },
         };
        }
        
    -   sassPluginエクスポートを新しい実装に合わせて修正してください:
        
        const sassPlugin = (nextConfig = {}) => {
         const aliases = {
         '@sass': path.join(\_\_dirname, '../../../assets', 'sass'),
         '@fontawesome': path.join(\_\_dirname, '../../../../node\_modules', 'font-awesome'),
         };
        
         return Object.assign({}, nextConfig, {
         sassOptions: {
         importers: createAliasImporter(aliases),
         // temporary measure until new versions of bootstrap and font-awesome released
         quietDeps: true,
         silenceDeprecations: "import", "legacy-js-api",
         },
         webpack: (config, options) => {
         // Exclude Node.js built-in modules used by this plugin from client bundle
         if (!options.isServer) {
         config.resolve.fallback = {
         ...config.resolve.fallback,
         fs: false,
         path: false,
         url: false,
         };
         }
        
         // Call existing webpack config if present
         if (typeof nextConfig.webpack === 'function') {
         return nextConfig.webpack(config, options);
         }
        
         return config;
         },
         });
        };
        

## 今後のステップ

アップグレードプロセスを最終決定するには、発生したエラーや警告を必ず解決してください。必要に応じて、JSS固有の問題に対してデバッグログを有効にしてください。